redis高可用三:redis-cluster

一:说明

redis-cluster需要6台redis才行,分为3组,每组都是1主1从。

二:启动6台redis

修改redis.conf 文件为redis1.conf redis2,conf redis3,conf redis4,conf redis5,conf redis6,conf

配置文件做如下修改,端口肯定是都不一样的,然后启动:

daemonize yes
protected-mode no
cluster-enabled yes
cluster-config-file nodes2.conf
cluster-node-timeout 5000
port 6372
>> ps -ef|grep redis看到
root      2912     1  0 13:48 ?        00:00:00 ./redis-server *:6371 [cluster]
root      2923     1  0 13:48 ?        00:00:00 ./redis-server *:6372 [cluster]
root      2930     1  0 13:48 ?        00:00:00 ./redis-server *:6373 [cluster]
root      2938     1  0 13:48 ?        00:00:00 ./redis-server *:6374 [cluster]
root      2946     1  0 13:48 ?        00:00:00 ./redis-server *:6375 [cluster]
root      2956     1  0 13:48 ?        00:00:00 ./redis-server *:6376 [cluster]
三:执行ruby
>> yum install ruby rubygems -y
>> 安装gem-redis
>> https://rubygems.org/gems/redis/versions/3.0.0
>> gem install -l redis-3.0.0.gem
>> ## 一定一定要注意,这里的ip是你主机的ip,而不一定是127.0.0.1 ,到时候用jedis去连接,这里的ip就是相对jedis的地址  
>> ./redis-trib.rb create --replicas 1 127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373 127.0.0.1:6374 127.0.0.1:6375 127.0.0.1:6376
四:Jedis
public class Main {
    public static void main(String[] args) throws InterruptedException {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(50);
        config.setMinIdle(20);
        config.setMaxWaitMillis(6 * 1000);
        config.setTestOnBorrow(true);
        Set jedisClusterNodes = new HashSet();
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6371));
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6372));
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6373));
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6374));
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6375));
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6376));
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, 2000, 100, config);
        String key = "aa";
        for (int i = 1; i <= 100; i++) {
            jedisCluster.set(key + i, key + i);
            String value = jedisCluster.get(key + i);
            System.out.println(value);
        }
    }
}

你可能感兴趣的:(redis)