Skip to content

SpringBoot 集成redis cluster集群

pom文件

xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
yaml
spring:
  application:
    name: cluster
  data:
    redis:
      password: 123456
      cluster:
        nodes:
          - 192.168.152.150:6379
          - 192.168.152.150:6380
          - 192.168.152.150:6381
          - 192.168.152.150:6382
          - 192.168.152.150:6383
          - 192.168.152.150:6384

SpringBoot 无法自动感知cluster

错误信息

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.QueryTimeoutException: Redis command timed out] with root cause

io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 minute(s)

解决

  1. 换成 jedis
  2. 重写 连接工厂
  3. 动态感应 redis集群动态配置
yaml
spring:
  application:
    name: cluster
  data:
    redis:
      password: 123456
      cluster:
        nodes:
          - 192.168.152.150:6379
          - 192.168.152.150:6380
          - 192.168.152.150:6381
          - 192.168.152.150:6382
          - 192.168.152.150:6383
          - 192.168.152.150:6384
      lettuce:
        cluster:
          refresh:
            adaptive: true # 开启动态刷新
            period: 2000 # 刷新时间