首页 > 数据库 >Spring Boot Redis 集群性能优化(基于 Redisson)

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

时间:2024-07-08 11:42:02浏览次数:16  
标签:Redisson Spring Redis connection redisson 默认值 Defines

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: 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 配置

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

    • 命名空间:redisson-demo
    • Data ID:redisson-dev.yaml
    • Group:redisson-demo
    • 配置内容:参考上一章节“最终参数配置”
  2. 项目 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
  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;
    @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

相关文章

  • java-spring boot光速入门教程(超详细!!)
    目录一、引言1.1初始化配置1.2整合第三方框架1.3后期维护1.4部署工程1.5敏捷式开发二、SpringBoot介绍springboot2.1搭建一个springboot工程2.2使用idea创建项目2.3在线创建姿势2.4项目的目录结构2.5项目的运行方式2.6yml文件格式2.7多环境配置2......
  • springboot在线智能助考系统-计算机毕业设计源码00068
    摘要随着人工智能技术的快速发展,智能辅助学习系统在教育领域日益受到重视。本研究旨在基于GPT构建在线智能助考系统,结合先进的自然语言处理技术,为用户提供智能问答、模拟考试、资源分享、交流论坛等功能,旨在提升用户学习效率和体验。GPT模型作为一种自然语言生成模型,具有......
  • springboot事故车辆与违章车辆跟踪系统-计算机毕业设计源码03863
    摘  要科技进步的飞速发展引起人们日常生活的巨大变化,电子信息技术的飞速发展使得电子信息技术的各个领域的应用水平得到普及和应用。信息时代的到来已成为不可阻挡的时尚潮流,人类发展的历史正进入一个新时代。在现实运用中,应用软件的工作规则和开发步骤,采用Java技术建设事......
  • springboot个人健康信息管理小程序-计算机毕业设计源码07695
    摘要在当今这个数字化、信息化的时代,个人健康管理已成为人们生活中不可或缺的一部分。随着生活节奏的加快,越来越多的人开始关注自己的身体状况,希望能够及时了解并调整自己的生活习惯,以达到最佳的健康状态。为此,我们开发了一款基于SpringBoot的个人健康信息管理小程序,旨在......
  • SpringBoot使用线程池实现异步批量处理任务
    模拟批处理大量数据@Slf4j@ComponentpublicclassTestFutureService{@AutowiredprivateTestFutureServiceImpltestFutureServiceImpl;/***多线程的优势:多核CPU使用多线程可以提高CPU的利用率(单核CPU不行,反而降低),可以实现异步调用。**......
  • Redisson锁误删除
    1、目标本文的主要目标是探究Redisson分布式锁在设置过期时间的情况下多线程是否会误删除的问题,首先分析单线程执行的完整过程,然后分析多线程锁误删除的现象,接着进行源码分析,理解Redisson如何保证多线程场景下当前线程不会误删除其他线程id的锁,最后是总结2、单线程执行的......
  • SpringBoot集成Mongodb文档数据库
    添加Maven依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>配置Mongodb连接信息spring:data:mongodb:host:10.30.29.246......
  • 课程设计-基于Springboot+Vue的网上商城购物系统的设计与实现(源码+LW+包运行)
    源码获取地址:https://download.csdn.net/download/u011832806/89426605系统演示视频:链接:https://pan.baidu.com/s/1p9Xv9VrlNXSyNXRkdhccPg?pwd=xfdy一.系统概述网上商城购物系统主要是为了提高工作人员的工作效率和更方便快捷的满足用户,更好存储所有数据信息及快速方......
  • Spring Boot3整合Mybatis Plus,数据库为MySQL
    项目结构如下:注意不需要任何XML文件1.导入依赖除了SpringBoot创建时自带的依赖,还需要加入:<!--MybatisPlus依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.7</version&g......
  • SpringBoot整合Radis(redis启用,maven依赖,及具体实用)
    文章目录1、本地下载redis并且开启2、导入maven依赖3、添加application.properties4、创建配置类RedisConfig.java5、使用1、注解1、@Cacheable(value="",key="")2、**@CachePut**(value="",key="")3、CacheEvict(value="",key="")2、示例1、本地下......