如何修改spring.redis.host配置而不必重新启动springboot项目?

eqqqjvef  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(480)

我以前的配置像

spring.redis.host=192.168.1.1

现在我想修改

spring.redis.host=192.168.1.2

但不需要重新启动我的项目,我怎么做呢?

8oomwypt

8oomwypt1#

好吧,我找到了答案:

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);

相关问题