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、監(jiān)聽的主機名: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依賴

工程結(jié)構(gòu)
如下:

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配置文件
配置文件添加內(nèi)容如下:
######################################################## ###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 哨兵監(jiān)聽的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
* 創(chuàng)建時間: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);
}
}測試結(jié)果
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目錄
主節(jié)點配置
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"
從節(jié)點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
從節(jié)點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 關(guān)閉保護模式,以便其他機器的能連接redis
daemonize后臺模式啟動
redis-v5版本使用replicaof替換舊的slaveof指令。
啟動這3個節(jié)點,在/soft/redis目錄下運行
redis-server config/redis-6379.conf redis-server config/redis-6380.conf redis-server config/redis-6381.conf
打開主節(jié)點客戶端看看配置是否成功
redis-cli -p 6379 info replication

再配置3個哨兵,監(jiān)控集群
哨兵節(jié)點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
哨兵節(jié)點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
哨兵節(jié)點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?
mymaster是master的名稱,192.168.4.176是master主機ip。后面的2表示有2個sentinel認為master下線了,則線下master,建議設置為 sentinel節(jié)點數(shù)/2 + 1
down-after-milliseconds?
發(fā)送ping請求給redis節(jié)點,在指定時間內(nèi)未收到回復,則認為該節(jié)點應該被下線
parallel-syncs ??
在執(zhí)行故障轉(zhuǎn)移時,最多可以有多少個從節(jié)點同時對新的主服務器進行同步。
啟動哨兵
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
觀察代碼編輯器控制臺輸出,經(jīng)過短暫的時間(大概是50s)后,程序重新運行正常
在6380和6381節(jié)點執(zhí)行info replication,發(fā)現(xiàn)6381變成了主節(jié)點

查看下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的從節(jié)點
有個比較坑爹的事情,RedisTemplate未實現(xiàn)讀寫分離,讀寫都是操作master節(jié)點。運行上面的代碼,在3個redis客戶端運行monitor發(fā)現(xiàn),只有master會運行g(shù)et、set命令,從節(jié)點只運行了set命令。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
理解 MyBatis 是如何在 Spring 容器中初始化的
這篇文章主要介紹了理解 MyBatis 是如何在 Spring 容器中初始化的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Java創(chuàng)建數(shù)組的幾種方式總結(jié)
下面小編就為大家?guī)硪黄狫ava創(chuàng)建數(shù)組的幾種方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Java中static和static?final的區(qū)別詳解
這篇文章主要介紹了Java中static和static?final的區(qū)別詳解,開發(fā)時我們經(jīng)常用到static以及static?final來修飾我們的字段變量,那么他們到底有什么區(qū)別呢?其實他們的區(qū)別可以用使用字節(jié)碼文件來解析,需要的朋友可以參考下2023-10-10

