千家信息网

spring boot如何整合redis主从sentinel方式

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,小编给大家分享一下spring boot如何整合redis主从sentinel方式,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!springboot整合redis主从sentinel
千家信息网最后更新 2026年10月31日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 26379sentinel monitor mymaster 127.0.0.1 6379 2sentinel down-after-milliseconds mymaster 5000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 15000

新建spring boot工程,并加入Redis依赖

工程结构

如下:

pom文件如下:

        4.0.0         com.chhliu.springboot.redis        springboot-redis        0.0.1-SNAPSHOT        jar         springboot-redis        Demo project for Spring Boot redis                         org.springframework.boot                spring-boot-starter-parent                1.4.3.RELEASE                                                  UTF-8                UTF-8                1.7                                                         org.springframework.boot                        spring-boot-starter-data-redis                                                         org.springframework.boot                        spring-boot-starter-test                        test                                                                                                         org.springframework.boot                                spring-boot-maven-plugin                                                

修改application.properties配置文件

配置文件添加内容如下:

###########################################################REDIS (RedisProperties) redis基本配置;######################################################### database namespring.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=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.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 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)@SpringBootTestpublic 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 6379protected-mode nodaemonize yespidfile "/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.1port 6380protected-mode nodaemonize yespidfile "/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.1port 6381protected-mode nodaemonize yespidfile "/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.confredis-server config/redis-6380.confredis-server config/redis-6381.conf

打开主节点客户端看看配置是否成功

redis-cli -p 6379info replication

再配置3个哨兵,监控集群

哨兵节点1

vim config/redis-sentinel-26379.conf

port 26379daemonize yespidfile "/var/run/redis-sentinel-26379.pid"dir /tmplogfile "log-sentinel-26379.log"sentinel monitor mymaster 192.168.4.176 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yes

哨兵节点2

vim config/redis-sentinel-26380.conf

port 26380daemonize yespidfile "/var/run/redis-sentinel-26380.pid"dir /tmplogfile "log-sentinel-26380.log"sentinel monitor mymaster 192.168.4.176 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yes

哨兵节点3

vim config/redis-sentinel-26381.conf

port 26381daemonize yespidfile "/var/run/redis-sentinel-26381.pid"dir /tmplogfile "log-sentinel-26381.log"sentinel monitor mymaster 192.168.4.176 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yes

配置说明

monitor mymaster 192.168.4.176 6379 2

mymaster是master的名称,192.168.4.176是master主机ip。后面的2表示有2个sentinel认为master下线了,则线下master,建议设置为 sentinel节点数/2 + 1

down-after-milliseconds

发送ping请求给redis节点,在指定时间内未收到回复,则认为该节点应该被下线

parallel-syncs

在执行故障转移时,最多可以有多少个从节点同时对新的主服务器进行同步。

启动哨兵

redis-sentinel config/redis-sentinel-26379.confredis-sentinel config/redis-sentinel-26380.confredis-sentinel config/redis-sentinel-26381.conf

配置spring-boot

pom.xml中导入依赖

    org.springframework.boot    spring-boot-starter-data-redis

application.properties加入两行配置

# 使用哨兵模式不能加以下两行配置,其他配置可以加# spring.redis.host=192.168.4.176# spring.redis.port=6379 spring.redis.sentinel.master=mymasterspring.redis.sentinel.nodes=192.168.4.176:26379, 192.168.4.176:26380, 192.168.4.176:26381

写一个测试类运行

@RunWith(SpringRunner.class)@SpringBootTestpublic 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 rediskill -9 11110

观察代码编辑器控制台输出,经过短暂的时间(大概是50s)后,程序重新运行正常

在6380和6381节点执行info replication,发现6381变成了主节点

查看下6380、6381的配置文件

cat config/redis-6380.confreplicaof 192.168.4.176 6381replicaof 变成了192.168.4.176 6381,而不是刚开始配置时的192.168.4.176 6379cat config/redis-6381.conf replicaof 的配置被删除了

重启下6379这个redis实例

redis-server config/redis-6379.conf

6379变成了6381的从节点

有个比较坑爹的事情,RedisTemplate未实现读写分离,读写都是操作master节点。运行上面的代码,在3个redis客户端运行monitor发现,只有master会运行get、set命令,从节点只运行了set命令。

看完了这篇文章,相信你对"spring boot如何整合redis主从sentinel方式"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

配置 节点 哨兵 运行 模式 文件 主从 整合 时间 目录 服务 测试 方式 下线 主机 代码 单机 口号 名称 命令 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技图片 新华互联网科技 壹账通互联网科技 互联网公司会议桌创意科技软装饰 互联网科技实习生岗位职责 注册互联网科技公司经营范围大全 未来五年的互联网科技 银川新华互联网科技学校联系电话 互联网金融科技展 深圳市罗太互联网科技有限公司 美国互联网科技实力 互联网领先科技宣传片 下一代互联网重大应用科技园 广州互联网高科技取餐柜 九思连强互联网电子科技有限公司 玉溪新华互联网科技怎么选 现代互联网科技公司 利用互联网等科技手段 互联网科技企业文化 互联网 科技节活动 微视频大赛 互联网科技会 科技互联网创业计划书 青海省互联网新科技 恒生互联网科技指数跳水 晋城互联网科技 互联网科技公司营业执照怎么办理 厦门江牛科技互联网 上海创道互联网络科技有限公司 互联网科技融合创新 兰石爱特工业互联网科技分公司
0