redis主从基于sentinel哨兵项目文件配置(基于springboot)

  1. 两种方式: 一种是基于yml方式,一种是基于java config
  2. 基于maven的pom依赖,哨兵模式在redisson版本>=3.12.5和<3.15.0有问题,项目启动的时候会报错Command execution timeout for command: (SENTINEL SENTINELS) :
    https://github.com/redisson/redisson/issues/3404
    like #3283
    through the test,:
    this not happen in 3.12.4
    this will happen in 3.12.5 or higher
    My current version is 3.15.0, and I use this configuration so that it doesn’t report errors:
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>3.12.4</version>
            </dependency>
  1. yml的配置方式:
spring:
 redis:
   redisson:
     # 配置模式
     config: classpath:redisson-sentinel.yml
           
redisson-sentinel.yml中的内容:

sentinelServersConfig:
 idleConnectionTimeout: 10000
 connectTimeout: 5000
 timeout: 5000
 retryAttempts: 3
 retryInterval: 1000
 password: 1234
 subscriptionsPerConnection: 200
 clientName: null
 loadBalancer: ! {}
 slaveConnectionMinimumIdleSize: 5
 slaveConnectionPoolSize: 250
 masterConnectionMinimumIdleSize: 5
 masterConnectionPoolSize: 250
 readMode: "SLAVE"
 subscriptionMode: SLAVE
 # 26379 26380 26381是sentinel中的端口,不是redis的端口
 sentinelAddresses:
   - "redis://127.0.0.1:26379"
   - "redis://127.0.0.1:26380"
   - "redis://127.0.0.1:26381"
   - #masterName是sentinel中定义的sentinel monitor 
 masterName: "bidMaster"
 database: 0
threads: 0
codec: ! {}
eventLoopGroup: null 
     
  1. 基于java config的方式:
配置文件:
 redisson:
   database: 0
   password: '1234'
   #2、哨兵模式
   master-name: bidMaster
   sentinel-addresses:   
     - "redis://127.0.0.1:26379"
     - "redis://127.0.0.1:26380"
     - "redis://127.0.0.1:26381"
   
 
java config类:

 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 *   redisson配置  
*

*
*

*
* * @author : * @date Date : * 修改人 修改日期 修改描述
* -------------------------------------------
*
*
*/
@Data @ConfigurationProperties(prefix = "redisson") public class RedissonProperties { private String address; private String password; private int slaveConnectionPoolSize = 250; private int masterConnectionPoolSize = 250; private String[] sentinelAddresses; private String masterName; private int database = 0; /** * 连接空闲超时,单位:毫秒 */ private int idleConnectionTimeout = 10000; /** * 连接超时,单位:毫秒 */ private int connectTimeout = 10000; /** * 命令等待超时,单位:毫秒 */ private int timeout = 3000; /** * 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。 * 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。 */ private int retryAttempts = 3; /** * 命令重试发送时间间隔,单位:毫秒 */ private int retryInterval = 1500; /** * 重新连接时间间隔,单位:毫秒 */ private int reconnectionTimeout = 3000; /** * 执行失败最大次数 */ private int failedAttempts = 3000; private int subscriptionsPerConnection = 200; /** * 客户端名称 */ private String clientName; /** * 发布和订阅连接的最小空闲连接数 */ private int subscriptionConnectionMinimumIdleSize = 1; /** * 发布和订阅连接池大小 */ private int subscriptionConnectionPoolSize = 200; /** * 最小空闲连接数 */ private int connectionMinimumIdleSize = 32; /** * 连接池大小 */ private int connectionPoolSize = 64; /** * DNS监测时间间隔,单位:毫秒 */ private int dnsMonitoringInterval = 5000; private int slaveConnectionMinimumIdleSize = 5; private int masterConnectionMinimumIdleSize = 5; private String readMode = "SLAVE"; private String subscriptionMode = "SLAVE"; } import cn.hutool.core.bean.BeanUtil; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.codec.JsonJacksonCodec; import org.redisson.config.Config; import org.redisson.config.SentinelServersConfig; import org.redisson.config.SingleServerConfig; import org.redisson.config.TransportMode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * redisson客户端配置
*

*
*

*
* * @author : * @date Date : * 修改人 修改日期 修改描述
* -------------------------------------------
*
*
*/
@Configuration @ConditionalOnClass(Config.class) @EnableConfigurationProperties(RedissonProperties.class) public class RedissonConfiguration { @Autowired private RedissonProperties redissonProperties; /** * 哨兵模式 * * @return */ @Bean(destroyMethod = "shutdown") @ConditionalOnProperty(name = "redisson.master-name") public RedissonClient redissonSentinel() { Config config = new Config(); config.setTransportMode(TransportMode.NIO); config.setCodec(new JsonJacksonCodec()); SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); BeanUtil.copyProperties(redissonProperties, sentinelServersConfig); return Redisson.create(config); } }
  1. 项目启动即可

我的公众号,欢迎关注一起探讨开发技术

我的公众号

你可能感兴趣的:(redis,spring,boot,java)