Use Lettuce topology refresh problem in SpringBoot2

The Redis client used by SpringBoot 2.x by default has changed from Jedis to Lettuce, but when a node in the Redis cluster hangs down, Lettuce will not be able to continue to operate Redis because Lettuce still uses the connection information that is problematic at this time .

In fact, Lettuce supports the dynamic refresh of the redis cluster topology, but it is not turned on by default, and SpringBoot is not turned on by default when integrating Lettuce. And before SpringBoot 2.3.0, there was no configuration item to set Lettuce to automatically refresh the topology.

相关issue:Add configuration to enable Redis Cluster topology refresh

Solution 1:

  1. Upgrade to SpringBoot 2.3.0 or above. And add the following configuration items
spring.redis.timeout=60s
spring.redis.lettuce.cluster.refresh.period=60s
spring.redis.lettuce.cluster.refresh.adaptive=true

Solution 2:
Configure LettuceConnectionFactory and set topology refresh strategy.

@Bean
public DefaultClientResources lettuceClientResources() {
    
    
    return DefaultClientResources.create();
}

@Bean
public LettuceConnectionFactory lettuceConnectionFactory(RedisProperties redisProperties, ClientResources clientResources) {
    
    

    ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
            .enablePeriodicRefresh(Duration.ofSeconds(30)) //按照周期刷新拓扑
            .enableAllAdaptiveRefreshTriggers() //根据事件刷新拓扑
            .build();

    ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
            //redis命令超时时间,超时后才会使用新的拓扑信息重新建立连接
            .timeoutOptions(TimeoutOptions.enabled(Duration.ofSeconds(10)))
            .topologyRefreshOptions(topologyRefreshOptions)
            .build();

    LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
            .clientResources(clientResources)
            .clientOptions(clusterClientOptions)
            .build();

    RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
    clusterConfig.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
    clusterConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));

    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(clusterConfig, clientConfiguration);

    return lettuceConnectionFactory;
}

Solution 3:

  1. Change Lettuce, which spring-boot-starter-data-redis depends on, to Jedis.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

Guess you like

Origin blog.csdn.net/u013202238/article/details/108528650