SpringBoot整合Lettuce redis過(guò)程解析
首先解釋一下Lettuce客戶端:
Lettuce 和 Jedis 的都是連接Redis Server的客戶端程序。Jedis在實(shí)現(xiàn)上是直連redis server,多線程環(huán)境下非線程安全,除非使用連接池,為每個(gè)Jedis實(shí)例增加物理連接。Lettuce基于Netty的連接實(shí)例(StatefulRedisConnection),可以在多個(gè)線程間并發(fā)訪問(wèn),且線程安全,滿足多線程環(huán)境下的并發(fā)訪問(wèn),同時(shí)它是可伸縮的設(shè)計(jì),一個(gè)連接實(shí)例不夠的情況也可以按需增加連接實(shí)例。
1、添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2、添加redis配置
spring:
redis:
host: ****
password:****
port: 6379
# 連接超時(shí)時(shí)間(毫秒)
timeout: 1000
# Redis默認(rèn)情況下有16個(gè)分片,這里配置具體使用的分片,默認(rèn)是0
database: 0
# 連接池配置
lettuce:
pool:
# 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) 默認(rèn) 8
max-active: 8
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) 默認(rèn) -1
max-wait: -1
# 連接池中的最大空閑連接 默認(rèn) 8
max-idle: 8
# 連接池中的最小空閑連接 默認(rèn) 0
min-idle: 0
3、實(shí)現(xiàn)邏輯
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public String testRedis(){
ExecutorService executorService = Executors.newFixedThreadPool(1000);
IntStream.range(0, 1000).forEach(i -> executorService.execute(() -> stringRedisTemplate.opsForValue().increment("lcl",1)));
System.out.println("lcl1=============" + stringRedisTemplate.opsForValue().get("lcl"));
stringRedisTemplate.opsForValue().set("lcl1","val1");
String val1 = stringRedisTemplate.opsForValue().get("lcl1");
System.out.println("lcl1=============" + val1);
String key = "redis:test:demo1";
User user = new User();
user.setId(100L);
user.setUsername("u2");
user.setPassword("p2");
stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(user));
String valUser = stringRedisTemplate.opsForValue().get(key);
System.out.println("redis:test:demo1=============" + valUser);
User getUser = JSON.parseObject(valUser, User.class);
System.out.println("redis:test:demo1=============" + getUser.getUsername()+ "========" + getUser.getPassword());
return null;
}
測(cè)試結(jié)果:


由于redis有String、list、set、zset、hash、geo等類型,因此使用時(shí)不止使用opsForValue()方法,具體的對(duì)應(yīng)方法如下:
- opsForValue: 對(duì)應(yīng) String(字符串)
- opsForZSet: 對(duì)應(yīng) ZSet(有序集合)
- opsForHash: 對(duì)應(yīng) Hash(哈希)
- opsForList: 對(duì)應(yīng) List(列表)
- opsForSet: 對(duì)應(yīng) Set(集合)
- opsForGeo: 對(duì)應(yīng) GEO(地理位置)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn)
- 關(guān)于Springboot2.x集成lettuce連接redis集群報(bào)超時(shí)異常Command timed out after 6 second(s)
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問(wèn)題)
- SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法
- Springboot2.X集成redis集群(Lettuce)連接的方法
- 關(guān)于SpringBoot整合redis使用Lettuce客戶端超時(shí)問(wèn)題
相關(guān)文章
Java使用Calendar類實(shí)現(xiàn)動(dòng)態(tài)日歷
這篇文章主要為大家詳細(xì)介紹了Java使用Calendar類實(shí)現(xiàn)動(dòng)態(tài)日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Java并發(fā)編程中的ConcurrentLinkedQueue詳解
這篇文章主要介紹了Java并發(fā)編程中的ConcurrentLinkedQueue詳解,GetThread線程不會(huì)因?yàn)镃oncurrentLinkedQueue隊(duì)列為空而等待,而是直接返回null,所以當(dāng)實(shí)現(xiàn)隊(duì)列不空時(shí),等待時(shí),則需要用戶自己實(shí)現(xiàn)等待邏輯,需要的朋友可以參考下2023-12-12
java字符串轉(zhuǎn)JSON簡(jiǎn)單代碼示例
這篇文章主要給大家介紹了關(guān)于java字符串轉(zhuǎn)JSON的相關(guān)資料,JSON?是一種輕量級(jí)的數(shù)據(jù)交換格式,常用于Web應(yīng)用程序中的數(shù)據(jù)傳輸,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Spring Boot中使用activiti的方法教程(一)
最近一直研究springboot,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用activiti的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Java實(shí)現(xiàn)過(guò)濾掉map集合中key或value為空的值示例
這篇文章主要介紹了Java實(shí)現(xiàn)過(guò)濾掉map集合中key或value為空的值,涉及java針對(duì)map的簡(jiǎn)單遍歷、判斷、移除等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
初次使用IDEA創(chuàng)建maven項(xiàng)目的教程
這篇文章主要介紹了初次使用IDEA創(chuàng)建maven項(xiàng)目的教程講解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作
這篇文章主要介紹了解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

