SpringBoot2中使用Lettuce 拓扑刷新问题

SpringBoot2.x开始默认使用的Redis客户端由Jedis变成了Lettuce,但是当Redis集群中某个节点挂掉之后,Lettuce将无法继续操作Redis,原因在于此时Lettuce使用的仍然是有问题的连接信息。

实际上,Lettuce支持redis 集群拓扑动态刷新,但是默认并没有开启,SpringBoot在集成Lettuce时默认也没有开启。并且在SpringBoot2.3.0之前,是没有配置项设置Lettuce自动刷新拓扑的。

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

解决方案1:

升级到SpringBoot2.3.0或以上版本。并添加如下配置项

spring.redis.timeout=60s
spring.redis.lettuce.cluster.refresh.period=60s
spring.redis.lettuce.cluster.refresh.adaptive=true

解决方案2:
配置LettuceConnectionFactory ,设置拓扑刷新策略。

@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;
}

解决方案3:

1 将spring-boot-starter-data-redis依赖的Lettuce,修改成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>

 

 

Spring Boot 使用 Lettuce 连接 Redis 时,可以通过自适应配置来适配单机版 Redis 和集群版 Redis。自适应配置是指,当配置文件的属性值符合某种特定的格式时,Lettuce 会自动识别当前 Redis 环境是否为集群版,并自动进行相应的连接池配置。 具体来说,当 `spring.redis.host` 属性为空时,Lettuce 将会按照集群版 Redis 的方式进行连接。此时,需要在 `spring.redis.cluster.nodes` 属性指定 Redis 集群所有节点的地址和端口号,以逗号分隔。例如: ```properties # Redis 自适应集群版连接池配置 spring.redis.host= spring.redis.port= spring.redis.password=your_password spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381 spring.redis.timeout=1000 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0 ``` 当 `spring.redis.host` 属性不为空时,Lettuce 将会按照单机版 Redis 的方式进行连接。此时,只需要在 `spring.redis.host` 和 `spring.redis.port` 属性指定 Redis 服务器的地址和端口号即可。例如: ```properties # Redis 自适应单机版连接池配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=your_password spring.redis.timeout=1000 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0 ``` 需要注意的是,当 `spring.redis.host` 属性为空时,必须同时指定 `spring.redis.cluster.nodes` 属性,否则会抛出异常。此外,当使用自适应配置连接集群版 Redis 时,需要在 `pom.xml` 文件添加 Lettuce 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.3.4.RELEASE</version> </dependency> ``` 以上就是在 Spring Boot 使用 Lettuce 进行自适应配置连接单机版 Redis 和集群版 Redis 的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值