• Redis哨兵模式(Docker)


    1. 获取配置
      1. #----------------------------------------------------------
      2. 拉取镜像 docker pull redis:latest
      3. #----------------------------------------------------------
      4. 创建容器 docker run --name redis -d redis:latest
      5. #----------------------------------------------------------
      6. 获取版本 docker exec redis redis-server --version
      7. #----------------------------------------------------------
      8. 删除容器 docker rm -f redis
      9. #----------------------------------------------------------
      10. 获取配置 http://download.redis.io/releases 解压获取 redis.conf sentinel.conf
      11. 注意事项 redis.conf sentinel.conf 同镜像中Redis版本必须一致 否则容器启动异常
      12. #----------------------------------------------------------
    2. 编辑配置
      1. #----------------------------------------------------------
      2. /opt/redis/conf/redis_6380.conf |# bind 127.0.0.1 -::1|protected-mode no|appendonly yes|appendfsync always|# appendfsync everysec|port 6380|
      3. /opt/redis/conf/redis_6381.conf |# bind 127.0.0.1 -::1|protected-mode no|appendonly yes|appendfsync always|# appendfsync everysec|port 6381|
      4. /opt/redis/conf/redis_6382.conf |# bind 127.0.0.1 -::1|protected-mode no|appendonly yes|appendfsync always|# appendfsync everysec|port 6382|
      5. #-------------------------主从模式-------------------------
      6. /opt/redis/conf/redis_6381.conf |replicaof <host-ip> 6380|
      7. /opt/redis/conf/redis_6382.conf |replicaof <host-ip> 6380|
      8. #-------------------------哨兵模式-------------------------
      9. /opt/redis/conf/sentinel_26380.conf |# bind 127.0.0.1 192.168.1.1|protected-mode no|sentinel monitor mymaster <host-ip> 6380 1|sentinel down-after-milliseconds mymaster 5000|sentinel failover-timeout mymaster 10000|port 26380|
      10. /opt/redis/conf/sentinel_26381.conf |# bind 127.0.0.1 192.168.1.1|protected-mode no|sentinel monitor mymaster <host-ip> 6380 1|sentinel down-after-milliseconds mymaster 5000|sentinel failover-timeout mymaster 10000|port 26381|
      11. /opt/redis/conf/sentinel_26382.conf |# bind 127.0.0.1 192.168.1.1|protected-mode no|sentinel monitor mymaster <host-ip> 6380 1|sentinel down-after-milliseconds mymaster 5000|sentinel failover-timeout mymaster 10000|port 26382|
      12. #----------------------------------------------------------
    3. 一主二从
      1. #-------------------------一主二从-------------------------
      2. docker rm -f redis_6380 redis_6381 redis_6382
      3. #----------------------------------------------------------
      4. docker run --name redis_6380 \
      5. --restart=always \
      6. --network host \
      7. -v /opt/redis/conf/redis_6380.conf:/etc/redis/redis.conf \
      8. -v /opt/redis/data/6380:/data \
      9. -d redis:latest \
      10. redis-server /etc/redis/redis.conf
      11. #----------------------------------------------------------
      12. docker run --name redis_6381 \
      13. --restart=always \
      14. --network host \
      15. -v /opt/redis/conf/redis_6381.conf:/etc/redis/redis.conf \
      16. -v /opt/redis/data/6381:/data \
      17. -d redis:latest \
      18. redis-server /etc/redis/redis.conf
      19. #----------------------------------------------------------
      20. docker run --name redis_6382 \
      21. --restart=always \
      22. --network host \
      23. -v /opt/redis/conf/redis_6382.conf:/etc/redis/redis.conf \
      24. -v /opt/redis/data/6382:/data \
      25. -d redis:latest \
      26. redis-server /etc/redis/redis.conf
      27. #----------------------------------------------------------
      28. firewall-cmd --zone=public --add-port=6380/tcp --permanent && firewall-cmd --reload
      29. firewall-cmd --zone=public --add-port=6381/tcp --permanent && firewall-cmd --reload
      30. firewall-cmd --zone=public --add-port=6382/tcp --permanent && firewall-cmd --reload
      31. #----------------------------------------------------------
      32. # 主从模式下 主节点可以增删改查 从节点只能查 主节点宕机则集群只能查 主节点恢复则集群自动恢复
      33. #----------------------------------------------------------
    4. 三个哨兵
      1. #-------------------------三个哨兵-------------------------
      2. docker rm -f sentinel_26380 sentinel_26381 sentinel_26382
      3. #----------------------------------------------------------
      4. docker run --name sentinel_26380 \
      5. --restart=always \
      6. --network host \
      7. -v /opt/redis/conf/sentinel_26380.conf:/etc/redis/sentinel.conf \
      8. -d redis:latest \
      9. redis-sentinel /etc/redis/sentinel.conf
      10. #----------------------------------------------------------
      11. docker run --name sentinel_26381 \
      12. --restart=always \
      13. --network host \
      14. -v /opt/redis/conf/sentinel_26381.conf:/etc/redis/sentinel.conf \
      15. -d redis:latest \
      16. redis-sentinel /etc/redis/sentinel.conf
      17. #----------------------------------------------------------
      18. docker run --name sentinel_26382 \
      19. --restart=always \
      20. --network host \
      21. -v /opt/redis/conf/sentinel_26382.conf:/etc/redis/sentinel.conf \
      22. -d redis:latest \
      23. redis-sentinel /etc/redis/sentinel.conf
      24. #----------------------------------------------------------
      25. firewall-cmd --zone=public --add-port=26380/tcp --permanent && firewall-cmd --reload
      26. firewall-cmd --zone=public --add-port=26381/tcp --permanent && firewall-cmd --reload
      27. firewall-cmd --zone=public --add-port=26382/tcp --permanent && firewall-cmd --reload
      28. #----------------------------------------------------------
      29. # 哨兵模式下 主节点宕机 从节点被切换为主节点 主节点恢复后变为从节点 哨兵数量至少2个 否则无法执行切换
      30. #----------------------------------------------------------
    5. 项目配置
      1. #----------------------------------------------------------
      2. spring:
      3. redis:
      4. sentinel:
      5. master: mymaster # 哨兵模式下的主节点名称
      6. nodes: <host-ip>:26380,<host-ip>:26381,<host-ip>:26382 # 哨兵节点的地址列表
      7. password: '' # 设置哨兵节点的密码
      8. lettuce:
      9. pool:
      10. max-active: 8 # 最大连接数据库连接数,设 0 为没有限制
      11. max-idle: 8 # 最大等待连接中的数量,设 0 为没有限制
      12. max-wait: -1ms # 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
      13. min-idle: 0 # 最小等待连接中的数量,设 0 为没有限制
      14. shutdown-timeout: 100ms
      15. #----------------------------------------------------------
      16. @Value("${spring.redis.sentinel.nodes}")
      17. private String sentinelNodes;
      18. @Value("${spring.redis.sentinel.master}")
      19. private String sentinelMaster;
      20. @Value("${spring.redis.sentinel.password}")
      21. private String sentinelPassword;
      22. public RedisCacheManager redisCacheManager() {
      23. log.info("===============(1)创建缓存管理器RedisCacheManager");
      24. RedisCacheManager redisCacheManager = new RedisCacheManager();
      25. redisCacheManager.setRedisManager(redisManager());
      26. //redis中针对不同用户缓存(此处的id需要对应user实体中的id字段,用于唯一标识)
      27. redisCacheManager.setPrincipalIdFieldName("id");
      28. //用户权限信息缓存时间
      29. redisCacheManager.setExpire(200000);
      30. return redisCacheManager;
      31. }
      32. @Bean
      33. public RedisManager redisManager() {
      34. log.info("===============(2)创建RedisManager,连接Redis..URL= " + sentinelNodes + ", masterName=" + sentinelMaster);
      35. RedisManager redisManager = new RedisManager();
      36. redisManager.setHost(sentinelNodes);
      37. if (!StringUtils.isEmpty(sentinelPassword)) {
      38. redisManager.setPassword(sentinelPassword);
      39. }
      40. return redisManager;
      41. }
      42. #----------------------------------------------------------
  • 相关阅读:
    大数据学长面试京东面试题
    js:Class对象中的函数,在使用 this 时理解
    应收和应付会计岗位自动化:多重赋能,为企业提效300%,让劳动更具价值
    【无标题】
    神经网络怎么调整权重,bp神经网络确定权重
    智能指针(Newbie Note)
    算法基础课
    不会还有人觉得会员营销很难做吧?教你几招速成!
    Android 13.0 进入recovery模式(等待用户选择recovery模式界面)进入自动恢复出厂设置模式
    Speedoffice(word)如何添加小方框和勾
  • 原文地址:https://blog.csdn.net/tongxin_tongmeng/article/details/134469708