• 使用Spring-data-redis操作Redis的Sentinel


    转载:http://blog.csdn.net/gaogaoshan/article/details/41043685

    基本介绍

    本文基于Redis-2.8.10和jedis2.4.2版本。

    redis 2.8之后的版本 开始支持Sentinel监听客户端功能。使用1个或多个的Sentinel作为一个监听的集群。

    后端N个(master+slave)组成一个redis集群。

     

    Sentinel通过检测Redis的master实例是否存活,并在Redis master实例发生故障时,将Redis master的slave提升为master,

    并在老的master重新加入到redis sentinel的群集之后,会被重新配置,作为新master的slave。

    这意味着基于redis sentinel的HA群集是能够自我管理的!

    应用通过sentinel可以动态的得到当前master的连接,而不是直接在应该中配置写死的master的IP和端口。

    这样就避免了master,slave切换后应用配置的连接不可用的情况。

    安装配置

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. http://download.redis.io/releases/redis-2.8.17.tar.gz  
    2.   
    3. 1.安装redis:   
    4.   
    5. tar zxvf redis-2.8.17.tar.gz (要先安装tcl)  
    6.   
    7. cd redis-2.8.8   
    8.   
    9. (查看Liunx版本方式:getconf LONG_BIT)  
    10. 如果是32位:make CFLAGS="-march=i686"  
    11. 如果是64位:make  
    12.   
    13. 要是编译失败(make clean)  
    14.   
    15. cd src   
    16. make test  
    17. make install    
     
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. mkdir -p /opt/portal/redis/bin  
    2. mkdir -p /opt/portal/redis/conf  
    3. mkdir -p /opt/portal/redis/logs  
    4. cd /opt/portal/redis/redis-2.8.8  
    5. cp redis.conf sentinel.conf /opt/portal/redis/conf  
    6. cd /opt/portal/redis/redis-2.8.8/src  
    7. cp  redis-server redis-cli redis-sentinel redis-benchmark  mkreleasehdr.sh redis-check-aof redis-check-dump   /opt/portal/redis/bin  
    8.   
    9. 设置别名  
    10. vi /etc/profile  
    11. alias redis-server='/opt/portal/redis/bin/redis-server /opt/portal/redis/conf/redis.conf'  
    12. alias redis-cli='/opt/portal/redis/bin/redis-cli'  
    13. 使配置文件生效  
    14. . /etc/profile  



    slave 的 redis  的redis.conf需要配置master的IP和端口# slaveof <masterip> <masterport>

    Sentinel官方文档:官网:http://redis.io/topics/sentinel

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:24px;">sentinel.conf主要有6个配置项</span>  
    2.   
    3. <span style="color:#ff0000;">port 26329  
    4. sentinel monitor mymaster 192.168.14.191 6379 2</span>  
    5. sentinel down-after-milliseconds mymaster 60000  
    6. sentinel failover-timeout mymaster 180000  
    7. sentinel parallel-syncs mymaster 1  
    8. sentinel notification-script <master-name<script-path>  
    9.   
    10.   
    11. port: 指定sentinel的侦听端口(即与redis server或client建立tcp连接的端口)  
    12. monitor: 指定sentinel要monitor的redis实例,包括一个redis实例的别名(alias)及redis实例的ip+port,<span style="color:#006600;">该行最后的数字2表示至少2个setinel实例同时检测到redis server异常时,才将redis server的状态判决为real fail。也即,若这里配置为2,但实际部署中sentinel只部署了1套,则即使redis实例已经挂掉,sentinel也不会给出任何警告。这一点需要特别引起注意。</span>  
    13. down-after-milliseconds: 指定sentinel监控到redis实例持续异常多长时间后,会判决其状态为down。若实际业务需要sentinel尽快判决出redis实例异常,则可适当配小。  
    14. failover-timeout: 若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。该配置有4个用途,具体可参考sentinel.conf中的说明,限于篇幅,此处不再赘述。  
    15. parallel-syncs: 指定failover过程中,同时被sentinel reconfigure的最大slave实例数。由于reconfigure过程中,对应的slave会中断响应客户端请求,故为避免所有的slave同时不可用,该值需适当配小。  
    16. notification-script: 指定sentinel检测到master-name指向的实例异常时,调用的报警脚本。该配置项可选,但线上系统建议配置。  



    测试

    191,192上面各部署一个Sentinel监控同一个master。
    192部署master,191部署slave(redis.conf 中配置master的地址 slave of 192.168.11.190 6379
     
    启动redis,Sentinel
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. nohup ./bin/redis-sentinel ./conf/sentinel.conf> ./logs/redis-sentinel.log 2>&1&  
    2.   
    3. redis-cli info 可以查看当前redis实例的基本信息  
    4. redis-cli -p 26379  连接redis-sentinel客户端  info 查看master地址,有几个slave,有几个监控  
    5.   
    6. 动态添加需要监控的master  
    7. 192.168.14.192:26381> sentinel monitor mymaster 192.168.14.193 6379 2   
    8. 查看一个指定的master有那些slaves:  
    9. 192.168.14.192:26379> sentinel slaves mymaster  

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <span style="color:#3333ff;">192关闭master: redis-cli下shutdown </span>  
    2. 在redis-sentinel.log中  
    3.  +sdown master mymaster 192.168.14.192 6379  监控到192down了  
    4. +vote-for-leader bccb0d2d048a9a8497f87137a856add2ff57bceb 1 选举新的master  
    5. +switch-master mymaster 192.168.14.192 6379 192.168.14.191 6379  将原来的master192 换成191  
    6. +slave slave 192.168.14.192:6379 192.168.14.192 6379 @ mymaster 192.168.14.191 6379 192变成了191的slave  
    7.   
    8. redis-cli info Replication <span style="color:#3333ff;">191 变成role:master</span>  
    9.   
    10.   
    11.   
    12. <span style="color:#3333ff;">重新开启 192  
    13. redis-cli info Replication 192 role:slave</span>  
    14.   
    15. <span style="color:#3333ff;">再把191 shutdown 192 又变回master了</span>  


    Spring配置文件&代码

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">  
    2.     <property name="master">  
    3.         <bean class="org.springframework.data.redis.connection.RedisNode">  
    4.             <property name="name" value="mymaster"></property>  
    5.         </bean>  
    6.     </property>  
    7.     <property name="sentinels">  
    8.         <set>  
    9.             <bean class="org.springframework.data.redis.connection.RedisNode">  
    10.                 <constructor-arg name="host" value="192.168.14.192"></constructor-arg>  
    11.                 <constructor-arg name="port" value="26379"></constructor-arg>  
    12.             </bean>         
    13.             <bean class="org.springframework.data.redis.connection.RedisNode">  
    14.                 <constructor-arg name="host" value="192.168.14.191"></constructor-arg>  
    15.                 <constructor-arg name="port" value="26379"></constructor-arg>  
    16.             </bean>             
    17.         </set>  
    18.     </property>  
    19. </bean>  
    20.   
    21. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
    22.     <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>  
    23. </bean>  
    24.   
    25.   
    26. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
    27.     <property name="connectionFactory" ref="connectionFactory" />  
    28. </bean>  
     
    配置文件要懒加载,java-dao 要用getBean获取redisTemplate,getBean之前还要用Jedis jedis = new Jedis(ip, port);测试,不然redis连接不上,
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Sentinel会死循环的不断连接  

    sentinelConfiguration配置监听的列表
    connectionFactory:从监听中获取当前的master
    redisTemplate:到connectionFactory获取连接
     
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Long listFormRedis=redisTemplate.opsForList().size("login_record_list");  
    2. String hostName=connectionFactory.getSentinelConnection().masters().iterator().next().getHost();  
    3.   
    4. System.out.println(listFormRedis);  
    5. System.out.println(hostName);  


    master切换的时候应用的日志:
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. It seems like server has closed the connection.; nested exception   
    2. 2014-11-10 16:57:46 redis.clients.jedis.JedisSentinelPool initPool  
    3. 信息: Created JedisPool to master at 192.168.14.191:6379 
  • 相关阅读:
    Kubernetes实战:高可用集群的搭建和部署
    华为云MVP程云:知识化转型,最终要赋能一线
    支持60+数据传输链路,华为云DRS链路商用大盘点
    关于单元测试的那些事儿,Mockito 都能帮你解决
    深入原生冰山安全体系,详解华为云安全服务如何构筑全栈安全
    云小课|ModelArts Pro 视觉套件:零代码构建视觉AI应用
    FLINK重点原理与机制:内存(1)task之间的数据传输
    FLINK重点原理与机制:内存(2)网络流控及反压机制剖析(一)
    FLINK重点原理与机制:状态(3)两阶段提交
    FLINK重点原理与机制:状态(2)Flink的检查点算法CHECKPOINT
  • 原文地址:https://www.cnblogs.com/ruiati/p/6386670.html
Copyright © 2020-2023  润新知