Redis集群

Redis 的哨兵模式基本已经可以实现高可用,读写分离 ,但是在这种模式下每台 Redis 服务器都存储相同的数据,很浪费内存,所以在redis3.0上加入了 Cluster 集群模式,实现了 Redis 的分布式存储,也就是说每台 Redis 节点上存储不同的内容。

 

根据官方推荐,集群部署至少要 3 台以上的master节点,最好使用 3 主 3 从六个节点的模式。在测试环境中,只能在一台机器上面开启6个服务实例来模拟。

1、修改配置文件

将 redis.conf 的配置文件复制6份到7000 .....7005(文件名最好加上端口后缀),然后开始修改配置文件中的参数

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005

采用最下配置,以7000为例(其他的参考修改端口和config-file):

port 7000

protected-mode no

cluster-enabled yes
cluster-config-file nodes7000.conf
cluster-node-timeout 5000
appendonly yes

cd 7000 

../redis-server ./redis.conf

启动6个服务:

运行以下命令集群(5.0以上版本)

 

redis-cli --cluster create --replicas 192.168.64.131:7000 192.168.64.131:7001 192.168.64.131:7002 192.168.64.131:7003 192.168.64.131:7004 192.168.64.131:7005 --cluster-replicas 1

JAVA集群测试:

public class TestCluster {

	public static void main(String[] args) {
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.64.131",7000));
        nodes.add(new HostAndPort("192.168.64.131",7001));
        nodes.add(new HostAndPort("192.168.64.131",7002));
        nodes.add(new HostAndPort("192.168.64.131",7003));
        nodes.add(new HostAndPort("192.168.64.131",7004));
        nodes.add(new HostAndPort("192.168.64.131",7005));
        
        JedisCluster jedisCluster = new JedisCluster(nodes);
        //使用jedisCluster操作redis
        jedisCluster.set("tes4", "testkey444");
        String str = jedisCluster.get("tes4");
        System.out.println(str);
        //关闭连接池
        try {
			jedisCluster.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  每运行一次会将数据插入到不同的redis实例中,3主3从

 



 

posted on 2020-04-05 01:49  卖肾割阑尾  阅读(102)  评论(0编辑  收藏  举报

导航