spring-boot-starter-data-redis lettuce pool 怎么获取连接池中连接数量?

配置如下

@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 配置注释了也没什么变化

没有通过 @Configuration 的方式来整合过。我一直用配置的形式来配置连接信息和连接池。这也是官方的配置,你要不然试试看?

spring:
  redis:
    database: 10
    host: 127.0.0.1
    port: 6379
    password: "123456"
    timeout: 2000
    lettuce:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0

maven还要依赖

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
</dependency>

我也没注意过这redis的日志,我觉得它挺烦人。我都给设置了level=INFO

同样的配置,log输出如下

2020-03-12 15:11:45.543 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:45.594  INFO 18264 --- [  restartedMain] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2020-03-12 15:11:45.594  INFO 18264 --- [  restartedMain] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
2020-03-12 15:11:45.781 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:46.782 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:46.795 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:47.795 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:47.809 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:48.810 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:48.823 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:49.824 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:49.836 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:50.838 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:50.850 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:51.851 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:51.870 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:52.871 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:52.884 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:53.884 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:53.896 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.
2020-03-12 15:11:54.898 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
2020-03-12 15:11:54.927 DEBUG 18264 --- [  restartedMain] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection.

感觉没用连接池

我在 stackoverflow 尝试搜了一下这个问题

这个日志是springRedisConnectionUtils 打印的,它不能直接创建,关闭链接。
本质上还是调用了底层的 XXXConnectionFactory 来获取链接,底层仍是用的连接池。

可以试着DBUG跟踪代码验证一下,我也不确定stackoverflow 的说法是否正确。