SpringBoot連接Redis2種模式解析
一、引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、配置YML文件(二選一)
1.sentinel模式
server: port: 80 spring: redis: sentinel: nodes: 192.168.0.106:26379,192.168.0.106:26380,192.168.0.106:26381 //哨兵的ip和端口 master: mymaster //這個(gè)就是哨兵配置文件中 sentinel monitor mymaster 192.168.0.103 6379 2 配置的mymaster
2.Cluster模式
server: port: 80 spring: redis: cluster: nodes: 192.168.0.106:7000,192.168.0.106:7001,192.168.0.106:7002,192.168.0.106:7003,192.168.0.106:7004,192.168.0.106:7005
三、配置RedisTemplate模版
個(gè)人認(rèn)為
setKeySerializer
setValueSerializer
不設(shè)置也可以,不過在使用的時(shí)候,需要自行將key\value 轉(zhuǎn)換為json字符串后存入
@Configuration
public class RedisConf {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
Jackson2JsonRedisSerializer serializer=new Jackson2JsonRedisSerializer(Object.class);
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(serializer); //設(shè)置key序列化
template.setValueSerializer(serializer);//設(shè)置value序列化
return template; } }
四、測(cè)試(簡單的model就省略了)
@RestController
public class RedisTestController {
@Autowired
RedisTemplate redisTemplate;
@GetMapping("set")
public void set(){
redisTemplate.opsForValue().set("key1","123");
User u=new User();
u.setId(1);
u.setName("name姓名");
redisTemplate.opsForValue().set("user",u);
}
@GetMapping("get")
public Map get(){
Map map=new HashMap();
map.put("v1",redisTemplate.opsForValue().get("key1"));
map.put("v2",redisTemplate.opsForValue().get("user"));
return map;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于weblogic部署Java項(xiàng)目的包沖突問題的解決
這篇文章主要介紹了關(guān)于weblogic部署Java項(xiàng)目的包沖突問題的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Java Calendar日歷與Date日期的相互轉(zhuǎn)換詳解
這篇文章主要介紹了Java Calendar日歷與Date日期的相互轉(zhuǎn)換詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
從內(nèi)存地址解析Java的static關(guān)鍵字的作用
這篇文章主要介紹了從內(nèi)存地址解析Java的static關(guān)鍵字的作用,包括靜態(tài)成員變量和靜態(tài)方法等重要內(nèi)容,需要的朋友可以參考下2015-10-10
SpringBoot?自定義注解實(shí)現(xiàn)涉密字段脫敏
關(guān)于數(shù)據(jù)脫敏,網(wǎng)上的文章都是硬編碼規(guī)則,比如對(duì)身份證,手機(jī)號(hào),郵件地址等固定寫法脫敏。本文在此基礎(chǔ)上,拓展動(dòng)態(tài)從數(shù)據(jù)庫查出涉密關(guān)鍵字執(zhí)行脫敏操作。感興趣的同學(xué)可以參考閱讀2023-03-03
SpringBoot實(shí)現(xiàn)點(diǎn)餐系統(tǒng)的登錄與退出功能流程詳解
結(jié)束了Springboot+MyBatisPlus也是開始了項(xiàng)目之旅,將從后端的角度出發(fā)來整理這個(gè)項(xiàng)目中重點(diǎn)業(yè)務(wù)功能的梳理與實(shí)現(xiàn)2022-10-10
IDEA設(shè)置Maven自動(dòng)編譯model的實(shí)現(xiàn)方法
本文主要介紹了IDEA設(shè)置Maven自動(dòng)編譯model的實(shí)現(xiàn)方法, maven結(jié)構(gòu)的項(xiàng)目,我們?cè)诿看涡薷拇a后都會(huì)需要手動(dòng)編譯,本文就可以解決這個(gè)問題,感興趣的可以了解一下2023-08-08
大廠禁止SpringBoot在項(xiàng)目使用Tomcat容器原理解析
這篇文章主要為大家介紹了大廠禁止SpringBoot在項(xiàng)目使用Tomcat原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
java8 LocalDate LocalDateTime等時(shí)間類用法實(shí)例分析
這篇文章主要介紹了java8 LocalDate LocalDateTime等時(shí)間類用法,結(jié)合具體實(shí)例形式分析了LocalDate、LocalTime、LocalDateTime等日期時(shí)間相關(guān)類的功能與具體使用技巧,需要的朋友可以參考下2017-04-04
SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決
這篇文章主要介紹了SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

