当前位置: 首页>后端>正文

springboot连接redis哨兵

一、集群介绍

1主2从3哨兵

springboot连接redis哨兵,第1张
1709361059832.png

二、程序配置

2.1 redis哨兵

连接池使用高性能的lettuce,底层基于Netty实现

  • yml配置

springboot2.3.12版本源码,哨兵读取密码的配置,过低的版本没有


springboot连接redis哨兵,第2张
1706259563781.png
spring:
  redis:
    sentinel:
      master: mymaster
      nodes: 10.255.1.47:26379,10.255.1.48:26379,10.255.1.49:26379
      password: xxx  #Sentinel哨兵连接密码(默认为空)
    database: 0  #Redis数据库索引(默认为0)
    password: xxxx  #Redis服务器连接密码(默认为空)
    timeout: 5000  #连接超时时间(毫秒)
    lettuce:
      pool:
        max-active: 200  #连接池最大连接数(使用负值表示没有限制)
        max-wait: -1  #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 10  #连接池中的最大空闲连接
        min-idle: 0  #连接池中的最小空闲连接
  • pom依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-pool2</artifactId>
  <version>2.6.1</version>
 </dependency>

2.2 Eureka

  • yml配置
eureka:
  instance:
    instance-id: ${server.address}:${server.port}
    prefer-ip-address: true
    ip-address: ${server.address}
    lease-renewal-interval-in-seconds: 5  # 心跳,默认30秒
    lease-expiration-duration-in-seconds: 10  # 服务续约,默认90秒
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:18761/eureka/
    registry-fetch-interval-seconds: 10  # 拉取服务列表间隔,默认为30秒

三、遇到问题

  • All sentinels down, cannot determine where is mymaster master is running
    没找到配置中心,检查多环境yml

  • NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
    需要添加apache的commons-pool2

  • Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
    没找到配置中心,检查多环境yml

  • eureka不能检测服务下线,EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
    达不到心跳检测阈值,触发自我保护,增加心跳频率

  • required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
    引用RedisTemplate<String, Object>时,要去掉泛型

  • Please refer to C:\Users\admin\IdeaProjects\xxx\target\surefire-reports for the individual test results.
    添加依赖maven-surefire-plugin


https://www.xamrdz.com/backend/3uk1922277.html

相关文章: