spring boot 配置redis(高可用) 哨兵主从复制【windows版】

redis 哨兵主从配置

简述sentinel
  • 监控:哨兵不断的检查master和slave是否正常的运行。
  • 通知:当监控的某台Redis实例发生问题时,可以通过API通知系统管理员和其他的应用程序。
  • 自动故障转移:如果一个master不正常运行了,哨兵可以启动一个故障转移进程,将一个slave升级成为master,其他的slave被重新配置使用新的master,并且应用程序使用Redis服务端通知的新地址。
  • 配置提供者:哨兵作为Redis客户端发现的权威来源:客户端连接到哨兵请求当前可靠的master的地址。如果发生故障,哨兵将报告新地址。
一主二从三哨兵
  1. 主redis:127.0.0.1:6379
  2. 从1:127.0.0.1:6389
  3. 从2:127.0.0.1:6399
  4. 哨兵1:127.0.0.1:26379
  5. 哨兵2:127.0.0.1:26389
  6. 哨兵3:127.0.0.1:26399
redis 主从配置 redis.windows.conf 设置
  • 主redis 配置不需要改变
  • 从redis 配置修改如下:
    1 端口修改:port 6389 port 6399
    2 复制主库:在代码行#slaveof 127.0.0.1 6379下面加入saveof 127.0.0.0 6379
    到此表示 主从复制已经完成;
redis 哨兵配置 sentinel.conf 配置
  • 安装包中不存在sentinel.conf 文件,则新建该文件夹
  • 复制如下代码到sentinel.conf 文件中
#端口
port 26379
# 内网地址,不可使用127.0.0.1,原因在spring boot 连接哨兵是 报错;
bind 10.13.14.21
# 哨兵监控的 master, [IP] [Port] [法定人数]
sentinel monitor mymaster 127.0.0.1 6379 2
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds mymaster 5000
#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout mymaster 15000
#设置master和slaves验证密码
sentinel auth-pass mymaster 123456
# 保护模式
protected-mode yes
# 指明日志文件名
logfile "./sentinel.log"

设置三个哨兵不同的端口 26379、26389、26399
到此 哨兵配置完成;

redis 服务安装到windows
  • 进入不同的redis安装目录,执行如下命令:
redis-server --service-install redis.windows.conf --loglevel verbose  --service-name redis6379

依次安装三个redis服务;

redis 哨兵服务安装到windows
  • 进入不同的redis安装目录,执行如下命令:
redis-server --service-install sentinel.conf --sentinel  --loglevel verbose  --service-name redissentinel26379

依次安装三个哨兵服务;

启动redis 服务以及哨兵服务,查看结果
  • redis 服务启动后,两个从库会复制主库数据,使用redis 桌面客户端可查


    spring boot 配置redis(高可用) 哨兵主从复制【windows版】_第1张图片
    image.png
  • redis 哨兵启动,使用命令 redis-cli -h [IP] -p [port] -a [pwd]连接哨兵服务器
  • 连接哨兵服务器后,使用命令info 查看redis 集群服务情况
image.png
spring boot 配置redis(高可用) 哨兵主从复制【windows版】_第2张图片
image.png

spring boot 配置redis(高可用) 哨兵主从复制【windows版】_第3张图片
image.png

Spring Boot 连接哨兵以及redis 服务

  • pom 文件
   
        
            org.springframework.boot
            spring-boot-starter-data-redis
            
                
                    redis.clients
                    jedis
                
                
                    io.lettuce
                    lettuce-core
                
            
        
        
            redis.clients
            jedis
        
  • yml 文件
spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password:
    jedis:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1
        min-idle: 0
    timeout: 6000
    sentinel:
      master: mymaster
      nodes: 127.0.0.1:26379,127.0.0.1:26389,127.0.0.1:26399
  • java 代码
/**
 * redis 配置类
 */
@Configuration
public class RedisConfig {

    @Bean
    public  RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        template.setHashValueSerializer(jsonRedisSerializer);
        template.setValueSerializer(jsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

你可能感兴趣的:(spring boot 配置redis(高可用) 哨兵主从复制【windows版】)