Spring Boot集成2个Redis
在分布式系统中,使用Redis作为缓存和数据存储是非常常见的。Redis是一个开源的内存数据库,具有高效、可靠和可扩展性的特点。Spring Boot提供了方便的方式来集成Redis,并且支持同时使用多个Redis实例。本文将介绍如何在Spring Boot中集成2个Redis实例,并给出相应的代码示例。
1. 引入依赖
首先,在pom.xml
文件中添加Redis和Spring Boot的依赖:
<dependencies>
<!-- Redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
2. 配置Redis连接
在application.properties
文件中配置两个Redis实例的连接信息:
# Redis 1连接配置
spring.redis1.host=localhost
spring.redis1.port=6379
spring.redis1.password=
spring.redis1.database=0
# Redis 2连接配置
spring.redis2.host=localhost
spring.redis2.port=6380
spring.redis2.password=
spring.redis2.database=0
3. 创建Redis配置类
创建一个RedisConfig
类,用来配置两个Redis实例的连接和相关操作:
@Configuration
@EnableCaching
public class RedisConfig {
@Value("${spring.redis1.host}")
private String redis1Host;
@Value("${spring.redis1.port}")
private int redis1Port;
@Value("${spring.redis1.password}")
private String redis1Password;
@Value("${spring.redis1.database}")
private int redis1Database;
@Value("${spring.redis2.host}")
private String redis2Host;
@Value("${spring.redis2.port}")
private int redis2Port;
@Value("${spring.redis2.password}")
private String redis2Password;
@Value("${spring.redis2.database}")
private int redis2Database;
@Bean("redis1ConnectionFactory")
public LettuceConnectionFactory redis1ConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(redis1Host);
config.setPort(redis1Port);
config.setPassword(RedisPassword.of(redis1Password));
config.setDatabase(redis1Database);
return new LettuceConnectionFactory(config);
}
@Bean("redis2ConnectionFactory")
public LettuceConnectionFactory redis2ConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(redis2Host);
config.setPort(redis2Port);
config.setPassword(RedisPassword.of(redis2Password));
config.setDatabase(redis2Database);
return new LettuceConnectionFactory(config);
}
@Bean("redis1Template")
public RedisTemplate<String, Object> redis1Template(@Qualifier("redis1ConnectionFactory") LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// 设置序列化器
template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
return template;
}
@Bean("redis2Template")
public RedisTemplate<String, Object> redis2Template(@Qualifier("redis2ConnectionFactory") LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// 设置序列化器
template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
return template;
}
@Bean("redis1CacheManager")
public CacheManager redis1CacheManager(@Qualifier("redis1Template") RedisTemplate<String, Object> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// 设置缓存过期时间
cacheManager.setDefaultExpiration(300);
return cacheManager;
}
@Bean("redis2CacheManager")
public CacheManager redis2CacheManager(@Qualifier("redis2Template") RedisTemplate<String, Object> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// 设置缓存过期时间
cacheManager.setDefaultExpiration(300);
return cacheManager;
}
}
在上述代码中,我们创建了两个LettuceConnectionFactory
对象,分别对应两个Redis实例的连接。同时,我们还创建了两个RedisTemplate
对象作为操作Redis的工具类,并设置了默认的序列化器。最后,我们还创建了两个RedisCacheManager
对象作为缓存管理器,并设置了默认的缓存过期时间。
4. 使用Redis实例
我们可以在任何Spring组件中通过依赖注入来使用两个Redis实例。以下是一个简单的示例:
@Service
public class UserService {
@Autowired
@Qualifier("