Redis哨兵模式(Docker)

  1. 获取配置
    #----------------------------------------------------------
    拉取镜像 docker pull redis:latest
    #----------------------------------------------------------
    创建容器 docker run --name redis -d redis:latest
    #----------------------------------------------------------
    获取版本 docker exec redis redis-server --version
    #----------------------------------------------------------
    删除容器 docker rm -f redis
    #----------------------------------------------------------
    获取配置 http://download.redis.io/releases 解压获取 redis.conf sentinel.conf
    注意事项 redis.conf sentinel.conf 同镜像中Redis版本必须一致 否则容器启动异常
    #----------------------------------------------------------
  2. 编辑配置
    #----------------------------------------------------------
    /opt/redis/conf/redis_6380.conf |# bind 127.0.0.1 -::1|protected-mode no|appendonly yes|appendfsync always|# appendfsync everysec|port 6380|
    /opt/redis/conf/redis_6381.conf |# bind 127.0.0.1 -::1|protected-mode no|appendonly yes|appendfsync always|# appendfsync everysec|port 6381|
    /opt/redis/conf/redis_6382.conf |# bind 127.0.0.1 -::1|protected-mode no|appendonly yes|appendfsync always|# appendfsync everysec|port 6382|
    #-------------------------主从模式-------------------------
    /opt/redis/conf/redis_6381.conf |replicaof  6380|
    /opt/redis/conf/redis_6382.conf |replicaof  6380|
    #-------------------------哨兵模式-------------------------
    /opt/redis/conf/sentinel_26380.conf |# bind 127.0.0.1 192.168.1.1|protected-mode no|sentinel monitor mymaster  6380 1|sentinel down-after-milliseconds mymaster 5000|sentinel failover-timeout mymaster 10000|port 26380|
    /opt/redis/conf/sentinel_26381.conf |# bind 127.0.0.1 192.168.1.1|protected-mode no|sentinel monitor mymaster  6380 1|sentinel down-after-milliseconds mymaster 5000|sentinel failover-timeout mymaster 10000|port 26381|
    /opt/redis/conf/sentinel_26382.conf |# bind 127.0.0.1 192.168.1.1|protected-mode no|sentinel monitor mymaster  6380 1|sentinel down-after-milliseconds mymaster 5000|sentinel failover-timeout mymaster 10000|port 26382|
    #----------------------------------------------------------
  3. 一主二从
    #-------------------------一主二从-------------------------
    docker rm -f redis_6380 redis_6381 redis_6382
    #----------------------------------------------------------
    docker run --name redis_6380 \
    --restart=always \
    --network host \
    -v /opt/redis/conf/redis_6380.conf:/etc/redis/redis.conf \
    -v /opt/redis/data/6380:/data \
    -d redis:latest \
    redis-server /etc/redis/redis.conf
    #----------------------------------------------------------
    docker run --name redis_6381 \
    --restart=always \
    --network host \
    -v /opt/redis/conf/redis_6381.conf:/etc/redis/redis.conf \
    -v /opt/redis/data/6381:/data \
    -d redis:latest \
    redis-server /etc/redis/redis.conf
    #----------------------------------------------------------
    docker run --name redis_6382 \
    --restart=always \
    --network host \
    -v /opt/redis/conf/redis_6382.conf:/etc/redis/redis.conf \
    -v /opt/redis/data/6382:/data \
    -d redis:latest \
    redis-server /etc/redis/redis.conf
    #----------------------------------------------------------
    firewall-cmd --zone=public --add-port=6380/tcp --permanent && firewall-cmd --reload
    firewall-cmd --zone=public --add-port=6381/tcp --permanent && firewall-cmd --reload
    firewall-cmd --zone=public --add-port=6382/tcp --permanent && firewall-cmd --reload
    #----------------------------------------------------------
    # 主从模式下 主节点可以增删改查 从节点只能查 主节点宕机则集群只能查 主节点恢复则集群自动恢复
    #----------------------------------------------------------
  4. 三个哨兵
    #-------------------------三个哨兵-------------------------
    docker rm -f sentinel_26380 sentinel_26381 sentinel_26382
    #----------------------------------------------------------
    docker run --name sentinel_26380 \
    --restart=always \
    --network host \
    -v /opt/redis/conf/sentinel_26380.conf:/etc/redis/sentinel.conf \
    -d redis:latest \
    redis-sentinel /etc/redis/sentinel.conf
    #----------------------------------------------------------
    docker run --name sentinel_26381 \
    --restart=always \
    --network host \
    -v /opt/redis/conf/sentinel_26381.conf:/etc/redis/sentinel.conf \
    -d redis:latest \
    redis-sentinel /etc/redis/sentinel.conf
    #----------------------------------------------------------
    docker run --name sentinel_26382 \
    --restart=always \
    --network host \
    -v /opt/redis/conf/sentinel_26382.conf:/etc/redis/sentinel.conf \
    -d redis:latest \
    redis-sentinel /etc/redis/sentinel.conf
    #----------------------------------------------------------
    firewall-cmd --zone=public --add-port=26380/tcp --permanent && firewall-cmd --reload
    firewall-cmd --zone=public --add-port=26381/tcp --permanent && firewall-cmd --reload
    firewall-cmd --zone=public --add-port=26382/tcp --permanent && firewall-cmd --reload
    #----------------------------------------------------------
    # 哨兵模式下 主节点宕机 从节点被切换为主节点 主节点恢复后变为从节点 哨兵数量至少2个 否则无法执行切换
    #----------------------------------------------------------
  5. 项目配置
    #----------------------------------------------------------
    spring:
      redis:
        sentinel:
          master: mymaster # 哨兵模式下的主节点名称
          nodes: :26380,:26381,:26382 # 哨兵节点的地址列表
          password: ''  # 设置哨兵节点的密码
        lettuce:
          pool:
            max-active: 8   # 最大连接数据库连接数,设 0 为没有限制
            max-idle: 8     # 最大等待连接中的数量,设 0 为没有限制
            max-wait: -1ms  # 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
            min-idle: 0     # 最小等待连接中的数量,设 0 为没有限制
          shutdown-timeout: 100ms
    #----------------------------------------------------------
    	@Value("${spring.redis.sentinel.nodes}")
    	private String sentinelNodes;
    
    	@Value("${spring.redis.sentinel.master}")
    	private String sentinelMaster;
    
    	@Value("${spring.redis.sentinel.password}")
    	private String sentinelPassword;
    
        public RedisCacheManager redisCacheManager() {
            log.info("===============(1)创建缓存管理器RedisCacheManager");
            RedisCacheManager redisCacheManager = new RedisCacheManager();
            redisCacheManager.setRedisManager(redisManager());
            //redis中针对不同用户缓存(此处的id需要对应user实体中的id字段,用于唯一标识)
            redisCacheManager.setPrincipalIdFieldName("id");
            //用户权限信息缓存时间
            redisCacheManager.setExpire(200000);
            return redisCacheManager;
        }
    
    	@Bean
    	public RedisManager redisManager() {
    		log.info("===============(2)创建RedisManager,连接Redis..URL= " + sentinelNodes + ", masterName=" + sentinelMaster);
    		RedisManager redisManager = new RedisManager();
    		redisManager.setHost(sentinelNodes);
    		if (!StringUtils.isEmpty(sentinelPassword)) {
    			redisManager.setPassword(sentinelPassword);
    		}
    		return redisManager;
    	}
    #----------------------------------------------------------
    

你可能感兴趣的:(redis,docker,主从模式,哨兵模式)