springboot 集成redis哨兵主從的實現(xiàn)
一、環(huán)境
spring boot 2.3.12.RELEASE
JDK 1.8
IntelliJ IDEA開發(fā)工具
Redis哨兵主從搭建
二、POM文件
pom文件其他忽略,只展示和redis有關(guān)系統(tǒng)的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 重點:redis依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 對象池框架,redis依賴 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
三、application.yml配置
關(guān)于springboot的配置忽略
spring 約定大于配置,對于默認(rèn)的就可以不用再配置文件中體現(xiàn)
spring:
redis:
# redis庫
database: 1
# redis節(jié)點的密碼
password: jwssw
# 集群配置
sentinel:
# 集群哨兵節(jié)點配置,多個節(jié)點之間用英文逗號分割
nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
# 主節(jié)點名稱
master: mymaster
# 密碼
password: jwssw
注意如果redis的sentinel配置文件增加了requirepass(訪問秘鑰),其sentinel節(jié)點下必須加上【password】,否則不需要添加。
四、reidsTemplate配置
該配置文件可以直接加載啟動類中,因為啟動類也是springboot的一種配置類
/**
* 方法描述: 初始化redis連接
*
* @param factory redis連接工廠
* @return {@link RedisTemplate}
*/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
// 新建redisTemplate對象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 設(shè)置工廠
template.setConnectionFactory(factory);
// 鍵值類型
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
// 返回redisTemplate對象
return template;
}
五、單元測試(JUnit4)
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
? ? // 注入redisTemplate對象
? ? @Autowired
? ? RedisTemplate<String, Object> redisTemplate;
? ? @Test
? ? public void setOrGetTest() {
? ? ? ? // redis鍵值?
? ? ? ? String redisKey = "name";
? ? ? ? // 向redis存放內(nèi)容
? ? ? ? redisTemplate.opsForValue().set(redisKey, "張三" + new Random().nextInt());
? ? ? ? // 獲取redis中的內(nèi)容并打印
? ? ? ? System.out.println(redisTemplate.opsForValue().get(redisKey));
? ? }
}到此這篇關(guān)于springboot 集成redis哨兵主從的實現(xiàn)的文章就介紹到這了,更多相關(guān)springboot redis哨兵主從內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postman測試傳入List<String>參數(shù)方式
這篇文章主要介紹了postman測試傳入List<String>參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
一文解決springboot打包成jar文件無法正常運行的問題
這篇文章主要介紹了一文解決springboot打包成jar文件無法正常運行的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
spring boot高并發(fā)下耗時操作的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于spring boot高并發(fā)下耗時操作的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java微信公眾平臺開發(fā)(9) 關(guān)鍵字回復(fù)以及客服接口實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第九步,關(guān)鍵字回復(fù)以及客服接口實現(xiàn),以及遇到該公眾號暫時無法提供服務(wù)的解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
SpringBoot+MyBatis+Redis實現(xiàn)分布式緩存
本文主要介紹了SpringBoot+MyBatis+Redis實現(xiàn)分布式緩存,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01

