Redis高可用(一主二从三哨兵)

Redis高可用(一主二从三哨兵)

一、准备工作

  1. 将Redis的安装包解压之后,新建两个文件夹conf和logs,方便管理配置和查看日志。   在这里插入图片描述

  2. 将redis.windows-service.conf拷贝6份至conf文件夹下,分别命名如下  在这里插入图片描述

二、主从节点部署

  1. 修改配置
    a) 主节点配置,以redis-6379.conf作为主节点,其它默认配置不变,以下配置项做如下修改:
    logfile "logs/6379.log"
    dbfilename dump-6379.rdb
    

b) 从节点配置,两个从节点redis-6389.conf和redis-6399.conf,其它默认配置不变,以下配置项做如下修改:

logfile "logs/6389.log"
dbfilename dump-6389.rdb
slaveof 127.0.0.1 6379
  1. 部署并启动
    a) 部署节点

    redis-server --service-install ./conf/redis-6379.conf --service-name redis-6379
    redis-server --service-install ./conf/redis-6389.conf --service-name redis-6389
    redis-server --service-install ./conf/redis-6399.conf --service-name redis-6399
    

b) 启动节点
 在windows服务中启动
在这里插入图片描述

c) 验证是否部署成功,连接主节点执行info replication查看从节点状态,或者连接从节点查看主节点状态

redis-cli -h 127.0.0.1 -p 6379
info replication

在这里插入图片描述

三、哨兵节点部署

  1. conf文件配置:

    # 当前Sentinel服务运行的端口
    port 16379
    # 保护模式 no
    protected-mode no
    
    # Sentinel去监视一个名为mymaster的主redis实例,
    # 这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,
    # 而将这个主实例判断为失效至少需要2个Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
    
    #logfile "logs/16379.log"
    dbfilename "dump16379.rdb"
    # down-after-milliseconds指定了Sentinel认为Redis实例已经失效所需的毫秒数。
    # 当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。
    # 只有一个Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线。
    # 这时自动故障迁移才会执行
    sentinel monitor mymaster 127.0.0.1 6379 2
    sentinel auth-pass mymaster abc.123
    # parallel-syncs指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,
    # 在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
    sentinel down-after-milliseconds mymaster 5000
    
    # 如果在failover-timeout该时间(ms)内未能完成failover操作,则认为该failover失败
    sentinel failover-timeout mymaster 15000
    # Generated by CONFIG REWRITE
    dir "C:\\Program Files\\Redis"
    
    
  2. 部署并启动
    a) 部署节点

    redis-server --service-install ./conf/sentinel-16379.conf --service-name sentinel-16379 --sentinel
    redis-server --service-install ./conf/sentinel-26379.conf --service-name sentinel-26379 --sentinel
    redis-server --service-install ./conf/sentinel-36379.conf --service-name sentinel-36379 --sentinel
    

b) 启动节点
在windows服务中启动
在这里插入图片描述
c) 验证是否部署成功,随便连接其中一个哨兵节点看监控状态

redis-cli -h 127.0.0.1 -p 16379
info sentinel

在这里插入图片描述

四、Springboot结合Redis高可用

  1. application.yml配置文件

    spring: 
    	redis:
      # 地址
      host: 127.0.0.1
      database: 0 # Redis数据库索引(默认为0)
      # 端口,默认为6379
      port: 6379
      # 连接超时时间
      timeout: 10000
      jedis:
        pool:
          max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
          max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
          max-idle: 8 # 连接池中的最大空闲连接
          min-idle: 0 # 连接池中的最小空闲连接
      sentinel:
        master: mymaster
        nodes: 127.0.0.1:16379,127.0.0.1:26379,127.0.0.1:36379
      lettuce:
        pool:
          # 连接池中的最小空闲连接
          min-idle: 0
          # 连接池中的最大空闲连接
          max-idle: 8
          # 连接池的最大数据库连接数
          max-active: 8
          # #连接池最大阻塞等待时间(使用负值表示没有限制)
          max-wait: -1ms
    
  2. config文件

    package com.raspberry.framework.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.RedisNode;
    import org.springframework.data.redis.connection.RedisSentinelConfiguration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.net.UnknownHostException;
    
    @Configuration
    public class RedisSentinelConfig extends CachingConfigurerSupport {
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.timeout}")
        private int timeout;
    
        @Value("${spring.redis.database}")
        private int database;
    
    
    
        @Value("${spring.redis.sentinel.nodes}")
        private String redisNodes;
    
        @Value("${spring.redis.sentinel.master}")
        private String master;
    
        //redis哨兵配置
        @Bean
        public RedisSentinelConfiguration redisSentinelConfiguration(){
            RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
            String[] host = redisNodes.split(",");
            for(String redisHost : host){
                String[] item = redisHost.split(":");
                String ip = item[0];
                String port = item[1];
                configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
            }
            configuration.setMaster(master);
            return configuration;
        }
    
        @Bean
        public JedisPoolConfig jedisPoolConfig() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setTestOnBorrow(true);
            return jedisPoolConfig;
        }
    
    
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
            {
            RedisTemplate<Object, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(connectionFactory);
    
            GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
    
            ObjectMapper mapper = new ObjectMapper();
            mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    //        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    //        serializer.setObjectMapper(mapper);
    
            template.setValueSerializer(serializer);
            // 使用StringRedisSerializer来序列化和反序列化redis的key值
            template.setKeySerializer(new StringRedisSerializer());
            template.afterPropertiesSet();
            return template;
        }
    
    }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值