redisson参数配置

文章目录

  • pom
  • 配置
  • 链接配置建议


pom

<!-- 引入spring-data-redis组件 -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 <exclusions>
 <!-- 因springboot2.x中默认集成了lettuce,因此需要排掉该依赖 -->
 <exclusion>
 <artifactId>lettuce-core</artifactId>
 <groupId>io.lettuce</groupId>
 </exclusion>
 </exclusions>
</dependency>
<!-- 引入redisson对springboot的集成适配包 -->
<dependency>
 <groupId>org.redisson</groupId>
 <artifactId>redisson-spring-boot-starter</artifactId>
 <version>${redisson.version}</version>
</dependency>

配置

@Configuration
public class RedisConfiguration {
 @Value("${redis.address}")
 private String redisAddress;
 @Value("${redis.password:}")
 private String redisPassword;
 @Value("${redis.database:0}")
 private Integer redisDatabase = 0;
 @Value("${redis.connect.timeout:3000}")
 private Integer redisConnectTimeout = 3000;
 @Value("${redis.cluster.address}")
 private List<String> redisClusterAddress;
 @Value("${redis.cluster.scan.interval:5000}")
 private Integer redisClusterScanInterval = 5000;
 @Value("${redis.password:}")
 private String redisPassword;
 @Value("${redis.connect.timeout:3000}")
 private Integer redisConnectTimeout = 3000;
 @Value("${redis.connection.idle.timeout:10000}")
 private Integer redisConnectionIdleTimeout = 10000;
 @Value("${redis.connection.ping.interval:1000}")
 private Integer redisConnectionPingInterval = 1000;
 @Value("${redis.timeout:2000}")
 private Integer timeout = 2000;
 @Value("${redis.retry.attempts:3}")
 private Integer redisRetryAttempts = 3;
 @Value("${redis.retry.interval:200}")
 private Integer redisRetryInterval = 200;
 @Value("${redis.master.connection.pool.min.size:50}")
 private Integer redisMasterConnectionPoolMinSize = 50;
 @Value("${redis.master.connection.pool.max.size:200}")
 private Integer redisMasterConnectionPoolMaxSize = 200;
  @Value("${redis.connection.idle.timeout:10000}")
 private Integer redisConnectionIdleTimeout = 10000;
 @Value("${redis.connection.ping.interval:1000}")
 private Integer redisConnectionPingInterval = 1000;
 @Value("${redis.timeout:2000}")
 private Integer timeout = 2000;
 @Value("${redis.connection.pool.min.size:50}")
 private Integer redisConnectionPoolMinSize;
 @Value("${redis.connection.pool.max.size:200}")
 private Integer redisConnectionPoolMaxSize;
 @Value("${redis.retry.attempts:3}")
 private Integer redisRetryAttempts = 3;
 @Value("${redis.retry.interval:200}")
 private Integer redisRetryInterval = 200;

单机
@Bean
 public RedissonConnectionFactory redissonConnectionFactory(Config
redissonSingleServerConfig) {
 return new RedissonConnectionFactory(redissonSingleServerConfig);
 }
 @Bean
 public Config redissonSingleServerConfig() {
 Config redissonConfig = new Config();
 SingleServerConfig serverConfig = redissonConfig.useSingleServer();
 serverConfig.setAddress(redisAddress);
 serverConfig.setConnectionMinimumIdleSize(redisConnectionPoolMinSize);
 serverConfig.setConnectionPoolSize(redisConnectionPoolMaxSize);
 serverConfig.setDatabase(redisDatabase);
 serverConfig.setPassword(redisPassword);
 serverConfig.setConnectTimeout(redisConnectTimeout);
 serverConfig.setIdleConnectionTimeout(redisConnectionIdleTimeout);
 serverConfig.setPingConnectionInterval(redisConnectionPingInterval);
 serverConfig.setTimeout(timeout);
 serverConfig.setRetryAttempts(redisRetryAttempts);
 serverConfig.setRetryInterval(redisRetryInterval);
 redissonConfig.setCodec(new JsonJacksonCodec());
 return redissonConfig;
 }

主备
@Bean
 public RedissonConnectionFactory redissonConnectionFactory(Config
redissonMasterSlaveServersConfig) {
 return new RedissonConnectionFactory(redissonMasterSlaveServersConfig);
 }
 @Bean
 public Config redissonMasterSlaveServersConfig() {
 Config redissonConfig = new Config();
 MasterSlaveServersConfig serverConfig = redissonConfig.useMasterSlaveServers();
 serverConfig.setMasterAddress(redisMasterAddress);
 serverConfig.setDatabase(redisDatabase);
 serverConfig.setPassword(redisPassword);
 serverConfig.setMasterConnectionMinimumIdleSize(redisMasterConnectionPoolMinSize);
 serverConfig.setMasterConnectionPoolSize(redisMasterConnectionPoolMaxSize);
 serverConfig.setReadMode(ReadMode.MASTER_SLAVE);
 serverConfig.setSubscriptionMode(SubscriptionMode.MASTER);
 serverConfig.setConnectTimeout(redisConnectTimeout);
 serverConfig.setIdleConnectionTimeout(redisConnectionIdleTimeout);
 serverConfig.setPingConnectionInterval(redisConnectionPingInterval);
 serverConfig.setTimeout(timeout);
 serverConfig.setRetryAttempts(redisRetryAttempts);
 serverConfig.setRetryInterval(redisRetryInterval);
 redissonConfig.setCodec(new JsonJacksonCodec());
     return redissonConfig;
 }

集群
@Bean
 public RedissonConnectionFactory redissonConnectionFactory(Config
redissonClusterServersConfig) {
 return new RedissonConnectionFactory(redissonClusterServersConfig);
 }
 @Bean
 public Config redissonClusterServersConfig() {
 Config redissonConfig = new Config();
 ClusterServersConfig serverConfig = redissonConfig.useClusterServers();
 serverConfig.setNodeAddresses(redisClusterAddress);
 serverConfig.setScanInterval(redisClusterScanInterval);
 serverConfig.setPassword(redisPassword);
 serverConfig.setMasterConnectionMinimumIdleSize(redisMasterConnectionPoolMinSize);
 serverConfig.setMasterConnectionPoolSize(redisMasterConnectionPoolMaxSize);
 serverConfig.setReadMode(ReadMode.MASTER);
 serverConfig.setSubscriptionMode(SubscriptionMode.MASTER);
 serverConfig.setConnectTimeout(redisConnectTimeout);
 serverConfig.setIdleConnectionTimeout(redisConnectionIdleTimeout);
 serverConfig.setPingConnectionInterval(redisConnectionPingInterval);
 serverConfig.setTimeout(timeout);
 serverConfig.setRetryAttempts(redisRetryAttempts);
 serverConfig.setRetryInterval(redisRetryInterval);
 redissonConfig.setCodec(new JsonJacksonCodec());
 return redissonConfig;
 }
}

链接配置建议

读取模式-readMode
建议采⽤ MASTER,即Master节点承担所有的读写流量,⼀⽅⾯避免数据因主从同步时延带来的⼀致性问题;另⼀⽅⾯,
如从节点故障,配置值=SLAVE,所有读请求会触发报错;配置值=MASTER_SLAVE,部分读请求会触发异常。读报错会持
续failedSlaveCheckInterval(默认30S)时间,直⾄从可⽤节点列表中摘除。
如需读写流量分流处理,DCS服务提供了针对读写流量分流的实例类型-读写分离,通过在中间架设代理节点实现读写流
量分发,遇到从节点故障时,⾃动切流⾄主节点,对业务应⽤⽆感知,且故障感知时间窗⼝远⼩于redisson内部的时间
窗⼝。
订阅模式-subscriptionMode
建立采⽤ MASTER,原理同上
连接池配置
连接池的⼤⼩没有绝对的标准,建议根据业务流量进⾏合理配置,⼀般连接池⼤⼩的参数计算公式如下
最⼩连接数 = (单机访问Redis QPS) / (1000ms / 单命令平均耗时)
最⼤连接数 = (单机访问Redis QPS) / (1000ms / 单命令平均耗时) * 150%
举例:某个业务应⽤的QPS为10000左右,每个请求需访问redis10次,也意味着每秒对redis的访问次数为100000次,
同时该业务应⽤有10台机器,计算如下
单机访问Redis QPS = 100000 / 10 = 10000
单命令访问耗时 = 20ms (redis处理单命令耗时为510ms,遇到⽹络抖动按照1520ms来估算)
最⼩连接数 = (10000) / (1000ms / 20ms)= 200
最⼤连接数 = (10000) / (1000ms / 20ms)* 150% = 300
以上计算⽅式只适⽤于⼀般业务场景,建议根据业务情况做适当调整适配
重试配置
redisson中⽀持重试配置,主要是如下两个参数,建议根据业务情况配置合理值,⼀般重试次数为3,重试间隔为200ms
左右
retryAttemps - 配置重试次数
retryInterval - 配置重试时间间隔

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