SpringBoot集成Lettuce客戶(hù)端操作Redis的實(shí)現(xiàn)
一、前言
spring-boot-starter-data-redis有兩種實(shí)現(xiàn) lettuce 和 jedis,spring boot 2的spring-boot-starter-data-redis中,默認(rèn)使用的是lettuce作為redis客戶(hù)端,也推薦使用lettuce,它與jedis的主要區(qū)別如下
- Jedis
- Jedis是同步的,不支持異步,Jedis客戶(hù)端實(shí)例不是線(xiàn)程安全的,需要每個(gè)線(xiàn)程一個(gè)Jedis實(shí)例,所以一般通過(guò)連接池來(lái)使用Jedis.
- 優(yōu)點(diǎn)
- 提供了比較全面的 Redis 操作特性的 API
- API 基本與 Redis 的指令一一對(duì)應(yīng),使用簡(jiǎn)單易理解
- 缺點(diǎn)
- 同步阻塞 IO
- 不支持異步
- 線(xiàn)程不安全
- Lettuce
- Lettuce是基于Netty框架的事件驅(qū)動(dòng)的Redis客戶(hù)端,其方法調(diào)用是異步的,Lettuce的API也是線(xiàn)程安全的,所以多個(gè)線(xiàn)程可以操作單個(gè)Lettuce連接來(lái)完成各種操作,同時(shí)Lettuce也支持連接池.
- 優(yōu)點(diǎn)
- 線(xiàn)程安全
- 基于 Netty 框架的事件驅(qū)動(dòng)的通信,支持異步和響應(yīng)式編程
- 適用于分布式緩存
- 支持集群,Sentinel,管道和編碼器等等功能
- 缺點(diǎn)
- API 更抽象,學(xué)習(xí)使用成本高,不過(guò)我們基本都使用的RedisTemplate來(lái)操作Redis,它抽象了Jedis或者Lettuce客戶(hù)端,底層實(shí)現(xiàn)我們可以不用關(guān)心
二、基礎(chǔ)集成配置(redis單節(jié)點(diǎn))
工程結(jié)構(gòu)

2.1、POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot中的redis依賴(lài)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce pool 緩存連接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- 使用jackson作為redis數(shù)據(jù)序列化 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
<!-- SpringBoot測(cè)試包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2、添加配置文件application.yml
因?yàn)槲覀冇玫膕pring-boot-starter-data-redis包會(huì)自動(dòng)配置redis連接,在配置文件中添加對(duì)應(yīng)配置即可
spring:
#redis配置信息
redis:
## Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0)
database: 0
## Redis服務(wù)器地址
host: 172.16.8.169
## Redis服務(wù)器連接端口
port: 6379
## Redis服務(wù)器連接密碼(默認(rèn)為空)
password: 123456
## 連接超時(shí)時(shí)間(毫秒)
timeout: 1200
lettuce:
pool:
## 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
max-active: 8
## 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
max-wait: -1
## 連接池中的最大空閑連接
max-idle: 8
## 連接池中的最小空閑連接
min-idle: 1
2.3、編寫(xiě)配置文件
這里需要添加一個(gè)RedisTemplate的bean,設(shè)置這個(gè)RedisTemplate序列化方式為jackson
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig{
/**
* retemplate相關(guān)配置
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠(chǎng)
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer來(lái)序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化輸入的類(lèi)型,類(lèi)必須是非final修飾的,final修飾的類(lèi),比如String,Integer等會(huì)跑出異常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer來(lái)序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 設(shè)置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
}
2.4、編寫(xiě)啟動(dòng)類(lèi)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LettuceApplication {
public static void main(String[] args) {
SpringApplication.run(LettuceApplication.class);
}
}
2.5、編寫(xiě)測(cè)試類(lèi)測(cè)試是否連接成功
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.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = LettuceApplication.class)
public class LettuceTest {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void t1(){
String key = "key1";
System.out.println("插入數(shù)據(jù)到redis");
redisTemplate.opsForValue().set(key,"value1");
Object value = redisTemplate.opsForValue().get(key);
System.out.println("從redis中獲取到值為 "+value);
Boolean delete = redisTemplate.delete(key);
System.out.println("刪除redis中值 "+delete);
}
}
到此這篇關(guān)于SpringBoot集成Lettuce客戶(hù)端操作Redis的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Lettuce操作Redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot靜態(tài)資源映射規(guī)則的使用小結(jié)
本文主要介紹了springboot靜態(tài)資源映射規(guī)則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
JavaEE在線(xiàn)人數(shù)管理系統(tǒng)
這篇文章主要為大家分享了JavaEE在線(xiàn)人數(shù)管理系統(tǒng),顯示在線(xiàn)人數(shù)、在線(xiàn)人詳細(xì)信息、管理員踢人等功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
java 對(duì)象參數(shù)去空格方式代碼實(shí)例
這篇文章主要介紹了java 對(duì)象參數(shù)去空格方式代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
SpringBoot 創(chuàng)建對(duì)象常見(jiàn)的幾種方式小結(jié)
Spring Boot中創(chuàng)建對(duì)象的幾種常見(jiàn)方式包括使用@Component、@Service、@Repository或@Controller注解,本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下2024-11-11
Java Socket實(shí)現(xiàn)猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了Java Socket實(shí)現(xiàn)猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦
這篇文章主要介紹了SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

