Aphsss
V2EX  ›  问与答

springboot 到底怎么配置 Redis 的连接池啊

  •  
  •   Aphsss · Aug 29, 2018 · 6015 views
    This topic created in 2813 days ago, the information mentioned may be changed or developed.

    程序是用的 Idea 创建的 2.1.0.M1 版本的 spring-boot-starter-parent, 用的 spring-boot-starter-data-redis2.6.0版本的 commons-pool2 依赖

    application.properties 的配置是

    spring.redis.host= 127.0.0.1
    spring.redis.port= 6379
    spring.redis.lettuce.pool.max-active= 8
    spring.redis.lettuce.pool.max-idle= 4
    spring.redis.lettuce.pool.min-idle= 4
    spring.redis.lettuce.pool.max-wait= -1ms
    

    代码块 1:

    @Configuration
    public class RedisConfigure {
    
      @Autowired
      RedisConnectionFactory redisConnectionFactory;
    
      @Bean("conn")
      RedisConnection getConn() {
        return redisConnectionFactory.getConnection();
      }
    
      @Bean
      RedisTemplate<String, UserEntity> userEntityRedisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, UserEntity> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
      }
    }
    

    代码块 2:

      @Autowired
      RedisTemplate<String, UserEntity> userEntityRedisTemplate;
    
      public Long saveToRedisMessageQueue() {
        return userEntityRedisTemplate.opsForList().leftPush("message:q", new UserEntity("lisi","passwd"));
      }
    

    此时 Redis 是零连接的,我在 redis-cli 里面执行info clientsconnected_clients:1

    然后启动程序,现在程序是可以操作 Redis 的,但是我在 redis-cli 里面执行info clientsconnected_clients:2

    啊啊啊啊啊。为什么不是 connected_clients:5 啊,怎么才能变成connected_clients:5 这样

    感谢各位的解答

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4241 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:56 · PVG 08:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.