日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson

發(fā)布時(shí)間:2025/3/21 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 官網(wǎng)
  • Jedis VS Lettuce
  • Jedis Code
    • POM依賴
    • 配置文件
    • 配置類
    • 單元測(cè)試
  • Lettuce Code
  • Redisson Code
    • POM依賴
    • 配置文件
    • 配置類
    • 單元測(cè)試

官網(wǎng)

https://spring.io/projects/spring-data-redis

我們知道常用的Redis客戶端 https://redis.io/clients#java

怎么還有 Spring Data Redis ?

莫慌,小兄弟, 來看個(gè)關(guān)系圖 幫你捋一捋


Jedis VS Lettuce

在 spring-boot-starter-data-redis 項(xiàng)目 2.X 版本中 ,默認(rèn)使用 Lettuce 作為 Java Redis 工具庫 , 為啥不用jedis 了?

Why is Lettuce the default Redis client used in Spring Session Redis? #789

也有可能跟jedis 有一段時(shí)間不更新了有關(guān)系~


Jedis Code

POM依賴

<dependencies><!-- 實(shí)現(xiàn)對(duì) Spring Data Redis 的自動(dòng)化配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><!-- 去掉 Lettuce 的依賴, Spring Boot 優(yōu)先使用 Lettuce 作為 Redis 客戶端 --><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions></dependency><!-- Jedis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><!-- 單元測(cè)試 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 使用 fastjson 作為 JSON 序列化的工具 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.61</version></dependency><!-- Spring Data Redis 默認(rèn)使用 Jackson 作為 JSON 序列化的工具 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies>

配置文件

spring:# 對(duì)應(yīng) RedisProperties 類redis:host: 127.0.0.1port: 6379password: # Redis 服務(wù)器密碼,默認(rèn)為空。生產(chǎn)中,一定要設(shè)置 Redis 密碼!database: 0 # Redis 數(shù)據(jù)庫號(hào),默認(rèn)為 0 。timeout: 0 # Redis 連接超時(shí)時(shí)間,單位:毫秒。# 對(duì)應(yīng) RedisProperties.Jedis 內(nèi)部類jedis:pool:max-active: 8 # 連接池最大連接數(shù),默認(rèn)為 8 。使用負(fù)數(shù)表示沒有限制。max-idle: 8 # 默認(rèn)連接數(shù)最小空閑的連接數(shù),默認(rèn)為 8 。使用負(fù)數(shù)表示沒有限制。min-idle: 0 # 默認(rèn)連接池最小空閑的連接數(shù),默認(rèn)為 0 。允許設(shè)置 0 和 正數(shù)。max-wait: -1 # 連接池最大阻塞等待時(shí)間,單位:毫秒。默認(rèn)為 -1 ,表示不限制。

配置類

package com.artisan.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/2/16 21:07* @mark: show me the code , change the world*/@Configuration public class RedisConfiguration {@Autowiredprivate RedisConnectionFactory connectionFactory;public RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String,Object> template = new RedisTemplate();template.setConnectionFactory(connectionFactory);// 序列化工具Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();template.setKeySerializer(stringRedisSerializer);template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;} }

單元測(cè)試

package com.artisan;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.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/2/16 20:56* @mark: show me the code , change the world*/ @RunWith(SpringRunner.class) @SpringBootTest public class JedisTest {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void testStringSetKey() {// setstringRedisTemplate.opsForValue().set("artisan", "good1");// getString artisan = stringRedisTemplate.opsForValue().get("artisan");System.out.println(artisan);}}


Lettuce Code

2.x 以上默認(rèn) 使用的客戶端為Lettuce , 參考 Spring Session - 使用Spring Session從零到一構(gòu)建分布式session

這里就不贅述了。


Redisson Code

POM依賴

<dependencies><!-- Redisson --><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.11.3</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.61</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies>

配置文件

【application.yml】

spring:# 對(duì)應(yīng) RedisProperties 類redis:host: 127.0.0.1port: 6379 # password: # Redis 服務(wù)器密碼,默認(rèn)為空。生產(chǎn)中,一定要設(shè)置 Redis 密碼!database: 0 # Redis 數(shù)據(jù)庫號(hào),默認(rèn)為 0 。timeout: 0 # Redis 連接超時(shí)時(shí)間,單位:毫秒。# 對(duì)應(yīng) RedissonProperties 類 如果為空 需要注釋掉 # redisson: # config: classpath:redisson.yml # 具體的每個(gè)配置項(xiàng),見 org.redisson.config.Config 類。

使用 Spring Boot 整合 Redisson 時(shí)候,通過該配置項(xiàng),引入一個(gè)外部的 Redisson 相關(guān)的配置文件 ,引入了 classpath:redisson.yaml 配置文件

引入的 redisson.config 對(duì)應(yīng)的配置文件,對(duì)應(yīng)的類是 org.redisson.config.Config 類。因?yàn)槭纠?#xff0c;我們使用的比較簡(jiǎn)單,所以就沒有做任何 Redisson 相關(guān)的自定義配置。 如果沒有配置任何內(nèi)容,需要在 application.yml 里注釋掉 redisson.config 。

具體配置信息可參考 Spring Boot2.x 整合lettuce redis 和 redisson


配置類

同上

package com.artisan.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/2/16 21:07* @mark: show me the code , change the world*/@Configuration public class RedisConfiguration {@Autowiredprivate RedisConnectionFactory connectionFactory;public RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String,Object> template = new RedisTemplate();template.setConnectionFactory(connectionFactory);// 序列化工具Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();template.setKeySerializer(stringRedisSerializer);template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;} }

單元測(cè)試

@Testpublic void test() {stringRedisTemplate.opsForValue().set("uuu", "xxxxx");}

總結(jié)

以上是生活随笔為你收集整理的Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。