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

相关推荐
AAA修煤气灶刘哥16 小时前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
凯子坚持 c1 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
weixin_456904271 天前
跨域(CORS)和缓存中间件(Redis)深度解析
redis·缓存·中间件
波波烤鸭1 天前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
MarkHard1232 天前
如何利用redis使用一个滑动窗口限流
数据库·redis·缓存
island13142 天前
【Redis#10】渐进式遍历 | 数据库管理 | redis_cli | RES
数据库·redis·bootstrap
心想事成的幸运大王2 天前
Redis的过期策略
数据库·redis·缓存
wuyunhang1234562 天前
Redis---集群模式
数据库·redis·缓存
Seven972 天前
Redis是如何进行内存管理的?缓存中有哪些常见问题?如何实现分布式锁?
redis