ETJava Beta | Java    注册   登录
  • 搜索:
  • Redis高可用-集群部署

    发表于      阅读(1)     博客类别:Crawler     转自:https://www.cnblogs.com/muoying/p/18499165
    如有侵权 请联系我们删除  (页面底部联系我们)  

    redis配置

    Redis集群需要至少3个主节点,为保证数据的完整性每个主节点至少需要一个从节点,所以至少需要准备6Redis服务

    img

    建议将redis注册为系统服务并设置自启动,服务注册命令为:

    redis-server --service-install redis.windows.conf --service-name redis6379 --loglevel verbose
    

    img

    img

    分别修改 redis.windows.config 中以下配置:

    bind 127.0.0.1                               // ip地址
    port 6379                                    // 端口
    logfile "redis79.log"                        // 日志文件名称
    
    cluster-enabled yes                          // 开启集群支持
    cluster-config-file nodes-6379.conf          // 集群配置文件名称
    cluster-node-timeout 15000                   // 集群节点超时时间 ms 
    

    启动所有Redis服务后,在任一Redis目录下执行创建集群命令,副本数为1,会自动生成三主三从节点:

    redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
    

    img

    代码示例

    public static void main(String[] args) {
    
        // 集群节点信息
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("127.0.0.1", 6379));
        nodes.add(new HostAndPort("127.0.0.1", 6380));
        nodes.add(new HostAndPort("127.0.0.1", 6381));
        nodes.add(new HostAndPort("127.0.0.1", 6382));
        nodes.add(new HostAndPort("127.0.0.1", 6383));
        nodes.add(new HostAndPort("127.0.0.1", 6384));
    
        // 连接池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMinIdle(1);
    
        // 初始化集群对象,全局唯一
        JedisCluster cluster = new JedisCluster(nodes, jedisPoolConfig);
    
        // 执行命令
        cluster.set("key1", "1");
        cluster.set("key12", "12");
        cluster.set("key123", "123");
        cluster.set("key1234", "1234");
        cluster.set("key12345", "12345");
        cluster.set("key123456", "123456");
        cluster.set("key1234567", "1234567");
        cluster.set("key12345678", "12345678");
        cluster.set("key123456789", "123456789");
    
        cluster.close();
    }
    

    添加的9key,分布在不同节点上

    img