Springboot Redis 哨兵模式的实现示例

 更新时间:2022年01月12日 08:40:23   作者:I'm Salted Fish  
本文主要介绍了Springboot Redis 哨兵模式的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Redis配置

redis.conf配置

由于服务器资源有限,我这里通过配置不同端口,模拟Redis集群,redis-server占用6379、6380、6381端口,redis-sentinel占用26379、26380、26381端口。

首先准备3份redis配置文件,分别取名redis6379.conf,redis6380.conf,redis6381.conf

redis6379.conf,6379端口作为master,需要修改的配置如下

protected-mode no #设置为NO,其他IP才可以访问
port 6379 #端口
daemonize yes 
pidfile "/var/run/redis_6379.pid"
logfile "6379.log" #日志,若redis分布在不同服务器,不用修改
dbfilename "dump6379.rdb" #数据,若redis分布在不同服务器,不用修改
masterauth "admin123/*-" #从节点访问主节点时需要的密码
requirepass "admin123/*-" #redis密码,应用访问redis时需要

redis6380.conf,6380端口作为slave,需要设置主节点ip和port,需要修改的配置如下:

protected-mode no #设置为NO,其他IP才可以访问
port 6380 #端口
daemonize yes 
pidfile "/var/run/redis_6380.pid"
logfile "6380.log" #日志,若redis分布在不同服务器,不用修改
dbfilename "dump6380.rdb" #数据,若redis分布在不同服务器,不用修改
replicaof 192.168.1.1 6379  #标记主节点ip+端口,IP设置为自己服务器IP地址即可
masterauth "admin123/*-" #从节点访问主节点时需要的密码
requirepass "admin123/*-" #redis密码,应用访问redis时需要

备注:slaveof 192.168.1.1 6379,这里需要设置成你自己的IP,可实现Redis 复制功能

redis6381.conf,6381端口作为slave,同上,需要修改的配置如下:

protected-mode no #设置为NO,其他IP才可以访问
port 6381 #端口
daemonize yes 
pidfile "/var/run/redis_6381.pid"
logfile "6381.log" #日志,若redis分布在不同服务器,不用修改
dbfilename "dump6381.rdb" #数据,若redis分布在不同服务器,不用修改
replicaof 192.168.1.1 6379  #标记主节点ip+端口,IP设置为自己服务器IP地址即可
masterauth "admin123/*-" #从节点访问主节点时需要的密码
requirepass "admin123/*-" #redis密码,应用访问redis时需要

sentinel.conf配置

准备3分sentinel.conf配置文件,分别为sentinel26379.conf、sentinel26380.conf、sentinel26381.conf。

sentinel26379.conf,26379端口作为哨兵1,需要修改配置如下

port 26379 #端口
daemonize yes   
pidfile "/var/run/redis-sentinel26379.pid"
logfile "/usr/local/bin/sentinelLogs/sentinel_26379.log"
#配置指示 Sentinel 去监视一个名为 mymaster 的主节点, 主节点的 IP 地址为 127.0.0.1 , 端口号为 6379 ,
#而将这个主服务器判断为失效至少需要 2 个 Sentinel 同意 (只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)
sentinel monitor mymaster 192.168.1.1 6379 2 
sentinel auth-pass mymaster admin123/*-   #哨兵访问主节点密码
protected-mode no

sentinel26380.conf,26380端口作为哨兵2,需要修改配置如下

port 26380 #端口
daemonize yes   
pidfile "/var/run/redis-sentinel26380.pid"
logfile "/usr/local/bin/sentinelLogs/sentinel_26380.log"
#配置指示 Sentinel 去监视一个名为 mymaster 的主节点, 主节点的 IP 地址为 127.0.0.1 , 端口号为 6379 ,
#而将这个主服务器判断为失效至少需要 2 个 Sentinel 同意 (只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)
sentinel monitor mymaster 192.168.1.1 6379 2 
sentinel auth-pass mymaster admin123/*-   #哨兵访问主节点密码
protected-mode no

sentinel26381.conf,26381端口作为哨兵3,需要修改配置如下

port 26381 #端口
daemonize yes   
pidfile "/var/run/redis-sentinel26381.pid"
logfile "/usr/local/bin/sentinelLogs/sentinel_26381.log"
#配置指示 Sentinel 去监视一个名为 mymaster 的主节点, 主节点的 IP 地址为 127.0.0.1 , 端口号为 6379 ,
#而将这个主服务器判断为失效至少需要 2 个 Sentinel 同意 (只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)
sentinel monitor mymaster 192.168.1.1 6379 2 
sentinel auth-pass mymaster admin123/*-   #哨兵访问主节点密码
protected-mode no

启动redis-server,redis-sentinel

[root@VM-20-5-centos bin]# redis-server redis6379.conf 
[root@VM-20-5-centos bin]# redis-server redis6380.conf 
[root@VM-20-5-centos bin]# redis-server redis6381.conf
[root@VM-20-5-centos bin]# redis-sentinel sentinel_26379.conf 
[root@VM-20-5-centos bin]# redis-sentinel sentinel_26380.conf 
[root@VM-20-5-centos bin]# redis-sentinel sentinel_26381.conf 

查看状态,如下图所示,即为启动成功

 [root@VM-20-5-centos mconfig]# redis-cli -p 26379
127.0.0.1:26379> sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.1.1"
    5) "port"
    6) "6379"
    7) "runid"
   
127.0.0.1:26379> sentinel slaves mymaster
1)  1) "name"
    2) "192.168.1.1:6381"
    3) "ip"
    4) "192.168.1.1"
    5) "port"
    6) "6381"
    7) "runid"
    8) ""
    
2)  1) "name"
    2) "192.75.218.240:6380"
    3) "ip"
    4) "192.75.218.240"
    5) "port"
    
127.0.0.1:26379> 

Springboot整合

导入包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

properties配置

spring.redis.host=192.168.1.1
spring.redis.port=6379
spring.redis.password=admin123/*-
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.password=admin123/*-
spring.redis.sentinel.nodes=192.168.1.1:26379,192.168.1.1:26380,192.168.1.1:26381

测试

代码示例

package com.mx.learnredis.controller;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
 
import java.util.Date;
 
@SpringBootTest
class UserControllerTest {
 
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
 
    @Test
    public void test() throws InterruptedException {
        redisTemplate.opsForValue().set("user", "xx");
        while (true)
        {
            Thread.sleep(2000);
            System.out.println("time "+new Date()+":" +redisTemplate.opsForValue().get("user"));
        }
    }
}

模拟redis宕机

idea控制台输出:

time Sat Jan 08 20:09:12 CST 2022:xx
time Sat Jan 08 20:09:14 CST 2022:xx
time Sat Jan 08 20:09:16 CST 2022:xx
time Sat Jan 08 20:09:18 CST 2022:xx
time Sat Jan 08 20:09:20 CST 2022:xx
time Sat Jan 08 20:09:22 CST 2022:xx

关闭掉主节点,登录linux服务器

[root@VM-20-5-centos mconfig]# ps -ef|grep redis
root     27881     1  0 17:19 ?        00:00:12 redis-server *:6380
root     27997     1  0 17:20 ?        00:00:17 redis-sentinel *:26379 [sentinel]
root     28017     1  0 17:20 ?        00:00:17 redis-sentinel *:26380 [sentinel]
root     28030     1  0 17:20 ?        00:00:17 redis-sentinel *:26381 [sentinel]
root     28471     1  0 20:13 ?        00:00:00 redis-server *:6381
root     28664     1  0 17:23 ?        00:00:12 redis-server *:6379
root     28753 11813  0 20:14 pts/1    00:00:00 grep --color=auto redis
[root@VM-20-5-centos mconfig]# kill -9 28664
[root@VM-20-5-centos mconfig]#

 idea控制台输出,可看到重连日志,Success

time Sat Jan 08 20:15:02 CST 2022:xx
time Sat Jan 08 20:15:04 CST 2022:xx
2022-01-08 20:15:05.010  INFO 7216 --- [xecutorLoop-1-3] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was /192.168.1.1:6379
2022-01-08 20:15:07.211  WARN 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:11.920  INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:14.081  WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:18.617  INFO 7216 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:20.767  WARN 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:26.119  INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:28.278  WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:33.720  INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:35.880  WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:40.020  INFO 7216 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:40.160  INFO 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ReconnectionHandler    : Reconnected to 192.168.1.1:6381
time Sat Jan 08 20:15:06 CST 2022:xx

到此这篇关于Springboot Redis 哨兵模式的实现示例的文章就介绍到这了,更多相关Springboot Redis 哨兵模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解java 客户端链接不上redis解决方案

    详解java 客户端链接不上redis解决方案

    这篇文章主要介绍了详解java 客户端链接不上redis解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • 关于Gateway网关中配置跨域的三种方案

    关于Gateway网关中配置跨域的三种方案

    文章总结:介绍了三种处理跨域请求的方法:在Controller类上添加注解、通过配置类实现重写WebMvcConfigurer接口和在配置文件中统一设置,希望这些方法能帮助读者解决跨域问题
    2024-11-11
  • 详解Spring Boot 使用slf4j+logback记录日志配置

    详解Spring Boot 使用slf4j+logback记录日志配置

    本篇文章主要介绍了Spring Boot 使用slf4j+logback记录日志配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Java并发编程中的volatile关键字详解

    Java并发编程中的volatile关键字详解

    这篇文章主要介绍了Java并发编程中的volatile关键字详解,volatile 用于保证我们某个变量的可见性,使其一直存放在主存中,不被移动到某个线程的私有工作内存中,需要的朋友可以参考下
    2023-08-08
  • Java实现平铺列表(List)互转树形(Tree)结构

    Java实现平铺列表(List)互转树形(Tree)结构

    本文主要介绍了Java实现平铺列表(List)互转树形(Tree)结构,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 在SpringBoot 中从application.yml中获取自定义常量方式

    在SpringBoot 中从application.yml中获取自定义常量方式

    这篇文章主要介绍了在SpringBoot 中从application.yml中获取自定义常量方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Springquartz的配置方式详解

    Springquartz的配置方式详解

    本文介绍了在Spring框架中使用Quartz进行任务调度的三种方式:使用@Scheduled注解、XML配置和Java配置,每种方式都有其特点和适用场景,感兴趣的朋友一起看看吧
    2025-01-01
  • SpringBoot 监听Redis键过期事件(过期监听)

    SpringBoot 监听Redis键过期事件(过期监听)

    Redis键过期事件是SpringBoot中常用的缓存失效通知方式,通过配置可以监听特定键的过期事件,具有一定的参考价值,感兴趣的可以了解一下
    2024-12-12
  • Java编写超时工具类实例讲解

    Java编写超时工具类实例讲解

    在本篇内容里小编给大家分享的是一篇关于Java编写超时工具类实例讲解内容,有兴趣的朋友们可以学习参考下。
    2021-02-02
  • 解决Aop @AfterReturning因返回类型不一致导致无法执行切面代码

    解决Aop @AfterReturning因返回类型不一致导致无法执行切面代码

    这篇文章主要介绍了解决Aop @AfterReturning因返回类型不一致导致无法执行切面代码问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07

最新评论