• Redis -- 哨兵


     哨兵

     

     

     

    搭建哨兵集群

     1.创建哨兵文件夹

    mkdir s1 s2 s3

     2.配置哨兵配置文件 sentinel.conf

    1. port 27001
    2. sentinel announce-ip 192.168.99.100
    3. sentinel monitor mymaster 192.168.99.100 7001 2
    4. sentinel down-after-millseconds mymaster 5000
    5. sentinel failover-timeout mymaster 60000
    6. dir "/tmp/s1"

    解读:

            port 27001 : 是当前sentinel 实例的端口

             sentinel monitor mymaster 192.168.99.100 7001 2 : 指定主节点信息

                    mymaster : 主节点名称,自定义,随便写

                    192.168.99.100 7001:主节点的ip和端口

                     2 :选举master时的quorum值

    拷贝sentinel.conf到s2,s3目录

     修改s2,s3的信息(端口和目录)

     启动哨兵

    1. redis-sentinel s1/sentinel.conf
    2. redis-sentinel s2/sentinel.conf
    3. redis-sentinel s3/sentinel.conf

     

     测试

            尝试让master节点7001宕机,查看sentinel日志:

     

     7002的日志

     7003的日志

    7001重启启动后的日志:

     代码

    1.引入依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. dependency>

    2.配置sentinel相关信息

    1. spring:
    2. redis:
    3. sentinel:
    4. master: mymaster #执行master名称
    5. nodes: #指定redis-sentinel集群信息
    6. - 192.168.99.100:27001
    7. - 192.168.99.100:27002
    8. - 192.168.99.100:27003

    在有哨兵模式,项目中不需要配置Redis的地址,因为Redis节点信息会动态改变. 连接Redis的事情都交给sentinel哨兵

    3.配置主从读写分离

     

    1. @Configuration
    2. public class SentinalConfig {
    3. @Bean
    4. public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
    5. return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    6. }
    7. }
    1. @Autowired
    2. private StringRedisTemplate stringRedisTemplate;
    3. @Test
    4. void testString() {
    5. //写操作将交给主节点
    6. stringRedisTemplate.opsForValue().set("name", "李四");
    7. //读操作交给从节点
    8. String value = stringRedisTemplate.opsForValue().get("name");
    9. System.out.println(value);
    10. }

  • 相关阅读:
    HJ3 随机数
    文字太多时给文本框添加滑动条——text + ContentSizeFitter + Scroll View
    “PairElimination“ app Tech Support(URL)
    理工ubuntu20.04电脑配置记录
    hadoop 实现数据排序
    网络原理---拿捏网络层:IP协议
    如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
    实践GoF的23种设计模式:装饰者模式
    LeetCode[105]从前序与中序遍历序列构造二叉树
    分散式车辆协同:伯克利DeepDrive无人机数据集B3D
  • 原文地址:https://blog.csdn.net/qq_33753147/article/details/126530204