配置如下
@Bean
LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig genericObjectPoolConfig) {
// 单机版配置
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(redisProperties.getTimeout())
.poolConfig(genericObjectPoolConfig)
.build();
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
return factory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(lettuceConnectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
template.setEnableTransactionSupport(true);
return template;
}
/**
* GenericObjectPoolConfig 连接池配置
*
* @return
*/
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig() {
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
genericObjectPoolConfig.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis());
return genericObjectPoolConfig;
}
观察log中并没有关于pool相关的log,只有类似这样的log
2020-03-12 14:28:43.482 DEBUG 12992 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : 2020-03-12 14:57:17.740 INFO 9500 --- [ restartedMain] io.lettuce.core.EpollProvider : Starting without optional epoll library
2020-03-12 14:57:17.741 INFO 9500 --- [ restartedMain] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
2020-03-12 14:57:17.890 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:18.908 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:19.949 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:20.962 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:21.973 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:23.012 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:24.046 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:25.083 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:26.094 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
2020-03-12 14:57:27.106 DEBUG 9500 --- [ restartedMain] o.s.d.redis.core.RedisConnectionUtils : Leaving bound Redis Connection attached.
另外发现lettuce pool 配置注释了也没什么变化