-2

My old config like

spring.redis.host=192.168.1.1

Now I want to modify

spring.redis.host=192.168.1.2

But need not restart My project, How can I do that?

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
sean
  • 11
  • 1
  • 1
    Please specify your real problem. Do you really need to implement live reload for configuration files or you need to reconnect to modified Redis host if it's changed? It's two different problem and two different possible solution., – zforgo Jun 02 '20 at 07:29

1 Answers1

1

ok, I found the answer:

        RedisTemplate template = (RedisTemplate) applicationContext.getBean("redisTemplate");
        JedisConnectionFactory redisConnectionFactory = (JedisConnectionFactory) template.getConnectionFactory();
        //关闭连接池
        redisConnectionFactory.destroy();
        redisConnectionFactory.setShardInfo(null);
        redisConnectionFactory.setHostName(host);
        redisConnectionFactory.setPort(port);
        redisConnectionFactory.setPassword(password);
        redisConnectionFactory.setDatabase(database);
        //重新创建连接池
        redisConnectionFactory.afterPropertiesSet();
        template.setConnectionFactory(redisConnectionFactory);
sean
  • 11
  • 1