在Springboot升级到2.0以后,Redis的配置相比于2.0之前有了特别大的变化,我也初学Springboot2.0,肯定有写得不太对的地方,希望能让阅读这篇帖子的朋友们都有所收获,本文中的springboot版本为:2.2.0.BUILD-SNAPSHOT
在springboot2.0以后,redis的配置给我的印象主要有两种方式,一是使用standalone方式,就是传统基于jedis的配置,二是使用Lettuce方式,基于netty做的通信,为springboot2.0的新方式,推荐使用lettuce方式,经过批量set和delete的测试(直接使用RedisTemplate的delete方法和opsFor中的set方法),后者的效率比前者要高出一倍左右。
在本文中不管是standalone还是lettuce方式均使用apache的commons-pool2中提供的GenericObjectPoolConfig实例做为redis的连接池,放弃使用jedisPoolConfig。
pom中配置Redis必须要有引用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
Spring的配置文件application.yml:
由于是手动配置多个Redis连接实例,所以必须要注意pool和redia-a/b/c下的变量名称,和springboot的auto-cofing名称有出入
spring:
redis:
lettuce:
pool:
MaxTotal: 50
minIdle: 1
maxWaitMillis: 5000
maxIdle: 5
testOnBorrow: true
testOnReturn: true
testWhileIdle: true
redis-a:
database: 0
hostName: 192.168.0.88
port: 6379
timeout: 5000
redis-b:
database: 1
hostName: ${spring.redis.redis-a.hostName}
port: ${spring.redis.redis-a.port}
timeout: ${spring.redis.redis-a.timeout}
redis-c:
database: 2
hostName: ${spring.redis.redis-a.hostName}
port: ${spring.redis.redis-a.port}
timeout: ${spring.redis.redis-a.timeout}
standalone
standalone方式配置多个redis连接实例,在使用redis的standalone方式时,需要额外在pom中引入jedis客户端,反正我的需要,虽然我在spring-boot-starter-data-redis这个starter的pom中看到有引入jedis,但是在我的这个demo中并没有把jedis import进来
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
RedisTemplate的配置:
@Configuration
public class StandaloneRedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
@Scope(value = "prototype")
public GenericObjectPoolConfig redisPool(){
return new GenericObjectPoolConfig();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.redis-a")
public RedisStandaloneConfiguration standaloneConfigurationA() {
return new RedisStandaloneConfiguration();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.redis-b")
public RedisStandaloneConfiguration standaloneConfigurationB() {
return new RedisStandaloneConfiguration();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.redis-c")
public RedisStandaloneConfiguration standaloneConfigurationC() {
return new RedisStandaloneConfiguration();
}
@Bean
public JedisConnectionFactory standaloneFactoryA(){
JedisClientConfiguration poolConfig = JedisClientConfiguration.builder().usePooling().poolConfig(redisPool()).build();
return new JedisConnectionFactory(standaloneConfigurationA(), poolConfig);
}
@Bean
public JedisConnectionFactory standaloneFactoryB(){
JedisClientConfiguration poolConfig = JedisClientConfiguration.builder().usePooling().poolConfig(redisPool()).build();
return new JedisConnectionFactory(standaloneConfigurationB(), poolConfig);
}
@Bean
public JedisConnectionFactory standaloneFactoryC(){
JedisClientConfiguration poolConfig = JedisClientConfiguration.builder().usePooling().poolConfig(redisPool()).build();
return new JedisConnectionFactory(standaloneConfigurationC(), poolConfig);
}
@Bean
public RedisTemplate StringRedisTemplateA(){
RedisTemplate<String, String> template = getRedisTemplate();
template.setConnectionFactory(standaloneFactoryA());
return template;
}
@Bean
public RedisTemplate StringRedisTemplateB(){
RedisTemplate<String, String> template = getRedisTemplate();
template.setConnectionFactory(standaloneFactoryB());
return template;
}
@Bean
public RedisTemplate StringRedisTemplateC(){
RedisTemplate<String, String> template = getRedisTemplate();
template.setConnectionFactory(standaloneFactoryC());
return template;
}
private RedisTemplate<String, String> getRedisTemplate(){
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setValueSerializer(new GenericFastJsonRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
Lettuce
lettuce方式不需要在pom中引入redisClient的包
RedisTemplate配置:
@Configuration
public class LettuceRedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
@Scope(value = "prototype")
public GenericObjectPoolConfig redisPool(){
return new GenericObjectPoolConfig();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.redis-a")
public RedisStandaloneConfiguration redisConfigA(){
return new RedisStandaloneConfiguration();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.redis-b")
public RedisStandaloneConfiguration redisConfigB(){
return new RedisStandaloneConfiguration();
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.redis-c")
public RedisStandaloneConfiguration redisConfigC(){
return new RedisStandaloneConfiguration();
}
@Bean
@Primary
public LettuceConnectionFactory factoryA(){
GenericObjectPoolConfig config = redisPool();
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
.poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
return new LettuceConnectionFactory(redisConfigA(), clientConfiguration);
}
@Bean
public LettuceConnectionFactory factoryB(){
GenericObjectPoolConfig config = redisPool();
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
.poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
return new LettuceConnectionFactory(redisConfigB(), clientConfiguration);
}
@Bean
public LettuceConnectionFactory factoryC(){
GenericObjectPoolConfig config = redisPool();
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
.poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
return new LettuceConnectionFactory(redisConfigC(), clientConfiguration);
}
@Bean
public StringRedisTemplate redisTemplateA(){
StringRedisTemplate template = getRedisTemplate();
template.setConnectionFactory(factoryA());
return template;
}
@Bean
public StringRedisTemplate redisTemplateB(){
StringRedisTemplate template = getRedisTemplate();
template.setConnectionFactory(factoryB());
return template;
}
@Bean
public StringRedisTemplate redisTemplateC(){
StringRedisTemplate template = getRedisTemplate();
template.setConnectionFactory(factoryC());
return template;
}
private StringRedisTemplate getRedisTemplate(){
StringRedisTemplate template = new StringRedisTemplate();
template.setValueSerializer(new GenericFastJsonRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
本文只是抛砖引玉,希望大家给出修改意见