马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
媒介
Spring Boot 3.4.x作为当前最新稳固版本,全面支持Java 17+与Jakarta EE 10规范。本文以Spring Boot 3.4.1和Redis 7.x为例,详解怎样在IDEA中快速接入Redis,涵盖最新依靠设置、数据序列化优化、缓存注解及高版本常见题目排查,助你把握企业级缓存方案!
一、环境要求
1. 根本环境
- JDK 17+(保举OpenJDK 17/21)
- IntelliJ IDEA 2023.1+
- Redis 7.x(官网下载)
2. 项目初始化
- 使用Spring Initializr创建项目,选择:
- Spring Boot 3.4.x
- 依靠项:Spring Web, Spring Data Redis, Lombok
二、依靠设置(Spring Boot 3.4.x+特性)
1. pom.xml关键依靠
- <!-- Spring Boot 3.4.x 父工程 -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>3.4.1</version>
- </parent>
- <!-- Redis Starter(默认Lettuce) -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- <!-- Jakarta JSON处理(兼容Redis序列化) -->
- <dependency>
- <groupId>jakarta.json</groupId>
- <artifactId>jakarta.json-api</artifactId>
- <version>2.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish</groupId>
- <artifactId>jakarta.json</artifactId>
- <version>2.0.1</version>
- </dependency>
复制代码 注:Spring Boot 3.4.x默认使用Lettuce 6.3.x,无需额外设置毗连池。若需Jedis,需手动清除Lettuce并添加Jedis依靠。
三、Redis毗连设置
1. application.yml(保举格式)
- spring:
- data:
- redis:
- host: localhost
- port: 6379
- password: # 无密码留空
- database: 0
- lettuce:
- pool:
- max-active: 8
- max-idle: 4
- min-idle: 0
- max-wait: 2000ms
- # 新版本支持客户端名称(监控
识别) - client-name: springboot-app
复制代码 2. 自界说序列化(办理Jackson兼容性题目)
- @Configuration
- public class RedisConfig {
- @Bean
- public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
- RedisTemplate<String, Object> template = new RedisTemplate<>();
- template.setConnectionFactory(factory);
-
- // Key使用String序列化
- template.setKeySerializer(RedisSerializer.string());
- // Value使用JSON序列化(适配Jakarta)
- template.setValueSerializer(buildJsonSerializer());
-
- return template;
- }
- private RedisSerializer<Object> buildJsonSerializer() {
- ObjectMapper mapper = new ObjectMapper();
- mapper.registerModule(new JavaTimeModule());
- mapper.activateDefaultTyping(
- mapper.getPolymorphicTypeValidator(),
- ObjectMapper.DefaultTyping.EVERYTHING,
- JsonTypeInfo.As.PROPERTY
- );
- return new GenericJackson2JsonRedisSerializer(mapper);
- }
- }
复制代码 四、缓存注解实战(适配Spring 6.x+)
1. 启用缓存
- @SpringBootApplication
- @EnableCaching
- public class DemoApplication { ... }
复制代码 2. 服务层缓存示例
- @Service
- @CacheConfig(cacheNames = "userCache")
- public class UserService {
- @Autowired
- private UserRepository repository;
- // 使用SpEL动态生成Key
- @Cacheable(key = "#id + '_' + #type")
- public User getUser(Long id, String type) {
- return repository.findById(id).orElseThrow();
- }
- // 条件缓存(仅缓存长度>5的名称)
- @Cacheable(key = "#name", condition = "#name.length() > 5")
- public User getUserByName(String name) {
- return repository.findByName(name);
- }
- // 事务性缓存更新(结合@Transactional)
- @Transactional
- @CachePut(key = "#user.id")
- public User updateUser(@NonNull User user) {
- return repository.save(user);
- }
- }
复制代码 五、相应式Redis利用(可选)
1. 添加相应式依靠
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
- </dependency>
复制代码 2. 相应式模板使用
- @Autowired
- private ReactiveRedisTemplate<String, Object> reactiveTemplate;
- public Mono<Boolean> saveSession(String sessionId, User user) {
- return reactiveTemplate.opsForValue()
- .set("session:" + sessionId, user, Duration.ofMinutes(30));
- }
复制代码 六、常见题目与办理方案
Q1:Jackson序列化失败(Jakarta兼容性题目)
- 征象:InvalidTypeIdException: Missing type id when trying to resolve subtype
- 办理:
- 确认添加了jakarta.json依靠。
- 在RedisConfig中显式设置ObjectMapper(参考上文)。
Q2:Redis毗连池不收效
- 缘故原由:Spring Boot 3.4.x默认启用Lettuce毗连池,无需额外设置。
- 验证:通过/actuator/metrics/redis.pool.active端点监控
毗连状态。
Q3:Spring Boot 3.4.x与Redis 7.x的TLS毗连
- spring:
- data:
- redis:
- url: rediss://localhost:6379 # SSL协议
- ssl: true
- client-type: lettuce
- lettuce:
- pool:
- enabled: true
- ssl:
- key-store-type: PKCS12
- key-store: classpath:keystore.p12
- key-store-password: 123456
复制代码 总结
Spring Boot 3.4.x在Redis集成上进一步优化了性能与兼容性,支持最新的Jakarta尺度和相应式编程。本文从依靠设置到高级特性,提供了一套完备的生产级办理方案。
|