Redis的集群 与 Java连接Redis集群

Redis的集群 与 Java连接Redis集群,第1张

Redis的集群 与 Java连接Redis集群

主从结构添加哨兵能够解决读写分离和单点故障高可用

但是如果数据量过大,特别是写数据量过大,这个时候一个主节点是不够的,需要进行集群搭建↓

Redis集群在保证主从加哨兵的基本功能之外,还能够提升Redis存储数据的能力↓

配置上图的集群,可以按照以下步骤配置:

停止并删除所有容器,创建docker_redis_cluster文件夹,

里面编辑docker-compose.yml文件:

version: "3.1"
services:
  redis1:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis1
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7001:7001
      - 17001:17001
    volumes:
      - ./conf/redis1.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]
  redis2:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis2
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7002:7002
      - 17002:17002
    volumes:
      - ./conf/redis2.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]  
  redis3:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis3
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7003:7003
      - 17003:17003
    volumes:
      - ./conf/redis3.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]  
  redis4:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis4
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7004:7004
      - 17004:17004
    volumes:
      - ./conf/redis4.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]  
  redis5:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis5
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7005:7005
      - 17005:17005
    volumes:
      - ./conf/redis5.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]  
  redis6:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis6
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7006:7006
      - 17006:17006
    volumes:
      - ./conf/redis6.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]  

redis.conf 配置文件↓

# redis.conf
# 指定redis的端口号
port 7001
# 开启Redis集群
cluster-enabled yes
# 集群信息的文件
cluster-config-file nodes-7001.conf
# 集群的对外ip地址
cluster-announce-ip 192.168.200.129
# 集群的对外port
cluster-announce-port 7001
# 集群的总线端口
cluster-announce-bus-port 17001

启动容器:

启动了6个Redis的节点

由于redis集群是无中心的,随便跳转到一个容器内部,使用redis-cli命令增加--cluster参数来进行集群命令,yes↓

redis-cli --cluster create 192.168.200.xxx:7001 192.168.200.xxx:7002 192.168.200.xxx:7003 192.168.200.xxx:7004 192.168.200.xxx:7005 192.168.200.xxx:7006 --cluster-replicas 1

另外关于更多的命令可以用help帮助提示

redis集群扩展:

为什么至少三个哨兵_Redis集群为什么是三个以上_weixin_39712016的博客-CSDN博客

redis集群为什么最少需要6个节点-Redis-PHP中文网

京东面试题(Redis):为啥RedisCluster设计成16384个槽 - 知乎

为什么redis cluster至少需要三个主节点? - 知乎

Java连接Redis集群

使用JedisCluster对象连接Redis集群

 @Test
    public void testCluster(){
        Set nodes = new HashSet();//搞一个集合存储主机和端口对象
        //此处ip端口与集群中redis对应
        nodes.add(new HostAndPort("192.168.200.129",7001));
        nodes.add(new HostAndPort("192.168.200.129",7002));
        nodes.add(new HostAndPort("192.168.200.129",7003));
        nodes.add(new HostAndPort("192.168.200.129",7004));
        nodes.add(new HostAndPort("192.168.200.129",7005));
        nodes.add(new HostAndPort("192.168.200.129",7006));

        //主要创建JedisCluster集群对象,然后查看有参构造,缺啥补啥即可
        JedisCluster jedisCluster = new JedisCluster(nodes);

        String value = jedisCluster.get("a");//用集群对象来 *** 作命令
        System.out.println(value);//a
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存