Spring Boot 3.4.4与JDK21集成Redis实战
环境准备
通过Spring Initializr创建基础项目结构,选择Web和Data Redis依赖项。
Redis服务部署
- 下载Redis 7.0.11版本(支持JDK21)
- 执行redis-server.exe启动服务端
- 通过redis-cli.exe验证连接状态(预期响应"PIPED")
- 推荐使用RedisDesktopManager进行可视化管理
核心配置实现
package com.example.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 键值序列化策略
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// 值对象序列化策略
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
接口层设计
package com.example.redis.controller;
import com.example.redis.service.RedisService;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/redis")
public class RedisController {
private final RedisService redisService;
public RedisController(RedisService redisService) {
this.redisService = redisService;
}
@PostMapping("/store")
public Map storeData(@RequestBody Map request) {
// 参数校验逻辑
// 数据存储操作
// 异常处理机制
return responseMap;
}
@PostMapping("/modify")
public Map modifyData(@RequestBody Map request) {
// 更新逻辑实现
// 原子性校验
// 返回更新结果
return responseMap;
}
@PostMapping("/partial")
public Map partialUpdate(@RequestBody Map request) {
// 部分更新逻辑
// 类型校验
// 合并操作
return responseMap;
}
}
业务逻辑封装
package com.example.redis.service;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Map;
@Service
public class RedisService {
private final RedisTemplate redisTemplate;
public RedisService(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void save(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object retrieve(String key) {
return redisTemplate.opsForValue().get(key);
}
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
public void update(String key, Object newValue) {
if (!exists(key)) {
throw new IllegalArgumentException("Key not found");
}
redisTemplate.opsForValue().set(key, newValue);
}
public void merge(String key, Map fields) {
Object current = retrieve(key);
if (current instanceof Map) {
((Map)current).putAll(fields);
redisTemplate.opsForValue().set(key, current);
} else {
throw new IllegalArgumentException("Value is not a map");
}
}
}
构建配置
<project>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
连接参数配置
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.timeout=5000ms
spring.data.redis.lettuce.pool.max-active=8
spring.data.redis.lettuce.pool.max-idle=8
spring.data.redis.lettuce.pool.min-idle=0
spring.data.redis.lettuce.pool.max-wait=-1ms
技术架构图
项目特性
- 轻量化设计:专注于缓存操作模块
- 技术前沿性:采用Spring Boot 3.4.4+JDK21组合
- 规范性:遵循RESTful API设计标准
- 可扩展性:分层架构便于功能扩展
- 生产就绪:内置健康检查与日志监控