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,分布在不同节点上

相关推荐
@_@哆啦A梦1 小时前
Redis 基础命令
java·数据库·redis
gentle coder3 小时前
Redis_Redission的入门案例、多主案例搭建、分布式锁进行加锁、解锁底层源码解析
java·redis·分布式
萝卜青今天也要开心3 小时前
读书笔记-《Redis设计与实现》(一)数据结构与对象(下)
java·数据结构·redis·学习
java1234_小锋5 小时前
说说Redis的内存淘汰策略?
数据库·redis·缓存
2的n次方_10 小时前
【Redis】set 和 zset 类型的介绍和常用命令
数据库·redis·缓存
桂月二二14 小时前
使用 Redis Streams 实现高性能消息队列
数据库·redis·缓存
biubiubiu07061 天前
Redisson
redis
maply1 天前
Redis 消息队列详解
数据库·redis·缓存
java1234_小锋1 天前
怎么实现Redis的高可用?
数据库·redis·缓存
�时过境迁,物是人非1 天前
Redis地理散列GeoHash
前端·redis·bootstrap