Spring Boot 2.x 到 3.x 迁移实战:Redis 配置篇

打印 上一主题 下一主题

主题 1645|帖子 1645|积分 4935

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
媒介

随着 Spring Boot 3.x 的发布,其对 Java 17 的支持和 模块化架构 的深化,Redis 配置与集成方式发生了明显变化。今天简单讲下redis的变化

一、Redis 配置前缀的模块化演进:从 spring.redis 到 spring.data.redis

1.1 Spring Boot 2.x(Java 8)



  • 配置前缀:spring.redis
  • 示例
    1. spring:
    2.   redis:
    3.     host: localhost
    4.     port: 6379
    5.     password: mypassword
    6.     timeout: 2000
    7.     lettuce:
    8.       pool:
    9.         max-active: 8
    10.         max-idle: 8
    复制代码
  • 特点:直接通过 spring.redis 配置 Redis 连接参数,未明白与 Spring Data 模块绑定。
1.2 Spring Boot 3.x(Java 17)



  • 配置前缀:spring.data.redis
  • 示例
    1. spring:
    2.   data:
    3.     redis:
    4.       host: localhost
    5.       port: 6379
    6.       password: mypassword
    7.       timeout: 2000ms
    8.       client:
    9.         type: lettuce
    10.         lettuce:
    11.           pool:
    12.             max-active: 8
    复制代码
  • 模块化筹划:遵照 Spring Data Commons 的统一定名规则,与 spring.data.mongodb 等其他数据源保持同等,便于扩展和维护。

二、Redis 客户端库的颠覆性变化:Lettuce 一统天下

2.1 Spring Boot 2.x:Jedis 和 Lettuce 并存



  • 支持客户端

    • Jedis:基于壅闭式 I/O,得当简单场景。
    • Lettuce:基于 Netty 的非壅闭式,支持异步操作和响应式流(Reactive Streams)。

  • 配置示例
    1. spring:
    2.   redis:
    3.     client-type: jedis  # 或 lettuce
    4.     jedis:
    5.       pool:
    6.         max-active: 8
    7.     lettuce:
    8.       pool:
    9.         max-active: 8
    复制代码
2.2 Spring Boot 3.x:Lettuce 独占



  • 官方声明

    • Jedis 客户端被弃用,仅支持 Lettuce。
    • 缘故起因:Lettuce 的非壅闭特性更契合现代高并发场景,且与 Spring Reactor 生态深度集成。

  • 配置示例
    1. spring:
    2.   data:
    3.     redis:
    4.       client-type: lettuce
    5.       lettuce:
    6.         pool:
    7.           max-active: 8
    8.           max-wait: 1000ms
    复制代码
  • 迁移建议
    1. <!-- 移除 Jedis 相关依赖 -->
    2. <!-- 保留 Lettuce 依赖 -->
    3. <dependency>
    4.     <groupId>io.lettuce</groupId>
    5.     <artifactId>lettuce-core</artifactId>
    6. </dependency>
    复制代码

三、RedisTemplate 的序列化策略升级:从 Java 序列化到 JSON

3.1 Spring Boot 2.x



  • 默认序列化器:JdkSerializationRedisSerializer(基于 Java 序列化)。
  • 题目

    • 序列化后键值对难以直接读取(如 "\xac\xed\x00...")。
    • 跨语言兼容性差,且存在性能消耗。

3.2 Spring Boot 3.x



  • 默认序列化器:GenericJackson2JsonRedisSerializer(基于 JSON)。
  • 优势

    • 键值对以 JSON 格式存储,可读性高。
    • 跨语言兼容性好,得当微服务场景。

  • 配置示例(若需规复旧行为)
    1. @Configuration
    2. public class RedisConfig {
    3.     @Bean
    4.     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    5.         RedisTemplate<String, Object> template = new RedisTemplate<>();
    6.         template.setKeySerializer(new StringRedisSerializer());
    7.         template.setValueSerializer(new JdkSerializationRedisSerializer());
    8.         template.setConnectionFactory(factory);
    9.         return template;
    10.     }
    11. }
    复制代码

四、集群与哨兵模式的配置迁移

4.1 Spring Boot 2.x

  1. spring:
  2.   redis:
  3.     cluster:
  4.       nodes: localhost:7000,localhost:7001
  5.     sentinel:
  6.       master: master1
  7.       nodes: localhost:26379
复制代码
4.2 Spring Boot 3.x

  1. spring:
  2.   data:
  3.     redis:
  4.       cluster:
  5.         nodes: localhost:7000,localhost:7001
  6.       sentinel:
  7.         master: master1
  8.         nodes: localhost:26379
复制代码

五、连接池与超时参数的规范化

5.1 Spring Boot 2.x

  1. spring:
  2.   redis:
  3.     lettuce:
  4.       pool:
  5.         max-active: 8
  6.         max-idle: 8
  7.         min-idle: 0
  8.         max-wait: -1
  9.       read-timeout: 1000
复制代码
5.2 Spring Boot 3.x

  1. spring:
  2.   data:
  3.     redis:
  4.       lettuce:
  5.         pool:
  6.           max-active: 8
  7.           max-idle: 8
  8.           min-idle: 0
  9.           max-wait: -1ms
  10.         read-timeout: 1000ms
复制代码


  • 关键变化

    • 超时参数需显式指定单位(如 ms)。
    • max-wait 改为 max-wait-time(部分版本大概不同,需参考文档)。


六、Redisson 集成的路径调解

6.1 Spring Boot 2.x

  1. spring:
  2.   redis:
  3.     redisson:
  4.       config: |
  5.         singleServerConfig:
  6.           address: redis://localhost:6379
复制代码
6.2 Spring Boot 3.x

  1. spring:
  2.   data:
  3.     redis:
  4.       redisson:
  5.         config: |
  6.           singleServerConfig:
  7.             address: redis://localhost:6379
复制代码

七、安全性配置:SSL/TLS 的统一路径

7.1 Spring Boot 3.x

  1. spring:
  2.   data:
  3.     redis:
  4.       ssl: true
  5.       ssl-trust-store: classpath:truststore.jks
  6.       ssl-trust-store-password: mypassword
  7.       ssl-trust-store-type: JKS
复制代码

八、迁移策略与最佳实践

8.1 配置文件迁移步调


  • 替换前缀

    • 将 spring.redis 改为 spring.data.redis。

  • 移除 Jedis 配置

    • 删除 jedis.pool 相关参数。

  • 查抄超时单位

    • 为 timeout、read-timeout 等参数添加 ms 后缀。

8.2 依赖管理

  1. <!-- Spring Boot 3.x 依赖 -->
  2. <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <dependency>
  7.     <groupId>io.lettuce</groupId>
  8.     <artifactId>lettuce-core</artifactId>
  9.     <version>6.2.4.RELEASE</version> <!-- 保持最新版本 -->
  10. </dependency>
复制代码
8.3 测试与验证

  1. @SpringBootTest
  2. @AutoConfigureCache
  3. public class RedisConfigTest {
  4.     @Autowired
  5.     private RedisTemplate<String, String> redisTemplate;
  6.     @Test
  7.     public void testRedisConnection() {
  8.         redisTemplate.opsForValue().set("testKey", "testValue");
  9.         assertEquals("testValue", redisTemplate.opsForValue().get("testKey"));
  10.     }
  11. }
复制代码

九、性能优化与高级配置

9.1 Lettuce 的异步与响应式支持



  • 异步操作
    1. @Autowired
    2. private ReactiveRedisTemplate<String, String> reactiveTemplate;
    3. public Mono<String> asyncGet(String key) {
    4.     return reactiveTemplate.opsForValue().get(key);
    5. }
    复制代码
  • 响应式流集成
    1. @Autowired
    2. private Flux<String> reactiveKeys() {
    3.     return reactiveTemplate.keys("pattern:*").flatMap(key -> reactiveTemplate.opsForValue().get(key));
    4. }
    复制代码
9.2 高级连接池配置

  1. spring:
  2.   data:
  3.     redis:
  4.       lettuce:
  5.         pool:
  6.           max-active: 100
  7.           max-wait: 1000ms
  8.           min-idle: 10
  9.           max-idle: 50
复制代码

十、总结:模块化筹划的深层价值

Spring Boot 3.x 的配置调解并非简单的语法变化,而是 模块化筹划 的体现:


  • 统一定名空间:通过 spring.data.redis 集中管理 Redis 配置,降低认知成本。
  • 技能演进:镌汰 Jedis,拥抱 Lettuce 的非壅闭特性,提升性能与可维护性。
  • 配置标准化:强制单位标注(如 timeout: 2000ms)镌汰配置歧义。

十一、附录:关键配置对比表

配置项Spring Boot 2.xSpring Boot 3.x客户端类型spring.redis.client-typespring.data.redis.client-type连接池配置spring.redis.poolspring.data.redis.lettuce.poolJSON 序列化需手动配置默认启用SSL 配置spring.redis.sslspring.data.redis.sslRedisson 集成路径spring.redis.redissonspring.data.redis.redisson
十二、进一步阅读



  • Spring Boot 3.x 迁移指南
  • Spring Data Redis 文档
  • Lettuce 官方文档

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

汕尾海湾

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表