1 缘起

最近在看JVM相关参数时,
发现对于不知道并且不清楚含义的参数,
根本没有使用的能力,所以从参数开始逐步学起,
虽然很没有耐心,这么多参数,学了忘,忘了学,
但是,还是要看(^ v ^),于是从使用的相关组件开始学习,
之前,总结了RabbitMQ的参数,这次学习到了Redis参数,
一文是搞不懂的,需要多实践,会用才是懂,看懂不是懂。
分享如下。

2 参数汇总

结果:Redis配置参数汇总如下表:

序号

属性

描述

1

database

连接Redis的数据库索引,默认为0,Redis数据索引:0~15

2

url

Redis服务端地址,格式:redis://user:password@host:6379,已配置host,port和密码的忽略此配置

3

host

Redis主机名称

4

username

登录Redis服务端用户名

5

password

登录Redis服务端密码

6

port

Redis服务端端口

7

ssl

是否开启SSL支持

8

timeout

读超时

9

connectTimeout

连接超时

10

clientName

客户端名称

11

clientType

使用的客户端类型,默认从classpath读取,枚举为:LETTUCE和JEDIS

12

sentinel

哨兵模式

12.1

+master

主节点Redis服务

12.2

+nodes

子节点列表,格式:host:port,多个使用英文逗号隔开

12.3

+password

哨兵认证密码

13

cluster

集群模式

13.1

+nodes

集群节点列表,至少填写一个格式:host:port,多一个使用英文逗号隔开

13.2

+maxRedirects

重定向最大次数,重定向查询数据

14

jdedis

客户端Jedis连接池,简单易用的Redis客户端

14.1

+pool

14.1.1

++maxIdle

连接池中最大连接数,默认为8,当连接池中有空闲连接时,空闲对象回收线程依据timeBetweenEvictionRuns回收连接

14.1.2

++minIdle

连接池中最小空闲连接数,默认为0

14.1.3

++maxActive

连接池可用的最大连接数,默认为8

14.1.4

++maxWait

获取Redis连接的最大等待时间,默认为-1,一直等待直到获取连接

14.1.5

++timeBetweenEvictionRuns

空闲对象回收线程运行间隔

15

lettuce

客户端Lettuce连接池,高级Redis客户端,线程安全、异步

15.1

+pool

15.1.1

++maxIdle

连接池中最大连接数,默认为8,当连接池中有空闲连接时,空闲对象回收线程依据timeBetweenEvictionRuns回收连接

15.1.2

++minIdle

连接池中最小空闲连接数,默认为0

15.1.3

++maxActive

连接池可用的最大连接数,默认为8

15.1.4

++maxWait

获取Redis连接的最大等待时间,默认为-1,一直等待直到获取连接

15.1.5

++timeBetweenEvictionRuns

空闲对象回收线程运行间隔

15.2

+refresh

刷新操作

15.2.1

++dynamicRefreshSources

标识位。为获取集群拓扑是否发现并查询所有集群节点,默认为true

15.2.2

++period

集群拓扑刷新周期

15.2.3

++adaptive

标识。是否使用所有可用刷新触发器进行自适应拓扑刷新

配置样例:

spring:
  redis:
    cluster:
      nodes: 192.168.211.129:9001,192.168.211.129:9002,192.168.211.129:9003,192.168.211.129:9004,192.168.211.129:9005,192.168.211.129:9006
    password: 123456
    jedis:
      pool:
        max-active: 1 # 连接池:最大连接数,-1不限制
        max-idle: 8 # 连接池:最大空闲连接数量
        max-wait: 2000 # 连接池:最大阻塞等待时间,-1不限制,单位:毫秒
        min-idle: 0 # 连接池;最小空闲连接数量
      timeout: 1000 # 连接Redis服务器超时时间,单位:毫秒

3 参数解析

首先要找到入口。

自动装配是SpringBoot的最重要设计,

因此,先从自动装配入手,找到自动装配包,如下图所示,

Redis是数据库组件,因此在data模块中可以找到。

redisConf配置类 redis配置参数_java


data包如下图所示,可以看到redis组件,

属性类为:RedisProperties。

redisConf配置类 redis配置参数_java_02

声明:

SpringBoot版本:2.4.5

3.1 Redis属性类

Redis属性类源码如下图所示,该类包括所有Redis配置参数,

后文的所有参数解析均基于该类。

位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties

redisConf配置类 redis配置参数_redisConf配置类_03

3.2 参数

这层参数即最外层的属性,如下表所示,
有的属性具有第二甚至第三层属性,如jedis和lettuce。

序号

属性

描述

1

database

连接Redis的数据库索引,默认为0,Redis数据索引:0~15

2

url

Redis服务端地址,格式:redis://user:password@host:6379,已配置host,port和密码的忽略此配置

3

host

Redis主机名称

4

username

登录Redis服务端用户名

5

password

登录Redis服务端密码

6

port

Redis服务端端口

7

ssl

是否开启SSL支持

8

timeout

读超时

9

connectTimeout

连接超时

10

clientName

客户端名称

11

clientType

使用的客户端类型,默认从classpath读取,枚举为:LETTUCE和JEDIS

12

sentinel

哨兵模式

13

cluster

集群模式

14

jdedis

客户端Jedis连接池,简单易用的Redis客户端

15

lettuce

客户端Lettuce连接池,高级Redis客户端,线程安全、异步

3.2.1 clientType

Redis客户端类型,源码提供的类型有:LETTUCE和JEDIS,源码如下图所示。

位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties.ClientType

redisConf配置类 redis配置参数_spring boot_04

3.2.2 cluster

Redis客户端采用集群方式:cluster,源码如下图所示,
集群参数:

序号

属性

描述

1

nodes

集群节点列表,至少填写一个格式:host:port,多一个使用英文逗号隔开

2

maxRedirects

重定向最大次数,重定向查询数据

位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster

redisConf配置类 redis配置参数_redis_05

3.2.3 sentinel

Redis客户端采用哨兵方式:sentinel,源码如下图所示,
参数如下:

序号

属性

描述

1

master

主节点Redis服务

2

nodes

子节点列表,格式:host:port,多个使用英文逗号隔开

3

password

哨兵认证密码

org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel

redisConf配置类 redis配置参数_Redis_06

3.2.4 Jedis

简单易用的Redis客户端(Java版)。同步阻塞IO、不支持异步,
源码位置:https://github.com/redis/jedis

redisConf配置类 redis配置参数_java_07

Jedis客户端属性如下表所示,源码如下图所示,
由源码可知,Jedis属性只有Pool
位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool

序号

属性

描述

1

maxIdle

连接池中最大连接数,默认为8,当连接池中有空闲连接时,空闲对象回收线程依据timeBetweenEvictionRuns回收连接

2

minIdle

连接池中最小空闲连接数,默认为0

3

maxActive

连接池可用的最大连接数,默认为8

4

maxWait

获取Redis连接的最大等待时间,默认为-1,一直等待直到获取连接

5

timeBetweenEvictionRuns

空闲对象回收线程运行间隔

位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties.Jedis

redisConf配置类 redis配置参数_redis_08

Pool源码如下:

/**
	 * Pool properties.
	 */
	public static class Pool {

		/**
		 * Maximum number of "idle" connections in the pool. Use a negative value to
		 * indicate an unlimited number of idle connections.
		 */
		private int maxIdle = 8;

		/**
		 * Target for the minimum number of idle connections to maintain in the pool. This
		 * setting only has an effect if both it and time between eviction runs are
		 * positive.
		 */
		private int minIdle = 0;

		/**
		 * Maximum number of connections that can be allocated by the pool at a given
		 * time. Use a negative value for no limit.
		 */
		private int maxActive = 8;

		/**
		 * Maximum amount of time a connection allocation should block before throwing an
		 * exception when the pool is exhausted. Use a negative value to block
		 * indefinitely.
		 */
		private Duration maxWait = Duration.ofMillis(-1);

		/**
		 * Time between runs of the idle object evictor thread. When positive, the idle
		 * object evictor thread starts, otherwise no idle object eviction is performed.
		 */
		private Duration timeBetweenEvictionRuns;
		// setter/getter ommited
	}

3.2.5 Lettuce

高级Redis客户端(Java版),线程安全且支持异步(基于Netty事件驱动)。
支持高级Redis特性:哨兵、集群、管道、自动重连和Redis数据模型等。
Lettuce

redisConf配置类 redis配置参数_java_09

Lettuce属性如下表所示,源码如下图所示。
其中,Pool与Jedis相同。
shutdownTimeout为关闭客户端及关闭所有打开的连接的超时时间。一旦关闭所有连接,相关的客户端资源会在安静期和关闭超时期优雅地关闭或释放。
cluster属性,由源码可知,cluster中嵌套了refresh属性,
refresh属性如下:

序号

属性

描述

1

dynamicRefreshSources

标识位。为获取集群拓扑是否发现并查询所有集群节点,默认为true

2

period

集群拓扑刷新周期

3

adaptive

标识。是否使用所有可用刷新触发器进行自适应拓扑刷新

位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce

redisConf配置类 redis配置参数_java_10

Lettuce中的Cluster源码如下所示:

public static class Cluster {

			private final Refresh refresh = new Refresh();

			public Refresh getRefresh() {
				return this.refresh;
			}

			public static class Refresh {

				/**
				 * Whether to discover and query all cluster nodes for obtaining the
				 * cluster topology. When set to false, only the initial seed nodes are
				 * used as sources for topology discovery.
				 */
				private boolean dynamicRefreshSources = true;

				/**
				 * Cluster topology refresh period.
				 */
				private Duration period;

				/**
				 * Whether adaptive topology refreshing using all available refresh
				 * triggers should be used.
				 */
				private boolean adaptive;

				public boolean isDynamicRefreshSources() {
					return this.dynamicRefreshSources;
				}

				public void setDynamicRefreshSources(boolean dynamicRefreshSources) {
					this.dynamicRefreshSources = dynamicRefreshSources;
				}

				public Duration getPeriod() {
					return this.period;
				}

				public void setPeriod(Duration period) {
					this.period = period;
				}

				public boolean isAdaptive() {
					return this.adaptive;
				}

				public void setAdaptive(boolean adaptive) {
					this.adaptive = adaptive;
				}

			}

		}

4 小结

核心:
(1)支持的客户端连接方式:直连、集群、哨兵;
(2)Redis客户端连接池类型:Jedis和Lettuce;
(3)Jedis简单易用的Redis客户端;Lettuce线程安全、异步的高性能Redis客户端;
(3)Jedis和Lettuce通用连接池配置:最大空闲连接、最小空闲连接等,通过timeBetweenEvictionRuns控制回收空闲连接的频率;
(4)防止获取连接阻塞,必须设置maxWait最大等待时间。