Spring Data Redis Lettuce 连接问题

2024-02-18

我有带有 MYSQL DB 的 Spring Boot 应用程序,并且我使用 @Cacheable Annotation 将数据缓存在服务器层的 Redis 中

@Cacheable(value= "employeeCache", key= "#customerId")
@Override
public Customer getEmployee(String customerId) {
    Optional<Customer> cust = customerRepository.findById(Long.parseLong(customerId));
    if(cust.isPresent()) {
        return cust.get();
    }
    return null;
}

我使用 1 个主节点、2 个从节点和 2 个哨兵节点,我已将应用程序部署在 AWS ec2 ubuntu 实例中的 docker 容器中。

应用程序和 Redis 主/从设置工作正常。

当 Redis 主容器出现故障时,其中一个从服务器成为主容器,这也可以正常工作

但是一旦其中一个从站成为主站,那么 Spring boot 就无法连接到 Redis,我会遇到连接超时,只有重新启动容器才能工作。

请检查配置是否有错误。

Java日志

2020-05-21 18:04:50.016 ERROR 1 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 minute(s)] with root cause

io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 minute(s)
        at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51) ~[lettuce-core-5.3.0.RELEASE.jar!/:5.3.0.RELEASE]
        at io.lettuce.core.protocol.CommandExpiryWriter.lambda$potentiallyExpire$0(CommandExpiryWriter.java:167) ~[lettuce-core-5.3.0.RELEASE.jar!/:5.3.0.RELEASE]
        at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98) ~[netty-common-4.1.49.Final.jar!/:4.1.49.Final]
        at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:170) ~[netty-common-4.1.49.Final.jar!/:4.1.49.Final]
        at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:66) ~[netty-common-4.1.49.Final.jar!/:4.1.49.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.49.Final.jar!/:4.1.49.Final]
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.49.Final.jar!/:4.1.49.Final]
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.49.Final.jar!/:4.1.49.Final]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]

Spring引导文件

应用程序.yaml

spring:
  datasource:
    url: jdbc:mysql://docker-mysql:3306/customerdb
    username: root
    password: root

  jpa:
    show-sql: true

    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl


  cache:
    type: redis  

  redis:
    sentinel:
      master: mymaster
      nodes:
        - redis-sentinel:26379
        - redis-sentinel-1:26380

management:
  endpoints:
    web: 
      exposure:
        include:
        - "*"
server:
  port: 8080

logging:
  file:
    name: customer.log
    path: ../logs

用于 Redis 和 Spring Boot 部署的文件

Docker-compose.yaml

version: '3.5'

services:

  redis-master:
    container_name: redis-master
    image: redis
    volumes:
      - "./data/redis-master:/data"
    networks:
      - backend

  redis-slave:
    container_name: redis-slave
    image: redis
    command: redis-server --slaveof redis-master 6379
    volumes:
      - "./data/redis-slave:/data"
    depends_on:
      - redis-master
    networks:
      - backend

  redis-slave-1:
    container_name: redis-slave-1
    image: redis
    command: redis-server --slaveof redis-master 6379
    volumes:
      - "./data/redis-slave:/data"
    depends_on:
      - redis-master
    networks:
      - backend    

  redis-sentinel:
    container_name: redis-sentinel
    ports:
      - "26379:26379"
    build: redis-sentinel
    depends_on:
      - redis-master
      - redis-slave
    networks:
      - backend

  redis-sentinel-1:
    container_name: redis-sentinel-1
    build: redis-sentinel
    ports:
      - "26380:26379"
    depends_on:
      - redis-master
      - redis-slave
    networks:
      - backend

  docker-mysql:
    restart: always
    container_name: docker-mysql
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: customerdb
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_HOST: "%"
    volumes:
      - db-data-new:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - backend

  customer-app:
    container_name: java-sentinel
    restart: on-failure
    image: kuldeep99/customer-sentinel:v10
    expose:
      - "8080"
    volumes:
      - /tmp:/logs
    ports:
      - 8080:8080
    depends_on:
      - docker-mysql
      - redis-master
      - redis-slave
      - redis-sentinel
    networks:
      - backend

volumes:
  db-data-new:

networks:
  backend:

Dockerfile

FROM redis

EXPOSE 26379
ENV SENTINEL_QUORUM 2
ENV SENTINEL_DOWN_AFTER 3000
ENV SENTINEL_FAILOVER 3000
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
ADD sentinel-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
ENTRYPOINT ["sentinel-entrypoint.sh"]

哨兵入口点.sh

#!/bin/sh

sed -i "s/\$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf
sed -i "s/\$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf
sed -i "s/\$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf

redis-server /etc/redis/sentinel.conf --sentinel

哨兵配置文件

port 26379
protected-mode no
dir /tmp
sentinel monitor mymaster redis-master 6379 $SENTINEL_QUORUM
sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster $SENTINEL_FAILOVER

我认为您没有设置拓扑刷新配置。 你可以像这样启用它们 -

解决方案1:

升级到SpringBoot 2.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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Data Redis Lettuce 连接问题 的相关文章

随机推荐