Redis高可用-集群部署

redis配置

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

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

Plain 复制代码
redis-server --service-install redis.windows.conf --service-name redis6379 --loglevel verbose

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

Plain 复制代码
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,会自动生成三主三从节点:

Plain 复制代码
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

代码示例

Java 复制代码
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,分布在不同节点上

相关推荐
考虑考虑29 分钟前
Redis8中的布谷鸟过滤器
redis·后端·程序员
鼠鼠我捏,要死了捏14 小时前
Redis、Zookeeper 与关系型数据库分布式锁方案对比及性能优化实战指南
redis·zookeeper·分布式锁
长安城没有风20 小时前
从入门到精通【Redis】安装Redis以及基本全局命令
数据库·redis
掘金-我是哪吒20 小时前
分布式微服务系统架构第168集:不要让“百万用户”直连 Redis
redis·分布式·微服务·架构·系统架构
芒果量化20 小时前
redis - 远程发送买卖信号、本地接收信号处理
redis·python·金融
叫我阿柒啊21 小时前
从全栈开发到云原生:一位Java工程师的实战经验分享
java·spring boot·redis·云原生·kafka·vue·全栈开发
没有bug.的程序员21 小时前
Redis 数据结构全面解析:从底层编码到实战应用
java·数据结构·redis·wpf
LQ深蹲不写BUG1 天前
Redis的五种常用数据类型。
数据库·redis·缓存
感哥1 天前
Redis基本数据类型
redis