1. Spring Boot Redis 集群性能优化(基于 Redisson)
1.1. 版本说明
构件 | 版本 |
---|---|
spring-boot | 2.6.13 |
spring-cloud | 2021.0.5 |
spring-cloud-alibaba | 2021.0.5.0 |
redisson-spring-boot-starter | 3.29.0 |
redisson-spring-data-26 | 3.29.0 |
1.2. 为什么是 Redisson
Redisson 提供了 Lettuce、Jedis 等 Java 客户端的所有功能,同时还提供了更丰富的分布式功能,并且与 Spring Boot、Hibernate、MyBatis 等更多框架无缝集成。
1.3. 参数优化
redisson-spring-boot-starter 支持 3 种配置方式:
- 基于
org.springframework.boot.autoconfigure.data.redis.RedisProperties
类配置。 - 由
spring.redis.redisson.config
指定 Redisson 自身的配置文本。 - 由
spring.redis.redisson.file
指定 Redisson 自身的配置文件位置。
方式 1 只能配置少数 Redis 参数,其他线程数量、超时时间等关键参数无法配置,只适用于开发环境使用;
方式 2 指定了一长串 yaml 配置字符串,不易阅读和配置;
方式 3 指定了一个本地 yaml 配置文件,对于很多使用 Nacos 作为配置中心的项目来说非常不方便;
以上 3 种方式都不适合在项目中使用,因此后续会在方式 3 项目上进行改造,使支持从 Nacos 读取配置。
1.3.1. Redisson 配置参数
1.3.1.1. 通用参数
threads
:线程池数量。这个线程池数量被所有 RTopic 对象监听器,RRemoteService 调用者和 RExecutorService 任务共同共享。默认值:16。nettyThreads
:Netty 线程池数量。这个线程池数量是在一个 Redisson 实例内,被其创建的所有分布式数据类型和服务,以及底层客户端所一同共享的线程池里保存的线程数量。默认值:32。nettyExecutor
:Use external ExecutorService which is used by Netty for Redis response decoding and command sending. 默认值:null。codec
:Redisson 的对象编码类是用于将对象进行序列化和反序列化,以实现对该对象在 Redis 里的读取和存储。默认值:!<org.redisson.codec.Kryo5Codec> {}
。executor
:单独提供一个用来执行所有 RTopic 对象监听器,RRemoteService 调用者和 RExecutorService 任务的线程池(ExecutorService)实例。默认值:null。transportMode
:传输模式。默认值:NIO。eventLoopGroup
:用于特别指定一个 EventLoopGroup. EventLoopGroup 是用来处理所有通过 Netty 与 Redis 服务之间的连接发送和接受的消息。每一个 Redisson 都会在默认情况下自己创建管理一个 EventLoopGroup 实例。因此,如果在同一个 JVM 里面可能存在多个 Redisson 实例的情况下,采取这个配置实现多个 Redisson 实例共享一个 EventLoopGroup 的目的。默认值:null。lockWatchdogTimeout
:监控锁的看门狗超时时间单位为毫秒。该参数只适用于分布式锁的加锁请求中未明确使用 leaseTimeout 参数的情况。如果该看门口未使用 lockWatchdogTimeout 去重新调整一个分布式锁的 lockWatchdogTimeout 超时,那么这个锁将变为失效状态。这个参数可以用来避免由 Redisson 客户端节点宕机或其他原因造成死锁的情况。默认值:30000。checkLockSyncedSlaves
:Defines whether to check synchronized slaves amount with actual slaves amount after lock acquisition。默认值:true。slavesSyncTimeout
:Defines slaves synchronization timeout in milliseconds applied to each operation of RLock, RSemaphore, RPermitExpirableSemaphore objects。默认值:1000。reliableTopicWatchdogTimeout
:Reliable Topic watchdog timeout in milliseconds. Reliable Topic subscriber expires after timeout if watchdog didn't extend it to next timeout time interval. This prevents against infinity grow of stored messages in topic due to Redisson client crush or any other reason when subscriber can't consumer messages anymore。默认值:600000。keepPubSubOrder
:通过该参数来修改是否按订阅发布消息的接收顺序出来消息,如果选否将对消息实行并行处理,该参数只适用于订阅发布消息的情况。默认值:true。useScriptCache
:Defines whether to use Lua-script cache on Redis side. Most Redisson methods are Lua-script based and this setting turned on could increase speed of such methods execution and save network traffic。默认值:false。minCleanUpDelay
:Defines minimum delay in seconds for clean up process of expired entries. Applied toJCache
,RSetCache
,RClusteredSetCache
,RMapCache
,RListMultimapCache
,RSetMultimapCache
,RLocalCachedMapCache
,RClusteredLocalCachedMapCache
objects.。默认值:5。maxCleanUpDelay
:Defines maximum delay in seconds for clean up process of expired entries. Applied toJCache
,RSetCache
,RClusteredSetCache
,RMapCache
,RListMultimapCache
,RSetMultimapCache
,RLocalCachedMapCache
,RClusteredLocalCachedMapCache
objects.。默认值:1800。cleanUpKeysAmount
:Defines expired keys amount deleted per single operation during clean up process of expired entries. Applied toJCache
,RSetCache
,RClusteredSetCache
,RMapCache
,RListMultimapCache
,RSetMultimapCache
,RLocalCachedMapCache
,RClusteredLocalCachedMapCache
objects.。默认值:100。nettyHook
:Netty hook applied to Netty Bootstrap and Channel objects.。默认值:!<org.redisson.client.DefaultNettyHook> {}
。connectionListener
:Connection listener which is triggered when Redisson connected/disconnected to Redis server。默认值:null。useThreadClassLoader
:Defines whether to supply Thread ContextClassLoader to Codec. Usage of Thread.getContextClassLoader() may resolve ClassNotFoundException error arise during Redis response decoding. This error might arise if Redisson is used in both Tomcat and deployed application。默认值:true。addressResolverGroupFactory
:Allows to specify customized implementation ofio.netty.resolver.dns.DnsAddressResolverGroup
。默认值:!<org.redisson.connection.SequentialDnsAddressResolverFactory> {}
。
Available implementations:org.redisson.connection.DnsAddressResolverGroupFactory
- uses default DNS servers list provided by OS.org.redisson.connection.SequentialDnsAddressResolverFactory
- uses default DNS servers list provided by OS and allows to control concurrency level of requests to DNS servers.org.redisson.connection.RoundRobinDnsAddressResolverGroupFactory
- uses default DNS servers list provided by OS in round robin mode.
lazyInitialization
:Defines whether Redisson connects to Redis only when first Redis call is made and not during Redisson instance creation。默认值:false。true
- connects to Redis only when first Redis call is madefalse
- connects to Redis during Redisson instance creation。
protocol
:Defines Redis protocol version. Available values: RESP2, RESP3。默认值:RESP2。
1.3.1.2. 集群参数
集群参数前缀:clusterServersConfig
idleConnectionTimeout
:如果当前连接池里的连接数量超过了最小空闲连接数,而同时有连接空闲时间超过了该数值,那么这些连接将会自动被关闭,并从连接池里去掉。时间单位是毫秒。默认值:10000。connectTimeout
:同任何节点建立连接时的等待超时。时间单位是毫秒。默认值:10000。timeout
:等待节点回复命令的时间。该时间从命令发送成功时开始计时。默认值:3000。subscriptionTimeout
:Defines subscription timeout in milliseconds applied per channel subscription。默认值:7500。retryAttempts
:如果尝试达到 retryAttempts(命令失败重试次数)仍然不能将命令发送至某个指定的节点时,将抛出错误。如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时)计时。默认值:3。retryInterval
:在某个节点执行相同或不同命令时,连续 失败 failedAttempts(执行失败最大次数)时,该节点将被从可用节点列表里清除,直到 reconnectionTimeout(重新连接时间间隔)超时以后再次尝试。默认值:1500。password
:用于节点身份验证的密码。默认值:null。username
:Username for Redis server authentication. Requires Redis 6.0+。默认值:null。credentialsResolver
:Defines Credentials resolver which is invoked during connection for Redis server authentication. Returns Credentials object per Redis node address, it containsusername
andpassword
fields. Allows to specify dynamically changing Redis credentials。默认值:!<org.redisson.client.DefaultCredentialsResolver> {}
。subscriptionsPerConnection
:每个连接的最大订阅数量。默认值:5。clientName
:在 Redis 节点里显示的客户端名称。默认值:null。sslEnableEndpointIdentification
:开启 SSL 终端识别能力。默认值:true。sslProvider
:确定采用哪种方式(JDK 或 OPENSSL)来实现 SSL 连接。默认值:JDK。sslTruststore
:Defines path to SSL truststore. It's stores certificates which is used to identify server side of SSL connection. SSL truststore is read on each new connection creation and can be dynamically reloaded。默认值:null。sslTruststorePassword
:指定 SSL 信任证书库的密码。默认值:null。sslKeystore
:Defines path to SSL keystore. It's stores private key and certificates corresponding to their public keys. Used if server side of SSL connection requires client authentication. SSL keystore is read on each new connection creation and can be dynamically reloaded。默认值:null。sslKeystorePassword
:指定 SSL 钥匙库的密码。默认值:null。sslProtocols
:Defines array of allowed SSL protocols. Example values: TLSv1.3, TLSv1.2, TLSv1.1, TLSv1。默认值:null。pingConnectionInterval
:This setting allows to detect and reconnect broken connections using PING command. PING command sending interval defined in milliseconds. Useful in cases when netty lib doesn't invokechannelInactive
method for closed connections. Set0
to disable。默认值:30000。keepAlive
:Enables TCP keepAlive for connection。默认值:false。tcpKeepAliveCount
:Defines the maximum number of keepalive probes TCP should send before dropping the connection. 0 value means use system default setting。默认值:0。tcpKeepAliveIdle
:Defines the time in seconds the connection needs to remain idle before TCP starts sending keepalive probes. 0 value means use system default setting。默认值:0。tcpKeepAliveInterval
:Defines the time in seconds between individual keepalive probes. 0 value means use system default setting。默认值:0。tcpUserTimeout
:Defines the maximum amount of time in milliseconds that transmitted data may remain unacknowledged, or buffered data may remain untransmitted (due to zero window size) before TCP will forcibly close the connection. 0 value means use system default setting。默认值:0。tcpNoDelay
:Enables TCP noDelay for connection。默认值:true。nameMapper
:Defines Name mapper which maps Redisson object name to a custom name. Applied to all Redisson objects。默认值:!<org.redisson.api.DefaultNameMapper> {}
。commandMapper
:Defines Command mapper which maps Redis command name to a custom name. Applied to all Redis commands。默认值:!<org.redisson.config.DefaultCommandMapper> {}
。loadBalancer
:Сonnection load balancer for multiple Redis servers.。默认值:!<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
。
Available implementations:org.redisson.connection.balancer.CommandsLoadBalancer
org.redisson.connection.balancer.WeightedRoundRobinBalancer
org.redisson.connection.balancer.RoundRobinLoadBalancer
org.redisson.connection.balancer.RandomLoadBalancer
slaveConnectionMinimumIdleSize
:Redis 'slave' node minimum idle connection amount for each slave node。默认值:24。slaveConnectionPoolSize
:Redis 'slave' node maximum connection pool size for each slave node。默认值:64。failedSlaveReconnectionInterval
:Interval of Redis Slave reconnection attempt when it was excluded from internal list of available servers. On each timeout event Redisson tries to connect to disconnected Redis server. Value in milliseconds。默认值:3000。masterConnectionMinimumIdleSize
:Minimum idle connections amount per Redis master node。默认值:24。masterConnectionPoolSize
:Redis 'master' node maximum connection pool size。默认值:64。readMode
:Set node type used for read operation。默认值:SLAVE。
Available values:SLAVE
- Read from slave nodes, uses MASTER if no SLAVES are available,MASTER
- Read from master node,MASTER_SLAVE
- Read from master and slave nodes
subscriptionMode
:Set node type used for subscription operation。默认值:MASTER。
Available values:SLAVE
- Subscribe to slave nodesMASTER
- Subscribe to master node
subscriptionConnectionMinimumIdleSize
:Minimum idle connection pool size for subscription (pub/sub) channels. Used byRTopic
,RPatternTopic
,RLock
,RSemaphore
,RCountDownLatch
,RClusteredLocalCachedMap
,RClusteredLocalCachedMapCache
,RLocalCachedMap
,RLocalCachedMapCache
objects and Hibernate Local Cached Region Factories。默认值:1。subscriptionConnectionPoolSize
:Maximum connection pool size for subscription (pub/sub) channels. Used byRTopic
,RPatternTopic
,RLock
,RSemaphore
,RCountDownLatch
,RClusteredLocalCachedMap
,RClusteredLocalCachedMapCache
,RLocalCachedMap
,RLocalCachedMapCache
objects and Hibernate Local Cached Region Factories。默认值:50。dnsMonitoringInterval
:Interval in milliseconds to check the endpoint's DNS. Applications must ensure the JVM DNS cache TTL is low enough to support this. Set -1 to disable。默认值:5000。failedSlaveNodeDetector
:Defines failed Redis Slave node detector object which implements failed node detection logic via org.redisson.client.FailedNodeDetector interface。默认值:!<org.redisson.client.FailedConnectionDetector> {}
。
Available implementations:org.redisson.client.FailedConnectionDetector
- marks Redis node as failed if it has ongoing connection errors in defined checkInterval interval in milliseconds. Default is 180000 milliseconds.org.redisson.client.FailedCommandsDetector
- marks Redis node as failed if it has certain amount of command execution errors defined by failedCommandsLimit in defined checkInterval interval in milliseconds.org.redisson.client.FailedCommandsTimeoutDetector
- marks Redis node as failed if it has certain amount of command execution timeout errors defined by failedCommandsLimit in defined checkInterval interval in milliseconds.
natMapper
:Defines NAT mapper interface which maps Redis URI object and applied to all Redis connections. Can be used to map internal Redis server IPs to external ones. Available implementations:org.redisson.api.HostPortNatMapper
andorg.redisson.api.HostNatMapper
。默认值:!<org.redisson.api.DefaultNatMapper> {}
。nodeAddresses
:Add Redis cluster node or Redis endpoint addresss inhost:port
format. Redisson discovers automatically cluster topology. Userediss://
protocol for SSL connection。默认值:null。scanInterval
:Scan interval in milliseconds. Applied to Redis clusters topology scan。默认值:5000。checkSlotsCoverage
:Enables cluster slots check during Redisson startup。默认值:true。shardedSubscriptionMode
:Defines whether to use sharded subscription feature available in Redis 7.0+. Used byRMapCache
,RLocalCachedMap
,RCountDownLatch
,RLock
,RPermitExpirableSemaphore
,RSemaphore
,RLongAdder
,RDoubleAdder
,Micronaut Session
,Apache Tomcat Manager
objects。默认值:AUTO。
1.3.1.3. 最终参数配置
threads: 32
nettyThreads: 64
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode: "NIO"
checkLockSyncedSlaves: false
clusterServersConfig:
idleConnectionTimeout: 60000
connectTimeout: 10000
timeout: 6000
retryAttempts: 3
retryInterval: 1500
password: 123456
pingConnectionInterval: 30000
keepAlive: true
tcpNoDelay: true
subscriptionsPerConnection: 5
clientName: demo
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
slaveConnectionMinimumIdleSize: 24
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 24
masterConnectionPoolSize: 64
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
failedSlaveReconnectionInterval: 3000
readMode: "SLAVE"
subscriptionMode: "MASTER"
scanInterval: 15000
nodeAddresses:
- "redis://127.0.0.1:7001"
- "redis://127.0.0.1:7002"
- "redis://127.0.0.1:7003"
- "redis://127.0.0.1:7004"
- "redis://127.0.0.1:7005"
- "redis://127.0.0.1:7006"
1.4. 从 Nacos 获取 Redisson 配置
-
在 Nacos 控制台页面新增配置
- 命名空间:
redisson-demo
- Data ID:
redisson-dev.yaml
- Group:
redisson-demo
- 配置内容:参考上一章节“最终参数配置”
- 命名空间:
-
项目 application.yaml 配置
spring:
application:
name: redisson-demo
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
namespace: redisson-demo
group: redisson-demo
file-extension: yaml
username: nacos
password: nacos
config:
import: nacos:redisson-demo.yaml
- 从 Nacos 获取配置实例化 RedissonClient
import com.alibaba.cloud.nacos.NacosConfigManager;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class RedissonConfiguration {
@Value("${spring.profiles.active}")
private String profile;
@Resource
private NacosConfigManager nacosConfigManager;
@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() throws Exception {
String redissonYaml = nacosConfigManager.getConfigService().getConfig(String.format("redisson-%s.yaml", profile), "redisson-demo", 5000L);
Config config = Config.fromYAML(redissonYaml);
return Redisson.create(config);
}
}
标签:Redisson,Spring,Redis,connection,redisson,默认值,Defines
From: https://www.cnblogs.com/jason207010/p/18289596