spring boot如何整合redis主从sentinel方式
数据库不知道大家是否熟悉?今天我将给大家介绍《spring boot如何整合redis主从sentinel方式》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
springboot整合redis主从sentinel
一主二从三sentinel配置
1、master:127.0.0.1:6379
2、slave1:127.0.0.1:6380
3、slave2:127.0.0.1:6381
4、sentinel1:127.0.0.1:26379
5、sentinel2:127.0.0.1:26479
6、sentinel3:127.0.0.1:26579
7、监听的主机名:mymaster
8、附上sentinel1的配置
port 26379 sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 15000
新建spring boot工程,并加入Redis依赖
工程结构
如下:
pom文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chhliu.springboot.redis</groupId> <artifactId>springboot-redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-redis</name> <description>Demo project for Spring Boot redis</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
修改application.properties配置文件
配置文件添加内容如下:
######################################################## ###REDIS (RedisProperties) redis基本配置; ######################################################## # database name spring.redis.database=0 # server host1 单机使用,对应服务器ip #spring.redis.host=127.0.0.1 # server password 密码,如果没有设置可不配 #spring.redis.password= #connection port 单机使用,对应端口号 #spring.redis.port=6379 # pool settings ...池配置 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 # name of Redis server 哨兵监听的Redis server的名称 spring.redis.sentinel.master=mymaster # comma-separated list of host:port pairs 哨兵的配置列表 spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26479,127.0.0.1:26579
新建Redis服务
package com.chhliu.springboot.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; @Service("redisService") public class RedisService { @Autowired //操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集 private StringRedisTemplate stringRedisTemplate; @Autowired // RedisTemplate,可以进行所有的操作 private RedisTemplate<Object,Object> redisTemplate; public void set(String key, String value){ stringRedisTemplate.opsForValue().set(key, value); } public void set(Student s){ redisTemplate.opsForValue().set(s.getId(), s); } public String get(String key){ return stringRedisTemplate.opsForValue().get(key); } public Student getStudent(String key){ return (Student) redisTemplate.opsForValue().get(key); } }
依赖的vo如下:
package com.chhliu.springboot.redis; import java.io.Serializable; public class Student implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String id; private String name; private String age; private String grade; // 省略getter,setter /** * attention: * Details:TODO * @author chhliu * 创建时间:2017-1-18 下午2:24:39 * @return */ @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]"; } }
测试类
package com.chhliu.springboot.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootRedisApplicationTests { @Autowired private RedisService service; @Test public void contextLoads() { service.set("myname", "chhliu"); Student s = new Student(); s.setId("001"); s.setName("chhliu"); s.setGrade("一年级"); s.setAge("28"); service.set(s); String name = service.get("myname"); System.out.println("name:"+name); Student stu = service.getStudent("001"); System.out.println(stu); } }
测试结果
name:chhliu
Student [id=001, name=chhliu, age=28, grade=一年级]
redis哨兵模式sentinel与springboot集成
Redis的哨兵模式是官方提供的一种高可用解决方案,而且配置非常简单。
安装Redis集群
本文使用redis-5.0.5,redis安装在/soft/redis目录下,需新建/soft/redis/data目录
主节点配置
vim config/redis-6379.conf
# bind 127.0.0.1 port 6379 protected-mode no daemonize yes pidfile "/var/run/redis_6379.pid" dir "/soft/redis/data" dbfilename "dump-6379.rdb" logfile "log-6379.log"
从节点1配置
vim config/redis-6380.conf
# bind 127.0.0.1 port 6380 protected-mode no daemonize yes pidfile "/var/run/redis_6380.pid" dir "/soft/redis/data" dbfilename "dump-6380.rdb" logfile "log-6380.log" replicaof 192.168.4.176 6379
从节点2配置
vim config/redis-6381.conf
# bind 127.0.0.1 port 6381 protected-mode no daemonize yes pidfile "/var/run/redis_6381.pid" dir "/soft/redis/data" dbfilename "dump-6381.rdb" logfile "log-6381.log" replicaof 192.168.4.176 6379
配置说明
# bind 127.0.0.1 注释掉这配置,以便其他机器的能连接redis
protected-mode no 关闭保护模式,以便其他机器的能连接redis
daemonize后台模式启动
redis-v5版本使用replicaof替换旧的slaveof指令。
启动这3个节点,在/soft/redis目录下运行
redis-server config/redis-6379.conf redis-server config/redis-6380.conf redis-server config/redis-6381.conf
打开主节点客户端看看配置是否成功
redis-cli -p 6379 info replication
再配置3个哨兵,监控集群
哨兵节点1
vim config/redis-sentinel-26379.conf
port 26379 daemonize yes pidfile "/var/run/redis-sentinel-26379.pid" dir /tmp logfile "log-sentinel-26379.log" sentinel monitor mymaster 192.168.4.176 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
哨兵节点2
vim config/redis-sentinel-26380.conf
port 26380 daemonize yes pidfile "/var/run/redis-sentinel-26380.pid" dir /tmp logfile "log-sentinel-26380.log" sentinel monitor mymaster 192.168.4.176 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
哨兵节点3
vim config/redis-sentinel-26381.conf
port 26381 daemonize yes pidfile "/var/run/redis-sentinel-26381.pid" dir /tmp logfile "log-sentinel-26381.log" sentinel monitor mymaster 192.168.4.176 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
配置说明
monitor mymaster 192.168.4.176 6379 2
master的名称为mymaster,其主机IP地址为192.168.4.176。后面的2表示有2个sentinel认为master下线了,则线下master,建议设置为 sentinel节点数/2 + 1
down-after-milliseconds
发送ping请求给redis节点,在指定时间内未收到回复,则认为该节点应该被下线
parallel-syncs
在执行故障转移时,最多可以有多少个从节点同时对新的主服务器进行同步。
启动哨兵
redis-sentinel config/redis-sentinel-26379.conf redis-sentinel config/redis-sentinel-26380.conf redis-sentinel config/redis-sentinel-26381.conf
配置spring-boot
pom.xml中导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
application.properties加入两行配置
# 使用哨兵模式不能加以下两行配置,其他配置可以加 # spring.redis.host=192.168.4.176 # spring.redis.port=6379 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=192.168.4.176:26379, 192.168.4.176:26380, 192.168.4.176:26381
写一个测试类运行
@RunWith(SpringRunner.class) @SpringBootTest public class Sentinel001 { @Autowired RedisTemplate redisTemplate; @Test public void test001() throws Exception{ while (true){ String key = "time:" + new Date().getTime(); redisTemplate.opsForValue().set(key, new Date().getTime()); TimeUnit.MILLISECONDS.sleep(100L); System.out.println(redisTemplate.opsForValue().get(key)); } } }
然后杀掉master实例(端口号为6379的redis)的进程
ps -ef|grep redis kill -9 11110
观察代码编辑器控制台输出,经过短暂的时间(大概是50s)后,程序重新运行正常
在6380和6381节点执行info replication,发现6381变成了主节点
查看下6380、6381的配置文件
cat config/redis-6380.conf replicaof 192.168.4.176 6381 replicaof 变成了192.168.4.176 6381,而不是刚开始配置时的192.168.4.176 6379 cat config/redis-6381.conf replicaof 的配置被删除了
重启下6379这个redis实例
redis-server config/redis-6379.conf
6379变成了6381的从节点
RedisTemplate并未实现读写分离,因此读写操作都会在主节点上执行,这有点让人不爽。运行上面的代码,在3个redis客户端运行monitor发现,只有master会运行get、set命令,从节点只运行了set命令。
今天关于《spring boot如何整合redis主从sentinel方式》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于redis,Spring,boot的内容请关注golang学习网公众号!

- 上一篇
- 一口气看完《哈利波特》:AI大模型「量子速读」,一分钟抵人类五小时

- 下一篇
- 怎么在MySQL中设置时间
-
- 数据库 · Redis | 2小时前 |
- Redis安全配置更新操作教程
- 146浏览 收藏
-
- 数据库 · Redis | 4小时前 |
- 监控Redis集群健康的必备工具与指标
- 324浏览 收藏
-
- 数据库 · Redis | 9小时前 |
- Redis带宽瓶颈检测与优化方法
- 337浏览 收藏
-
- 数据库 · Redis | 17小时前 |
- Redis漏洞扫描与修复方法大全
- 371浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis与Kafka消息集成实战解析
- 443浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis哈希技巧与实战应用
- 326浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis事务实现的4步关键流程
- 313浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis哈希高效使用技巧全解析
- 121浏览 收藏
-
- 数据库 · Redis | 2天前 |
- Redis与MySQL缓存同步方法解析
- 189浏览 收藏
-
- 数据库 · Redis | 2天前 |
- Redis哈希技巧与实战应用
- 105浏览 收藏
-
- 数据库 · Redis | 2天前 |
- Redis内存过高怎么优化?
- 200浏览 收藏
-
- 数据库 · Redis | 2天前 |
- Redis强密码与访问控制设置指南
- 433浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 85次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 77次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 88次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 87次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 83次使用
-
- redis复制有可能碰到的问题汇总
- 2023-01-01 501浏览
-
- 使用lua+redis解决发多张券的并发问题
- 2023-01-27 501浏览
-
- Redis应用实例分享:社交媒体平台设计
- 2023-06-21 501浏览
-
- 使用Python和Redis构建日志分析系统:如何实时监控系统运行状况
- 2023-08-08 501浏览
-
- 如何利用Redis和Python实现消息队列功能
- 2023-08-16 501浏览