有关Redis 其他知识脉络详细学习,可以关注 鱼皮
www.codefather.cn/course/1789…
在 Spring Boot 中集成 Redis 是一种常见的需求,因为 Redis 是一个高性能的键值存储系统,常用于缓存、会话管理、消息队列等场景。Spring Boot 提供了对 Redis 的良好支持,通过简单的配置即可快速集成 Redis。
以下是 Spring Boot 集成 Redis 的详细步骤:
1. 添加 Redis 依赖
在 pom.xml 中添加 Spring Boot 对 Redis 的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
如果你使用的是 Lettuce 作为 Redis 客户端(默认),则无需额外配置;如果需要使用 Jedis,可以添加以下依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
后续默认以 lettuce 作为默认
2. 配置 Redis 连接信息
在 application.properties 或 application.yml 中配置 Redis 的连接信息:
application.properties 配置:
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=your_password # 如果没有密码,可以省略
spring.redis.database=0 # 默认数据库索引
application.yml 配置:
spring:
redis:
host: localhost
port: 6379
database: 0
timeout: 2000
password: # 如果没有密码,可以省略
lettuce:
pool: # 配置连接池
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
3. 使用 RedisTemplate 操作 Redis
Spring Boot 提供了 RedisTemplate 和 StringRedisTemplate 来操作 Redis。RedisTemplate 支持泛型操作,而 StringRedisTemplate 专门用于操作字符串类型的数据。
示例代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void setValue(String key, String value) {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
ops.set(key, value);
}
public String getValue(String key) {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
return ops.get(key);
}
}
4. 使用 StringRedisTemplate
如果你只需要操作字符串类型的数据,可以使用 StringRedisTemplate:
示例代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setValue(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
public String getValue(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
}
5. 使用 Redis 作为缓存
Spring Boot 支持将 Redis 作为缓存管理器。首先需要在启动类上添加 @EnableCaching 注解,然后在需要缓存的方法上添加 @Cacheable 注解。
配置缓存:
在 application.properties 或 application.yml 中配置缓存过期时间等参数:
spring.cache.type=redis
spring.cache.redis.time-to-live=60000 # 缓存过期时间(毫秒)
示例代码:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable(value = "users", key = "#id")
public String getUserById(String id) {
// 模拟从数据库查询数据
return "User " + id;
}
}
6. 测试 Redis 集成
编写单元测试或通过 API 测试 Redis 的功能是否正常。
示例测试代码:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class RedisServiceTest {
@Autowired
private RedisService redisService;
@Test
public void testRedis() {
redisService.setValue("testKey", "testValue");
String value = redisService.getValue("testKey");
System.out.println("Value from Redis: " + value);
}
}
8. 常见问题
- 连接失败:检查 Redis 服务是否启动,以及配置的主机名、端口和密码是否正确。
- 序列化问题:如果存储的对象没有实现
Serializable接口,可能会导致序列化失败。 - 性能问题:在高并发场景下,建议使用连接池(如 Lettuce 或 Jedis 连接池)来优化性能。
通过以上步骤,你可以轻松在 Spring Boot 中集成 Redis,并实现缓存、数据存储等功能。