Spring Boot Redis 集群性能优化(基于 Redisson)

news/2024/10/6 23:35:04/文章来源:https://www.cnblogs.com/jason207010/p/18289596
  • 1. Spring Boot Redis 集群性能优化(基于 Redisson)
    • 1.1. 版本说明
    • 1.2. 为什么是 Redisson
    • 1.3. 参数优化
      • 1.3.1. Redisson 配置参数
        • 1.3.1.1. 通用参数
        • 1.3.1.2. 集群参数
        • 1.3.1.3. 最终参数配置
    • 1.4. 从 Nacos 获取 Redisson 配置

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 等更多框架无缝集成。

Redisson vs Jedis

Redisson vs Lettuce

1.3. 参数优化

redisson-spring-boot-starter 支持 3 种配置方式:

  1. 基于 org.springframework.boot.autoconfigure.data.redis.RedisProperties 类配置。
  2. spring.redis.redisson.config 指定 Redisson 自身的配置文本。
  3. spring.redis.redisson.file 指定 Redisson 自身的配置文件位置。

方式 1 只能配置少数 Redis 参数,其他线程数量、超时时间等关键参数无法配置,只适用于开发环境使用;
方式 2 指定了一长串 yaml 配置字符串,不易阅读和配置;
方式 3 指定了一个本地 yaml 配置文件,对于很多使用 Nacos 作为配置中心的项目来说非常不方便;

以上 3 种方式都不适合在项目中使用,因此后续会在方式 3 项目上进行改造,使支持从 Nacos 读取配置。

1.3.1. Redisson 配置参数

1.3.1.1. 通用参数

  1. threads:线程池数量。这个线程池数量被所有 RTopic 对象监听器,RRemoteService 调用者和 RExecutorService 任务共同共享。默认值:16。
  2. nettyThreads:Netty 线程池数量。这个线程池数量是在一个 Redisson 实例内,被其创建的所有分布式数据类型和服务,以及底层客户端所一同共享的线程池里保存的线程数量。默认值:32。
  3. nettyExecutor:Use external ExecutorService which is used by Netty for Redis response decoding and command sending. 默认值:null。
  4. codec:Redisson 的对象编码类是用于将对象进行序列化和反序列化,以实现对该对象在 Redis 里的读取和存储。默认值:!<org.redisson.codec.Kryo5Codec> {}
  5. executor:单独提供一个用来执行所有 RTopic 对象监听器,RRemoteService 调用者和 RExecutorService 任务的线程池(ExecutorService)实例。默认值:null。
  6. transportMode:传输模式。默认值:NIO。
  7. eventLoopGroup:用于特别指定一个 EventLoopGroup. EventLoopGroup 是用来处理所有通过 Netty 与 Redis 服务之间的连接发送和接受的消息。每一个 Redisson 都会在默认情况下自己创建管理一个 EventLoopGroup 实例。因此,如果在同一个 JVM 里面可能存在多个 Redisson 实例的情况下,采取这个配置实现多个 Redisson 实例共享一个 EventLoopGroup 的目的。默认值:null。
  8. lockWatchdogTimeout:监控锁的看门狗超时时间单位为毫秒。该参数只适用于分布式锁的加锁请求中未明确使用 leaseTimeout 参数的情况。如果该看门口未使用 lockWatchdogTimeout 去重新调整一个分布式锁的 lockWatchdogTimeout 超时,那么这个锁将变为失效状态。这个参数可以用来避免由 Redisson 客户端节点宕机或其他原因造成死锁的情况。默认值:30000。
  9. checkLockSyncedSlaves:Defines whether to check synchronized slaves amount with actual slaves amount after lock acquisition。默认值:true。
  10. slavesSyncTimeout:Defines slaves synchronization timeout in milliseconds applied to each operation of RLock, RSemaphore, RPermitExpirableSemaphore objects。默认值:1000。
  11. 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。
  12. keepPubSubOrder:通过该参数来修改是否按订阅发布消息的接收顺序出来消息,如果选否将对消息实行并行处理,该参数只适用于订阅发布消息的情况。默认值:true。
  13. 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。
  14. minCleanUpDelay:Defines minimum delay in seconds for clean up process of expired entries. Applied to JCache, RSetCache, RClusteredSetCache, RMapCache, RListMultimapCache, RSetMultimapCache, RLocalCachedMapCache, RClusteredLocalCachedMapCache objects.。默认值:5。
  15. maxCleanUpDelay:Defines maximum delay in seconds for clean up process of expired entries. Applied to JCache, RSetCache, RClusteredSetCache, RMapCache, RListMultimapCache, RSetMultimapCache, RLocalCachedMapCache, RClusteredLocalCachedMapCache objects.。默认值:1800。
  16. cleanUpKeysAmount:Defines expired keys amount deleted per single operation during clean up process of expired entries. Applied to JCache, RSetCache, RClusteredSetCache, RMapCache, RListMultimapCache, RSetMultimapCache, RLocalCachedMapCache, RClusteredLocalCachedMapCache objects.。默认值:100。
  17. nettyHook:Netty hook applied to Netty Bootstrap and Channel objects.。默认值:!<org.redisson.client.DefaultNettyHook> {}
  18. connectionListener:Connection listener which is triggered when Redisson connected/disconnected to Redis server。默认值:null。
  19. 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。
  20. addressResolverGroupFactory:Allows to specify customized implementation of io.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.
  21. 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 made
    • false - connects to Redis during Redisson instance creation。
  22. protocol:Defines Redis protocol version. Available values: RESP2, RESP3。默认值:RESP2。

1.3.1.2. 集群参数

集群参数前缀:clusterServersConfig

  1. idleConnectionTimeout:如果当前连接池里的连接数量超过了最小空闲连接数,而同时有连接空闲时间超过了该数值,那么这些连接将会自动被关闭,并从连接池里去掉。时间单位是毫秒。默认值:10000。
  2. connectTimeout:同任何节点建立连接时的等待超时。时间单位是毫秒。默认值:10000。
  3. timeout:等待节点回复命令的时间。该时间从命令发送成功时开始计时。默认值:3000。
  4. subscriptionTimeout:Defines subscription timeout in milliseconds applied per channel subscription。默认值:7500。
  5. retryAttempts:如果尝试达到 retryAttempts(命令失败重试次数)仍然不能将命令发送至某个指定的节点时,将抛出错误。如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时)计时。默认值:3。
  6. retryInterval:在某个节点执行相同或不同命令时,连续 失败 failedAttempts(执行失败最大次数)时,该节点将被从可用节点列表里清除,直到 reconnectionTimeout(重新连接时间间隔)超时以后再次尝试。默认值:1500。
  7. password:用于节点身份验证的密码。默认值:null。
  8. username:Username for Redis server authentication. Requires Redis 6.0+。默认值:null。
  9. credentialsResolver:Defines Credentials resolver which is invoked during connection for Redis server authentication. Returns Credentials object per Redis node address, it contains username and password fields. Allows to specify dynamically changing Redis credentials。默认值:!<org.redisson.client.DefaultCredentialsResolver> {}
  10. subscriptionsPerConnection:每个连接的最大订阅数量。默认值:5。
  11. clientName:在 Redis 节点里显示的客户端名称。默认值:null。
  12. sslEnableEndpointIdentification:开启 SSL 终端识别能力。默认值:true。
  13. sslProvider:确定采用哪种方式(JDK 或 OPENSSL)来实现 SSL 连接。默认值:JDK。
  14. 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。
  15. sslTruststorePassword:指定 SSL 信任证书库的密码。默认值:null。
  16. 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。
  17. sslKeystorePassword:指定 SSL 钥匙库的密码。默认值:null。
  18. sslProtocols:Defines array of allowed SSL protocols. Example values: TLSv1.3, TLSv1.2, TLSv1.1, TLSv1。默认值:null。
  19. 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 invoke channelInactive method for closed connections. Set 0 to disable。默认值:30000。
  20. keepAlive:Enables TCP keepAlive for connection。默认值:false。
  21. tcpKeepAliveCount:Defines the maximum number of keepalive probes TCP should send before dropping the connection. 0 value means use system default setting。默认值:0。
  22. 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。
  23. tcpKeepAliveInterval:Defines the time in seconds between individual keepalive probes. 0 value means use system default setting。默认值:0。
  24. 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。
  25. tcpNoDelay:Enables TCP noDelay for connection。默认值:true。
  26. nameMapper:Defines Name mapper which maps Redisson object name to a custom name. Applied to all Redisson objects。默认值:!<org.redisson.api.DefaultNameMapper> {}
  27. commandMapper:Defines Command mapper which maps Redis command name to a custom name. Applied to all Redis commands。默认值:!<org.redisson.config.DefaultCommandMapper> {}
  28. 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
  29. slaveConnectionMinimumIdleSize:Redis 'slave' node minimum idle connection amount for each slave node。默认值:24。
  30. slaveConnectionPoolSize:Redis 'slave' node maximum connection pool size for each slave node。默认值:64。
  31. 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。
  32. masterConnectionMinimumIdleSize:Minimum idle connections amount per Redis master node。默认值:24。
  33. masterConnectionPoolSize:Redis 'master' node maximum connection pool size。默认值:64。
  34. 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
  35. subscriptionMode:Set node type used for subscription operation。默认值:MASTER。
    Available values:
    • SLAVE - Subscribe to slave nodes
    • MASTER - Subscribe to master node
  36. subscriptionConnectionMinimumIdleSize:Minimum idle connection pool size for subscription (pub/sub) channels. Used by RTopic, RPatternTopic, RLock, RSemaphore, RCountDownLatch, RClusteredLocalCachedMap, RClusteredLocalCachedMapCache, RLocalCachedMap, RLocalCachedMapCache objects and Hibernate Local Cached Region Factories。默认值:1。
  37. subscriptionConnectionPoolSize:Maximum connection pool size for subscription (pub/sub) channels. Used by RTopic, RPatternTopic, RLock, RSemaphore, RCountDownLatch, RClusteredLocalCachedMap, RClusteredLocalCachedMapCache, RLocalCachedMap, RLocalCachedMapCache objects and Hibernate Local Cached Region Factories。默认值:50。
  38. 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。
  39. 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.
  40. 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 and org.redisson.api.HostNatMapper。默认值:!<org.redisson.api.DefaultNatMapper> {}
  41. nodeAddresses:Add Redis cluster node or Redis endpoint addresss in host:port format. Redisson discovers automatically cluster topology. Use rediss:// protocol for SSL connection。默认值:null。
  42. scanInterval:Scan interval in milliseconds. Applied to Redis clusters topology scan。默认值:5000。
  43. checkSlotsCoverage:Enables cluster slots check during Redisson startup。默认值:true。
  44. shardedSubscriptionMode:Defines whether to use sharded subscription feature available in Redis 7.0+. Used by RMapCache, 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: 60000connectTimeout: 10000timeout: 6000retryAttempts: 3retryInterval: 1500password: 123456pingConnectionInterval: 30000keepAlive: truetcpNoDelay: truesubscriptionsPerConnection: 5clientName: demoloadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}slaveConnectionMinimumIdleSize: 24slaveConnectionPoolSize: 64masterConnectionMinimumIdleSize: 24masterConnectionPoolSize: 64subscriptionConnectionMinimumIdleSize: 1subscriptionConnectionPoolSize: 50failedSlaveReconnectionInterval: 3000readMode: "SLAVE"subscriptionMode: "MASTER"scanInterval: 15000nodeAddresses:- "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 配置

  1. 在 Nacos 控制台页面新增配置

    • 命名空间:redisson-demo
    • Data ID:redisson-dev.yaml
    • Group:redisson-demo
    • 配置内容:参考上一章节“最终参数配置”
  2. 项目 application.yml 配置

spring:application:name: redisson-demoprofiles:active: devcloud:nacos:config:server-addr: 127.0.0.1:8848namespace: redisson-demogroup: redisson-demofile-extension: yamlusername: nacospassword: nacosconfig:import: nacos:redisson-demo.yaml
  1. 从 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;@Resourceprivate 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);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/740466.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【比赛】高一小学期2

【比赛】高一小学期2 题解挺唐的比赛,一道数位 dp 原题一道平衡树,然后 T1 数据范围还整错了。。没图了呜呜【比赛】高一小学期2$Rank$赛时日前赛后T1 同类分布 思路 印象里为数不多搞懂了的数位 dp,但过太久忘了,只能赛时打暴力 后来发现跟正解很接近了,只是在 dfs 前的预…

针对于早期版本的flutter开发的app的处理

下为某个flutter开发的app的hook app为flutter开发,知道是一个AES加密,但是相关so中方法的hook经验较少 这个app的dart的版本较早,是2.10的,找了一个开源的脚本,对代码进行了处理,获取到相对的so层的方法和地址 脚本地址:https://github.com/rscloura/Doldrums 目前较好…

(10)逻辑综合添加约束(环境约束)

一、环境约束此外,还有电路内互连线的延时也没有考虑在内四个环境约束:1.输出负载如果电路的输出负载过大,将会加大电路的transition time,从而影响电路时序此外,若dc默认输出负载为0,即相当于不接负载,这样综合出来的电路时序显然过于乐观,不能反映实际工作情况可以设…

[GIT] 解决:git status时有Untracked files(未跟踪的文件)

1 问题描述git pull时失败,报Please move or remove them before you merge。结果git status显示有一堆不太想提交的Untracked files(未跟踪的文件)。那么,Untracked files文件状态的文件,是什么?一般又如何处理呢?2 原因分析我们要真正弄明白问题的原因,我们就要先知道文…

windows版Oracle11g安装记录

一、下载Oracle 11g数据库安装包 已上传至博客园文件中 二、安装Oracle 11g 下载下来是下图这样的两个压缩包:1、解压这两个压缩包到同一个文件夹(切记路径文件不可有中文、空格和不规则字符。): 2、将解压好的win64_11gR2_database_2of2\database\stage\Components下的所有…

使用UFUNCTION(BlueprintCallable)修饰了函数,但是在蓝图中找不到怎么办?

在蓝图图表中,任意空位右键,去掉“情境勾选”就好了。

thinkphp6 使用Workerman测试

1.首先通过 composer 安装 think-worker、think-viewcomposer require topthink/think-worker composer require topthink/think-view安装成功后在项目的config目录会新增3个配置文件 2.修改woker.php中监听的端口return [// 扩展自身需要的配置host => 12…

C++ Boost 字符串处理库

Boost 库是一个由C/C++语言的开发者创建并更新维护的开源类库,其提供了许多功能强大的程序库和工具,用于开发高质量、可移植、高效的C应用程序。Boost库可以作为标准C库的后备,通常被称为准标准库,是C标准化进程的重要开发引擎之一。使用Boost库可以加速C应用程序的开发过程…

LLM推理引擎怎么选?TensorRT vs vLLM vs LMDeploy vs MLC-LLM

LLM擅长文本生成应用程序,如聊天和代码完成模型,能够高度理解和流畅。但是它们的大尺寸也给推理带来了挑战。有很多个框架和包可以优化LLM推理和服务,所以在本文中我将整理一些常用的推理引擎并进行比较。 https://avoid.overfit.cn/post/33f6420c91e74c0eb8d6737cb9471e27…

WPF ComboBox数据绑定:初始化动态加载ItemsSource后首次赋值Text不显示问题解决

原来:<ComboBox Text="{Binding Item}" ItemsSource="{Binding ItemLists}"></ComboBox>private void Paras_Init(){ItemLists = new ObservableCollection<string>();ItemLists.Add("11111");ItemLists.Add("22222&qu…

多边形填充-活动边表法

参考文档: 参考1:https://blog.csdn.net/u013044116/article/details/49737585 参考2:https://blog.csdn.net/keneyr/article/details/83747501 算法思想: 对多边形沿y轴从0开始遍历,建立边表NET。只记录顶点的x, dx, ymax。 根据NET构建活动边表AET(activate edge table)…

WebOffice在线编微软Offfice,并以二进制流的形式打开Word文档

在日常办公场景中,我们经常会遇到这种场景:我们的合同管理系统的各种Word,excel,ppt数据都是以二进制数组的形式存储在数据库中,如何从数据库中读取二进制数据,以二进制数据作为参数,然后加载到浏览器的Office窗口,实现在线编辑Office的功能呢? 猿大师办公助手是猿大师旗…