spring-boot-data-redis,使用redisson作為redis客戶端


spring-boot-data-redis,使用Redisson作為redis客戶端

spring-boot-data-redis 對 redis 客戶端又進行了一系列的封裝,抽象出了一層接口。在使用的時候可以靈活的切換 redis 客戶端的實現。

常用的客戶端

Jedis

 

 Lettuce (spring-boot-data-redis 默認使用)

 

 Redisson

 

 

Redisson

很強大,它提供的功能遠遠超出了一個Redis客戶端的范疇,它基於Redis實現了各種分布式環境下的常用功能。使用它來替換spring-boot-data-redis 默認使用的 Lettuce。在可以使用基本Redis功能的同時,也能使用它提供的一些服務。

  • 遠程調用
  • 分布式鎖
  • 分布式的對象、容器
  • MQ

Maven依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.redisson/redisson-spring-boot-starter -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.12.0</version>
</dependency>

yaml配置

# 公共的spring配置

spring.redis.database=
spring.redis.host=
spring.redis.port=
spring.redis.password=
spring.redis.ssl=
spring.redis.timeout=
spring.redis.cluster.nodes=
spring.redis.sentinel.master=
spring.redis.sentinel.nodes=

# Redisson 的特殊配置

# 可以從本地的類路徑下讀取配置文件
spring.redis.redisson.config=classpath:redisson.yaml

在工程中使用

redisson-spring-boot-starter 實現了 spring-boot-data-redis。所以跟平時沒有區別。直接使用 springboot提供的,RedisTemplate 即可。

也可以從IOC中獲取到 RedissonClient,直接使用Redisson 提供的各種強大功能。

@Autowired
RedissonClient redissonClient;

 

轉載於:SpringBoot中文社區

地址:https://springboot.io/t/topic/1250

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM