jedis参数配置

文章目录

  • pom
  • properties
  • 配置
  • 连接池配置


pom

<!-- 引入spring-data-redis组件 -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 <!--spring boot 2.0之后默认lettuce客户端, 使⽤jedis时需要排包-->
 <exclusions>
 <exclusion>
 <groupId>io.lettuce</groupId>
 <artifactId>lettuce-core</artifactId>
 </exclusion>
 </exclusions>
</dependency>
<!-- 引入jedis依赖包 -->
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>3.6.0</version>
</dependency>

properties

单机实例配置
#redis host
spring.redis.host=<host>
#redis 端⼝号
spring.redis.port=<port>
#redis 数据库下标
spring.redis.database=0
#redis 密码
spring.redis.password=<password>
#redis 读写超时
spring.redis.timeout=2000
#是否开启连接池
spring.redis.jedis.pool.enabled=true
#连接池的最⼩连接数
spring.redis.jedis.pool.min-idle=50
#连接池的最⼤空闲连接数
spring.redis.jedis.pool.max-idle=200
#连接池的最⼤连接数
spring.redis.jedis.pool.max-active=200
#连接池耗尽后获取连接的最⼤等待时间, 默认-1表⽰⼀直等待
spring.redis.jedis.pool.max-wait=3000
#空闲连接逐出的检测周期,默认为60S
spring.redis.jedis.pool.time-between-eviction-runs=60S

主备实例配置
#redis host
spring.redis.host=<host>
#redis 端⼝号
spring.redis.port=<port>
#redis 数据库下标
spring.redis.database=0
#redis 密码
spring.redis.password=<password>
#redis 读写超时
spring.redis.timeout=2000
#是否开启连接池
spring.redis.jedis.pool.enabled=true
#连接池的最⼩连接数
spring.redis.jedis.pool.min-idle=50
#连接池的最⼤空闲连接数
spring.redis.jedis.pool.max-idle=200
#连接池的最⼤连接数
spring.redis.jedis.pool.max-active=200
#连接池耗尽后获取连接的最⼤等待时间, 默认-1表⽰⼀直等待
spring.redis.jedis.pool.max-wait=3000
#空闲连接逐出的检测周期,默认为60S
spring.redis.jedis.pool.time-between-eviction-runs=60S

Cluster实例配置
#redis cluster节点连接信息
spring.redis.cluster.nodes=<ip:port>,<ip:port>,<ip:port>
#redis cluster节点密码
spring.redis.password=<password>
#redis cluster访问最⼤重定向次数
spring.redis.cluster.max-redirects=3
#redis 读写超时
spring.redis.timeout=2000
#是否开启连接池
spring.redis.jedis.pool.enabled=true
#连接池的最⼩连接数
spring.redis.jedis.pool.min-idle=50
#连接池的最⼤空闲连接数
spring.redis.jedis.pool.max-idle=200
#连接池的最⼤连接数
spring.redis.jedis.pool.max-active=200
#连接池耗尽后获取连接的最⼤等待时间, 默认-1表⽰⼀直等待
spring.redis.jedis.pool.max-wait=3000
#空闲连接逐出的检测周期,默认为60S
spring.redis.jedis.pool.time-between-eviction-runs=60S

配置

单机实例配置
@Configuration
public class RedisConfiguration {
 @Value("${redis.host}")
 private String redisHost;
 @Value("${redis.port:6379}")
 private Integer redisPort = 6379;
 @Value("${redis.database:0}")
 private Integer redisDatabase = 0;
 @Value("${redis.password:}")
 private String redisPassword;
 @Value("${redis.connect.timeout:3000}")
 private Integer redisConnectTimeout = 3000;
 @Value("${redis.read.timeout:2000}")
 private Integer redisReadTimeout = 2000;
 @Value("${redis.pool.minSize:50}")
 private Integer redisPoolMinSize = 50;
 @Value("${redis.pool.maxSize:200}")
 private Integer redisPoolMaxSize = 200;
 @Value("${redis.pool.maxWaitMillis:3000}")
 private Integer redisPoolMaxWaitMillis = 3000;
 @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}")
 private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 *
1000;
 @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}")
 private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000;
 @Bean
 public RedisConnectionFactory
redisConnectionFactory(JedisClientConfiguration clientConfiguration) {
 RedisStandaloneConfiguration standaloneConfiguration = new
RedisStandaloneConfiguration();
 standaloneConfiguration.setHostName(redisHost);
 standaloneConfiguration.setPort(redisPort);
 standaloneConfiguration.setDatabase(redisDatabase);
 standaloneConfiguration.setPassword(redisPassword);
 return new JedisConnectionFactory(standaloneConfiguration,
clientConfiguration);
 }
@Bean
 public JedisClientConfiguration clientConfiguration() {
 JedisClientConfiguration clientConfiguration =
JedisClientConfiguration.builder()
 .connectTimeout(Duration.ofMillis(redisConnectTimeout))
 .readTimeout(Duration.ofMillis(redisReadTimeout))
 .usePooling().poolConfig(redisPoolConfig())
 .build();
 return clientConfiguration;
 }
 private JedisPoolConfig redisPoolConfig() {
 JedisPoolConfig poolConfig = new JedisPoolConfig();
 //连接池的最⼩连接数
 poolConfig.setMinIdle(redisPoolMinSize);
 //连接池的最⼤空闲连接数
 poolConfig.setMaxIdle(redisPoolMaxSize);
 //连接池的最⼤连接数
 poolConfig.setMaxTotal(redisPoolMaxSize);
 //连接池耗尽后是否需要等待, 默认true表⽰等待. 当值为true时, setMaxWait才
会⽣效
 poolConfig.setBlockWhenExhausted(true);
 //连接池耗尽后获取连接的最⼤等待时间, 默认-1表⽰⼀直等待
 poolConfig.setMaxWaitMillis(redisPoolMaxWaitMillis);
 //创建连接时校验有效性(ping),默认false
 poolConfig.setTestOnCreate(false);
 //获取连接时校验有效性(ping), 默认false, 业务量⼤时建议设置为false减少开
销
 poolConfig.setTestOnBorrow(true);
 //归还连接时校验有效性(ping),默认false, 业务量⼤时建议设置为false减少开
销
 poolConfig.setTestOnReturn(false);
 //是否开启空闲连接检测,如为false,则不剔除空闲连接
 poolConfig.setTestWhileIdle(true);
 //连接空闲多久后逐出, (空闲时间>该值 && 空闲连接>最⼤空闲数)时直接逐出, 不
再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)

poolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolSoftMinEvictableIdleTi
meMillis);
 //空闲连接逐出的检测周期,默认为60S

poolConfig.setTimeBetweenEvictionRunsMillis(redisPoolBetweenEvictionRunsMill
is);
 return poolConfig;
 }
}

主备实例配置
@Configuration
public class RedisConfiguration {
 @Value("${redis.host}")
 private String redisHost;
 @Value("${redis.port:6379}")
 private Integer redisPort = 6379;
 @Value("${redis.database:0}")
 private Integer redisDatabase = 0;
 @Value("${redis.password:}")
 private String redisPassword;
 @Value("${redis.connect.timeout:3000}")
 private Integer redisConnectTimeout = 3000;
 @Value("${redis.read.timeout:2000}")
 private Integer redisReadTimeout = 2000;
 @Value("${redis.pool.minSize:50}")
 private Integer redisPoolMinSize = 50;
 @Value("${redis.pool.maxSize:200}")
 private Integer redisPoolMaxSize = 200;
 @Value("${redis.pool.maxWaitMillis:3000}")
 private Integer redisPoolMaxWaitMillis = 3000;
 @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}")
 private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 *
1000;
 @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}")
 private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000;
 @Bean
public RedisConnectionFactory
redisConnectionFactory(JedisClientConfiguration clientConfiguration) {
 RedisStandaloneConfiguration standaloneConfiguration = new
RedisStandaloneConfiguration();
 standaloneConfiguration.setHostName(redisHost);
 standaloneConfiguration.setPort(redisPort);
 standaloneConfiguration.setDatabase(redisDatabase);
 standaloneConfiguration.setPassword(redisPassword);
 return new JedisConnectionFactory(standaloneConfiguration,
clientConfiguration);
 }
 @Bean
 public JedisClientConfiguration clientConfiguration() {
 JedisClientConfiguration clientConfiguration =
JedisClientConfiguration.builder()
 .connectTimeout(Duration.ofMillis(redisConnectTimeout))
 .readTimeout(Duration.ofMillis(redisReadTimeout))
 .usePooling().poolConfig(redisPoolConfig())
 .build();
 return clientConfiguration;
 }
 private JedisPoolConfig redisPoolConfig() {
 JedisPoolConfig poolConfig = new JedisPoolConfig();
 //连接池的最⼩连接数
 poolConfig.setMinIdle(redisPoolMinSize);
 //连接池的最⼤空闲连接数
 poolConfig.setMaxIdle(redisPoolMaxSize);
 //连接池的最⼤连接数
 poolConfig.setMaxTotal(redisPoolMaxSize);
 //连接池耗尽后是否需要等待, 默认true表⽰等待. 当值为true时, setMaxWait才
会⽣效
 poolConfig.setBlockWhenExhausted(true);
 //连接池耗尽后最⼤等待时间, 默认-1表⽰⼀直等待
 poolConfig.setMaxWaitMillis(redisPoolMaxWaitMillis);
 //创建连接时校验有效性(ping),默认false
 poolConfig.setTestOnCreate(false);
 //获取连接时校验有效性(ping), 默认false, 业务量⼤时建议设置为false减少开
销
 poolConfig.setTestOnBorrow(true);
 //归还连接时校验有效性(ping),默认false, 业务量⼤时建议设置为false减少开
销
 poolConfig.setTestOnReturn(false);
 //是否开启空闲连接检测,如为false,则不剔除空闲连接
 poolConfig.setTestWhileIdle(true);
 //连接空闲多久后逐出, (空闲时间>该值 && 空闲连接>最⼤空闲数)时直接逐出, 不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
poolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolSoftMinEvictableIdleTi
meMillis);
 //空闲连接逐出的检测周期,默认为60S
poolConfig.setTimeBetweenEvictionRunsMillis(redisPoolBetweenEvictionRunsMill
is);
 return poolConfig;
 }
}

Cluster实例配置
@Configuration
public class RedisConfiguration {
 @Value("${redis.cluster.nodes}")
 private String redisClusterNodes;
 @Value("${redis.password:}")
 private String redisPassword;
 @Value("${redis.connect.timeout:3000}")
private Integer redisConnectTimeout = 3000;
 @Value("${redis.read.timeout:2000}")
 private Integer redisReadTimeout = 2000;
 @Value("${redis.pool.minSize:50}")
 private Integer redisPoolMinSize = 50;
 @Value("${redis.pool.maxSize:200}")
 private Integer redisPoolMaxSize = 200;
 @Value("${redis.pool.maxWaitMillis:3000}")
 private Integer redisPoolMaxWaitMillis = 3000;
 @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}")
 private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 *
1000;
 @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}")
 private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000;
 @Bean
 public RedisConnectionFactory
redisConnectionFactory(JedisClientConfiguration clientConfiguration) {
 RedisClusterConfiguration clusterConfiguration = new
RedisClusterConfiguration();
 List<RedisNode> clusterNodes = new ArrayList<>();
 for (String clusterNodeStr : redisClusterNodes.split(",")) {
 String[] nodeInfo = clusterNodeStr.split(":");
 clusterNodes.add(new RedisNode(nodeInfo[0],
Integer.valueOf(nodeInfo[1])));
 }
 clusterConfiguration.setClusterNodes(clusterNodes);
 clusterConfiguration.setPassword(redisPassword);
 clusterConfiguration.setMaxRedirects(3);
 return new JedisConnectionFactory(clusterConfiguration,
clientConfiguration);
 }
 @Bean
 public JedisClientConfiguration clientConfiguration() {
 JedisClientConfiguration clientConfiguration =
JedisClientConfiguration.builder()
 .connectTimeout(Duration.ofMillis(redisConnectTimeout))
 .readTimeout(Duration.ofMillis(redisReadTimeout))
 .usePooling().poolConfig(redisPoolConfig())
 .build();
 return clientConfiguration;
}
 private JedisPoolConfig redisPoolConfig() {
 JedisPoolConfig poolConfig = new JedisPoolConfig();
 //连接池的最⼩连接数
 poolConfig.setMinIdle(redisPoolMinSize);
 //连接池的最⼤空闲连接数
 poolConfig.setMaxIdle(redisPoolMaxSize);
 //连接池的最⼤连接数
 poolConfig.setMaxTotal(redisPoolMaxSize);
 //连接池耗尽后是否需要等待, 默认true表⽰等待. 当值为true时, setMaxWait才
会⽣效
 poolConfig.setBlockWhenExhausted(true);
 //连接池耗尽后最⼤等待时间, 默认-1表⽰⼀直等待
 poolConfig.setMaxWaitMillis(redisPoolMaxWaitMillis);
 //创建连接时校验有效性(ping),默认false
 poolConfig.setTestOnCreate(false);
 //获取连接时校验有效性(ping), 默认false, 业务量⼤时建议设置为false减少开
销
 poolConfig.setTestOnBorrow(true);
 //归还连接时校验有效性(ping),默认false, 业务量⼤时建议设置为false减少开
销
 poolConfig.setTestOnReturn(false);
 //是否开启空闲连接检测,如为false,则不剔除空闲连接
 poolConfig.setTestWhileIdle(true);
 //连接空闲多久后逐出, (空闲时间>该值 && 空闲连接>最⼤空闲数)时直接逐出, 不
再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)

poolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolSoftMinEvictableIdleTi
meMillis);
 //空闲连接逐出的检测周期,默认为60S

poolConfig.setTimeBetweenEvictionRunsMillis(redisPoolBetweenEvictionRunsMill
is);
 return poolConfig;
 }
}

连接池配置

连接池的⼤⼩没有绝对的标准,建议根据业务流量进⾏合理配置,⼀般连接池⼤⼩的参数计算公
式如下
最⼩连接数 = (单机访问Redis QPS) / (1000ms / 单命令平均耗时)
最⼤连接数 = (单机访问Redis QPS) / (1000ms / 单命令平均耗时) * 150%
举例:某个业务应⽤的QPS10000左右,每个请求需访问redis10次,也意味着每秒对redis的
访问次数为100000次,同时该业务应⽤有10台机器,计算如下
单机访问Redis QPS = 100000 / 10 = 10000
单命令访问耗时 = 20ms (redis处理单命令耗时为5~10ms,遇到⽹络抖动按照15~20ms来估算)
最⼩连接数 = (10000) /1000ms / 20ms)= 200
最⼤连接数 = (10000) /1000ms / 20ms)* 150% = 300
以上计算⽅式只适⽤于⼀般业务场景,建议根据业务情况做适当调整适配

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