spring-boot-data-redis,使用redisson作为redis客户端

spring-boot-data-redis,使用Redisson作为redis客户端

spring-boot-data-redisredis 客户端又进行了一系列的封装,抽象出了一层接口。在使用的时候可以灵活的切换 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;