Redisson 实战

摘要

与Springboot集成

添加依赖

1
2
3
4
5
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.52.0</version>
</dependency>

PS: 目前redisson-spring-boot-starte最新版是4.1.0,其对应的是 Springboot 4.x 版本,本文基于springboot-3.5.8,所以这里用的是3.52.0这个版本。

Redisson 模块 对应 Spring Boot 对应 Spring Data Redis
redisson-spring-data-16 Spring Boot 1.3.x Spring Data Redis 1.6.x
redisson-spring-data-17 Spring Boot 1.4.x Spring Data Redis 1.7.x
redisson-spring-data-18 Spring Boot 1.5.x Spring Data Redis 1.8.x
redisson-spring-data-2x Spring Boot 2.x Spring Data Redis 2.x
redisson-spring-data-3x Spring Boot 3.x Spring Data Redis 3.x
redisson-spring-data-4x Spring Boot 4.x Spring Data Redis 4.x

配置文件

  • 配置文件application.properties 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# redis config
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.username=admin
spring.data.redis.password=123456
spring.data.redis.database=0
# 超时时间
spring.data.redis.timeout=3s
# 连接池配置
# 启用连接池
spring.data.redis.lettuce.pool.enabled=true
# 连接池最大连接数
spring.data.redis.lettuce.pool.max-active=8
# 获取连接最大等待时间
spring.data.redis.lettuce.pool.max-wait=2s
# 最大空闲连接
spring.data.redis.lettuce.pool.max-idle=8
# 最小空闲连接
spring.data.redis.lettuce.pool.min-idle=0
# 关闭超时时间
spring.data.redis.lettuce.shutdown-timeout=5s

获取RedissonClient

  • Redisson 提供了3个 Client,本质上是同一套底层连接与命令能力的不同编程模型封装,主要区别在于:

调用方式(同步 / 异步 / Reactive / RxJava)、线程模型、背压能力、适用场景。

1
2
3
4
5
6
7
// Sync and Async API
@Autowired
RedissonClient redisson;
// Reactive API
RedissonReactiveClient redissonReactive = redisson.reactive();
// RxJava3 API
RedissonRxClient redissonRx = redisson.rxJava();
  • 三个 Client 的定位概览

Client 类型 编程模型 返回值 是否阻塞 背压支持 典型用途
RedissonClient 同步 / 异步 Imperative T / RFuture 同步会阻塞 传统服务、简单业务
RedissonReactiveClient Reactive Streams Reactor / Flow Mono / Flux WebFlux、高并发
RedissonRxClient RxJava3 RxJava3 Observable / Single / Flowable RxJava 项目
  • 📊 Redisson Client 选型对照表

场景 技术架构特征 推荐 Client 编程模型 核心理由 典型应用
普通 Spring Boot(MVC) 同步请求模型
线程池可控
阻塞 IO 可接受
RedissonClient(同步 / Async) Imperative + Future - 编码简单,维护成本低
- 与 JDBC / 事务模型一致
- 易于线程与连接池治理
- 调试与排障成本低
业务系统、管理后台、内部服务
高并发 IO 服务(WebFlux / 网关) 非阻塞架构
事件驱动
高并发连接数
RedissonReactiveClient Reactive Streams(Mono / Flux) - 全链路非阻塞
- 支持背压控制
- 与 Reactor 生态天然兼容
- 最大化吞吐与资源利用率
API 网关、实时服务、推送系统
已有 RxJava 技术栈 已广泛使用 RxJava3
成熟流式管道
RedissonRxClient RxJava(Single / Flowable) - 无需引入 Reactor 生态
- 与现有流式模型无缝集成
- 支持背压(Flowable)
- 改造成本最低
Android 后端、历史系统

📊 Redis 数据类型 ↔ Redisson 对象映射表

1️⃣ String

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
String RBucket<T> 单值对象 get / set / delete GET / SET 最常用
String RAtomicLong 原子计数器 incrementAndGet INCRBY 强语义
String RAtomicDouble 原子浮点 addAndGet 自实现 Lua
String RLongAdder 高并发计数 increment Hash + Lua 分片
String RBitSet 位集合 set / get SETBIT / GETBIT 位图
String RBinaryStream 二进制流 read / write GETRANGE 大对象

2️⃣ Hash

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
Hash RMap<K,V> Map get / put / remove HGET / HSET 核心
Hash RMapCache<K,V> 带 TTL Map put(key,val,ttl) Hash + ZSet 过期控制

3️⃣ List

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
List RList<V> List add / get / remove LPUSH / LRANGE 顺序
List RQueue<V> Queue offer / poll RPUSH / LPOP 队列
List RDeque<V> Deque addFirst / addLast LPUSH / RPUSH 双端
List RBlockingQueue<V> BlockingQueue take BLPOP 阻塞消费

4️⃣ Set

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
Set RSet<V> Set add / contains SADD / SISMEMBER 无序
Set RSetCache<V> TTL Set add(ttl) Set + ZSet 自动过期
Set RSortedSet<V> SortedSet add / first ZSet 有序

5️⃣ ZSet

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
ZSet RScoredSortedSet<V> 带 score 集合 add(score) ZADD 排行榜
ZSet RLexSortedSet 字典排序集合 add ZRANGEBYLEX 字典序
ZSet RPriorityQueue<V> 优先队列 poll ZSet 堆语义
ZSet RPriorityDeque<V> 双端优先队列 pollFirst ZSet 扩展

6️⃣ Bitmap

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
Bitmap RBitSet BitSet set / get SETBIT 位图
Bitmap RLongAdder 计数位 increment Bitmap 分片 高并发

7️⃣ HyperLogLog

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
HyperLogLog RHyperLogLog<V> 去重计数 add / count PFADD / PFCOUNT UV统计

8️⃣ Geo

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
Geo RGeo<V> 地理索引 add / radius GEOADD LBS

9️⃣ Stream

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
Stream RStream<K,V> 消息流 add / read XADD / XREAD 消息队列
Stream RStreamReactive Reactive Stream subscribe XREADGROUP 流式

🔟 Pub/Sub

Redis Redisson 对象 Java 语义 常用 API 底层映射 说明
PubSub RTopic Topic publish / addListener PUBLISH 广播
PubSub RPatternTopic Pattern subscribe PSUBSCRIBE 模式订阅