springboot redis多数据源配置
时间: 2023-07-22 17:08:50 浏览: 249
springboot多数据源配置
在Spring Boot中配置多个Redis数据源需要做以下几个步骤:
1. 在pom.xml中添加Redis和Lettuce的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
```
2. 在application.yml中配置多个Redis数据源,例如:
```yaml
spring:
redis:
host: localhost
port: 6379
password: password
database: 0
jedis:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
redis2:
host: localhost
port: 6380
password: password
database: 1
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
```
注意,每个Redis数据源需要定义一个唯一的名称,如redis和redis2。
3. 在Java代码中创建多个RedisTemplate实例,并指定对应的RedisConnectionFactory,例如:
```java
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
return redisTemplate;
}
@Bean(name = "redisTemplate2")
public RedisTemplate<String, Object> redisTemplate2(@Qualifier("redisConnectionFactory2") RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
return redisTemplate;
}
@Bean(name = "redisConnectionFactory")
public RedisConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(redisProperties.getHost());
jedisConnectionFactory.setPort(redisProperties.getPort());
jedisConnectionFactory.setPassword(redisProperties.getPassword());
jedisConnectionFactory.setDatabase(redisProperties.getDatabase());
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.getPoolConfig().setMaxTotal(redisProperties.getJedis().getPool().getMaxActive());
jedisConnectionFactory.getPoolConfig().setMaxIdle(redisProperties.getJedis().getPool().getMaxIdle());
jedisConnectionFactory.getPoolConfig().setMinIdle(redisProperties.getJedis().getPool().getMinIdle());
jedisConnectionFactory.getPoolConfig().setMaxWaitMillis(redisProperties.getJedis().getPool().getMaxWait().toMillis());
return jedisConnectionFactory;
}
@Bean(name = "redisConnectionFactory2")
public RedisConnectionFactory redisConnectionFactory2(@Qualifier("redisProperties2") RedisProperties redisProperties) {
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
lettuceConnectionFactory.setHostName(redisProperties.getHost());
lettuceConnectionFactory.setPort(redisProperties.getPort());
lettuceConnectionFactory.setPassword(redisProperties.getPassword());
lettuceConnectionFactory.setDatabase(redisProperties.getDatabase());
lettuceConnectionFactory.setUsePooling(true);
lettuceConnectionFactory.setPoolConfig(getLettucePoolConfig(redisProperties.getLettuce().getPool()));
return lettuceConnectionFactory;
}
@Bean(name = "redisProperties")
@ConfigurationProperties(prefix = "spring.redis")
public RedisProperties redisProperties() {
return new RedisProperties();
}
@Bean(name = "redisProperties2")
@ConfigurationProperties(prefix = "spring.redis2")
public RedisProperties redisProperties2() {
return new RedisProperties();
}
private GenericObjectPoolConfig<?> getLettucePoolConfig(LettucePoolingClientConfiguration.Pool pool) {
GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(pool.getMaxActive());
config.setMaxIdle(pool.getMaxIdle());
config.setMinIdle(pool.getMinIdle());
config.setMaxWaitMillis(pool.getMaxWait().toMillis());
return config;
}
}
```
要使用第二个Redis数据源,只需使用@Qualifier指定对应的RedisTemplate实例,例如:
```java
@Autowired
@Qualifier("redisTemplate2")
private RedisTemplate<String, Object> redisTemplate2;
```
这样就可以在Spring Boot中配置多个Redis数据源了。
阅读全文