centos7 安装 redis 集群

centos7 安装 redis 集群

标签(空格分隔): centos linux redis


redis集群

1.redis 集群安装

  1. 数据自动切分到多个节点
  2. 具有冗余,部分节点挂了之后继续可以进行,这里建了 6 个节点,6 个节点之间可以相互 ping,端口分别是 7000,7001,7002,7003, 7004, 7005

2.下载安装解压编译 redis

cd /usr/local/
wget http://download.redis.io/releases/redis-3.2.9.tar.gz

tar -zxvf redis-3.2.9.tar.gz
cd redis-3.2.9
yum install gcc-c++
make && make install

3.创建 redis 节点并且配置 redis

将 redis 的信息放到 data 目录,并且创建集群节点文件夹

mkdir /data/redis/cluster -p
cd /data/redis/cluster
mkdir 7000 7001 7002 7003 7004 7005
cp /usr/local/redis-3.2.9/redis.conf ./7000/
vim ./7000/redis.conf

修改 redis.conf

daemonize yes
pidfile /var/run/redis/redis-7000.pid
bind 192.168.80.227
port 7000
logfile ./7000/redis.log
dir ./7000/
cluster-enabled yes
cluster-config-file nodes.conf

将集群其他节点配置文件cp并且修改

4.启动 redis

redis-server ./7000/redis.conf
redis-server ./7001/redis.conf
redis-server ./7002/redis.conf
redis-server ./7003/redis.conf
redis-server ./7004/redis.conf
redis-server ./7005/redis.conf

查看进程是否存在

ps -ef | grep redis

5.创建redis集群

下载 ruby 和 rubygem

yum install ruby rubygems -y

安装 gem-redis,需要手工下载并安装:

wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install -l redis-3.2.1.gem

创建集群

cp /usr/local/redis-3.2.9/src/redis-trib.rb /usr/local/bin/redis-trib.rb
redis-trib.rb create --replicas 1 192.168.80.227:7000 192.168.80.227:7001 192.168.80.227:7002 192.168.80.227:7003 192.168.80.227:7004 192.168.80.227:7005

6.集群测试

1.设置 key: msg, value:hello

redis-cli -c -h 192.168.80.227 -p 7000
192.168.80.227:7000> set msg "hello"
OK
192.168.80.227:7001> get msg
hello

2.查看集群信息

redis-cli -h 192.168.80.227 -p 7000 -c cluster nodes

aa2380c9acb8e52bf7c98948728d19c45f7c4bb7 192.168.80.227:7001 slave
6aaa67aa6783c797297dacd537757347f58b89c0 0 1438830197669 7 connected
93fa026cedc8487d90916cce858f0189de17bc57 192.168.80.227:7005 master - 0
1438830200675 8 connected 10923-16383
44451f7f75b482d434798e801a9bab3536f513c6 192.168.80.227:7002 slave
93fa026cedc8487d90916cce858f0189de17bc57 0 1438830198671 8 connected
7997ecc7cd9e3bc0dfc3ddcad05ac6ca997b87ad 192.168.80.227:7003 slave
6aa6b557722f44acaf5eac71e8b51b8571de8131 0 1438830199674 4 connected
6aa6b557722f44acaf5eac71e8b51b8571de8131 192.168.80.227:7000 myself,master -
0 0 1 connected 0-5460
6aaa67aa6783c797297dacd537757347f58b89c0 192.168.80.227:7004 master - 0
1438830201678 7 connected 5461-10922

可以看到 7000, 7004, 7005 作为 master,其他的 salve, kill 了 7000 的进程

ps -ef | grep redis
kill 30740

链接 7001 的客户端,查询是否可以获取到值

redis-cli -h 192.168.80.227 -p 7001 -c
192.168.80.227:7001> get msg

spring与redis集成

1.依赖的 jar 包如下

commons-pool2-2.4.2.jar
jedis-2.8.1.jar

2.redis 的 spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig"
          p:minIdle="${redis.minIdle}"
          p:maxIdle="${redis.maxIdle}"
          p:maxTotal="${redis.maxTotal}"
          p:maxWaitMillis="${redis.maxWaitMillis}"/>

    <bean id="jedisCluster" class="com.kehua.platfrom.redis.JedisClusterFactory"
          p:genericObjectPoolConfig-ref="genericObjectPoolConfig"
          p:connectionTimeout="${redis.connectionTimeout}"
          p:soTimeout="${redis.soTimeout}"
          p:maxRedirections="${redis.maxRedirections}">
        <property name="jedisClusterNodes">
            <set>
                <value>${redis.port7000}</value>
                 <value>${redis.port7000}</value>
                 <value>${redis.port7000}</value>
                 <value>${redis.port7000}</value>
                 <value>${redis.port7000}</value>
                 <value>${redis.port7000}</value>
            </set>
        </property>
    </bean>

    <bean id="redisTools" class="com.kehua.platfrom.redis.RedisTools">
    <property name="jedisCluster" ref="jedisCluster"/>
    </bean>
</beans>

3.redis.properties 配置文件

redis.maxWaitMillis=1500
redis.maxTotal=2048
redis.minIdle=20
redis.maxIdle=200
redis.connectionTimeout=500
redis.soTimeout=500
redis.maxRedirections=10

4.JedisClusterFactory 核心类源码

package com.kehua.platfrom.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;

public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {

    private GenericObjectPoolConfig genericObjectPoolConfig;
    private JedisCluster jedisCluster;
    private int connectionTimeout = 2000;
    private int soTimeout = 3000;
    private int maxRedirections = 5;
    private Set<String> jedisClusterNodes;

    public void afterPropertiesSet() throws Exception {
        if (jedisClusterNodes == null || jedisClusterNodes.size() == 0) {
            throw new NullPointerException("jedisClusterNodes is null.");
        }
        Set<HostAndPort> haps = new HashSet<HostAndPort>();
        for (String node : jedisClusterNodes) {
            String[] arr = node.split(":");
            if (arr.length != 2) {
                throw new ParseException("node address error !", node.length() - 1);
            }
            haps.add(new HostAndPort(arr[0], Integer.valueOf(arr[1])));
        }
        jedisCluster = new JedisCluster(haps, connectionTimeout, 3, genericObjectPoolConfig);
    }

    public JedisCluster getObject() throws Exception {
        return jedisCluster;
    }

    public Class<?> getObjectType() {
        return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
    }

    public boolean isSingleton() {
        return true;
    }

    public GenericObjectPoolConfig getGenericObjectPoolConfig() {
        return genericObjectPoolConfig;
    }

    public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
        this.genericObjectPoolConfig = genericObjectPoolConfig;
    }

    public JedisCluster getJedisCluster() {
        return jedisCluster;
    }

    public void setJedisCluster(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public int getSoTimeout() {
        return soTimeout;
    }

    public void setSoTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
    }

    public int getMaxRedirections() {
        return maxRedirections;
    }

    public void setMaxRedirections(int maxRedirections) {
        this.maxRedirections = maxRedirections;
    }

    public Set<String> getJedisClusterNodes() {
        return jedisClusterNodes;
    }

    public void setJedisClusterNodes(Set<String> jedisClusterNodes) {
        this.jedisClusterNodes = jedisClusterNodes;
    }

}

5.RedisTools 工具类

package com.kehua.platfrom.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.JedisCluster;

public class RedisTools implements ICacheTools {

    public static class SerializeUtil {
        public static byte[] serialize(Object value) {
            if (value == null) {
                throw new NullPointerException("Can't serialize null");
            }
            byte[] rv = null;
            ByteArrayOutputStream bos = null;
            ObjectOutputStream os = null;
            try {
                bos = new ByteArrayOutputStream();
                os = new ObjectOutputStream(bos);
                os.writeObject(value);
                os.close();
                bos.close();
                rv = bos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(os);
                close(bos);
            }
            return rv;
        }

        public static Object deserialize(byte[] in) {
            return deserialize(in, Object.class);
        }

        @SuppressWarnings("unchecked")
        public static <T> T deserialize(byte[] in, Class<T> requiredType) {
            Object rv = null;
            ByteArrayInputStream bis = null;
            ObjectInputStream is = null;
            try {
                if (in != null) {
                    bis = new ByteArrayInputStream(in);
                    is = new ObjectInputStream(bis);
                    rv = is.readObject();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(is);
                close(bis);
            }
            return (T) rv;
        }

        private static void close(Closeable closeable) {
            if (closeable != null)
                try {
                    closeable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

    private JedisCluster jedisCluster;

    public void setJedisCluster(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    public String setObject(Object key, Object value) {
        String ret = jedisCluster.set(SerializeUtil.serialize(key), SerializeUtil.serialize(value));
        return ret;
    }

    public <T> T getObject(Object key, Class<T> clazz) {
        byte[] value = jedisCluster.get(SerializeUtil.serialize(key));
        return SerializeUtil.deserialize(value, clazz);
    }

    public String set(String key, String value) {
        String ret = jedisCluster.set(key, value);
        return ret;
    }

    public String get(String key) {
        String str = jedisCluster.get(key);
        return str;
    }

    public boolean exist(String key) {
        return jedisCluster.exists(key);
    }

    public long expireAt(String key, long xtime) {
        return jedisCluster.expireAt(key, xtime);
    }

    /**
     * 设置一个key的过期时间(单位:秒)
     *
     * @param key
     *            key值
     * @param seconds
     *            多少秒后过期
     * @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间
     */
    public long expire(String key, int seconds) {
        if (key == null || key.equals("")) {
            return 0;
        }
        return jedisCluster.expire(key, seconds);
    }

    public Long hset(final String key, final String field, final String value) {
        return jedisCluster.hset(key, field, value);
    }

    public String hget(final String key, final String field) {
        return jedisCluster.hget(key, field);
    }

    public Long lpush(final String key, final String... string) {
        return jedisCluster.lpush(key, string);
    }

    public Set<String> hkeys(final String key) {
        return jedisCluster.hkeys(key);
    }

    public List<String> hmget(final String key, final String... fields) {
        return jedisCluster.hmget(key, fields);
    }

    public Long hdel(final String key, final String... field) {
        return jedisCluster.hdel(key, field);
    }

    public List<String> lrange(final String key, final long start, final long end) {
        return jedisCluster.lrange(key, start, end);
    }

    public Long del(final String key) {
        return jedisCluster.del(key);
    }

    public Long setnx(final String key, final String value) {
        return jedisCluster.setnx(key, value);
    }

}

6.测试例子

/**
* 测试redis的保存与获取
*/
@RequestMapping("/saveRedis")
public void saveRedis() {
     String ruleLssuedKey =
     ConstCacheKey.business.rule.rulelssued_key+"sid_serialnum";//redis的key为: redis前辍+站点ID+设备串号
     Resp resp = new Resp();
     resp.setMsg("测试存储对象的功能-MSG");
     String json = JsonUtils.toJson(resp);
     redisTools.set(ruleLssuedKey, json);
     log.debug("--存储redis缓存成功! ");
     String rsJson = redisTools.get(ruleLssuedKey);
     log.debug("--获取redis缓存: "+rsJson);
     Resp r = JsonUtils.parse(rsJson, Resp.class);
     log.debug("--获取最终对象的属性: "+r.getMsg());
}

7.其他

package com.kehua.platfrom.redis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kehua.business.constant.RedisConstant;
import com.kehua.util.PropertiesUtil;
import com.kehua.util.SpringContextHolder;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

/**
 * <p>
 * Description:
 * </p>
 *
 * @date 2017年4月20日下午4:41:13
 * @author yxb
 * @version 1.0
 */
public class RedisConnect {

    protected static Log logger = LogFactory.getLog(RedisConnect.class);

    private static JedisClusterFactory jedisCluster = SpringContextHolder.getBean(JedisClusterFactory.class);

    /**
     *
     * 方法用途:连接redis集群服务器- <br>
     *
     * @author yxb
     * @version 1.0
     * @date 2017年4月20日下午4:56:06
     * @return
     */
    public static List<Jedis> connectAll() {

        Properties pro = PropertiesUtil.getProperty(RedisConstant.REDIS_PROPERTIES);

        logger.info("---------------------------------连接redis集群服务器-----------------------------");
        List<Jedis> list = new ArrayList<>();
        Jedis jedis1 = new Jedis(pro.getProperty(RedisConstant.REDIS_IP), 7000);
        Jedis jedis2 = new Jedis(pro.getProperty(RedisConstant.REDIS_IP), 7001);
        Jedis jedis3 = new Jedis(pro.getProperty(RedisConstant.REDIS_IP), 7002);
        Jedis jedis4 = new Jedis(pro.getProperty(RedisConstant.REDIS_IP), 7003);
        Jedis jedis5 = new Jedis(pro.getProperty(RedisConstant.REDIS_IP), 7004);
        Jedis jedis6 = new Jedis(pro.getProperty(RedisConstant.REDIS_IP), 7005);
        // 权限认证
        // jedis.auth("admin");
        list.add(jedis1);
        list.add(jedis2);
        list.add(jedis3);
        list.add(jedis4);
        list.add(jedis5);
        list.add(jedis6);

        return list;
    }

    /**
     *
     * 方法用途: 获取redis所有的key<br>
     *
     * @author yxb
     * @version 1.0
     * @date 2017年4月20日下午4:54:40
     * @param pattern
     * @return
     */
    public static List<String> getAllRedisKeys(String pattern) {
        List<String> keys = new ArrayList<>();
        List<Jedis> jediss = connectAll();
        for (Jedis jedis : jediss) {
            Set<String> set = jedis.keys(pattern);
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String keyStr = it.next();
                // System.out.println(keyStr);
                logger.debug("redis已存在的key值:" + keyStr);
                keys.add(keyStr);
            }
            jedis.close();
        }
        return keys;
    }

    /**
     *
     * 方法用途:获取指定的redis的keys <br>
     *
     * @author yxb
     * @version 1.0
     * @date 2017年4月20日下午5:16:35
     * @param ip
     * @param port
     * @param pattern
     * @return
     */
    public static List<String> getRedisKeysByIpPort(String ip, int[] port, String pattern) {
        List<String> keys = new ArrayList<>();
        List<Jedis> jediss = connects(ip, port);
        for (Jedis jedis : jediss) {
            Set<String> set = jedis.keys(pattern);
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String keyStr = it.next();
                System.out.println(keyStr);
                keys.add(keyStr);
            }
            jedis.close();
        }
        return keys;
    }

    /**
     *
     * 方法用途:获取指定的redis <br>
     *
     * @author yxb
     * @version 1.0
     * @date 2017年4月20日下午5:16:14
     * @param ip
     * @param port
     * @return
     */
    public static List<Jedis> connects(String ip, int[] port) {
        List<Jedis> list = new ArrayList<>();
        for (int p : port) {
            Jedis jedis = new Jedis(ip, p);
            list.add(jedis);
        }
        return list;
    }

    /**
     * 方法用途: 获取所有redis的集群节点<br>
     *
     * @author yxb
     * @version 1.0
     * @date 2017年4月20日下午5:17:30
     */
    public static List<String> getRedisNodes() {
        List<String> nodes = new ArrayList<>();
        Set<String> jedisClusterNodesa = jedisCluster.getJedisClusterNodes();
        for (String str : jedisClusterNodesa) {
            nodes.add(str);
        }
        return nodes;
    }

    /**
     * 方法用途: <br>
     *
     * @author yxb
     * @version 1.0
     * @throws Exception
     * @date 2017年4月20日下午5:21:32
     */
    public static List<String> getKeys() throws Exception {
        List<String> list = new ArrayList<>();
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

        Properties pro = PropertiesUtil.getProperty(RedisConstant.REDIS_PROPERTIES);

        jedisClusterNodes.add(new HostAndPort(pro.getProperty(RedisConstant.REDIS_IP), 7000));
        jedisClusterNodes.add(new HostAndPort(pro.getProperty(RedisConstant.REDIS_IP), 7001));
        jedisClusterNodes.add(new HostAndPort(pro.getProperty(RedisConstant.REDIS_IP), 7002));
        jedisClusterNodes.add(new HostAndPort(pro.getProperty(RedisConstant.REDIS_IP), 7003));
        jedisClusterNodes.add(new HostAndPort(pro.getProperty(RedisConstant.REDIS_IP), 7004));
        jedisClusterNodes.add(new HostAndPort(pro.getProperty(RedisConstant.REDIS_IP), 7005));
        JedisCluster jc = new JedisCluster(jedisClusterNodes);
        Set<String> hkeys = jc.hkeys("*");
        for (String string : hkeys) {
            System.out.println(string);
            list.add(string);
        }
        jc.close();
        return list;
    }

}
发布了10 篇原创文章 · 获赞 2 · 访问量 1624

猜你喜欢

转载自blog.csdn.net/qq_35623011/article/details/89081050