1. Springboot Redis 性能优化

1.1. 客户端选择

Springboot 2.x.x 开始默认使用 lettuce 作为 redis 客户端。

引用 baeldung.com 的话:

Why Lettuce?
We’ve covered Jedis in one of the previous posts. What makes Lettuce different?
The most significant difference is its asynchronous support via Java 8’s CompletionStage interface and support for Reactive Streams. As we’ll see below, Lettuce offers a natural interface for making asynchronous requests from the Redis database server and for creating streams.
It also uses Netty for communicating with the server. This makes for a “heavier” API but also makes it better suited for sharing a connection with more than one thread.

总结:选择 lettuce 作为 redis 客户端。

1.2. 参数优化

1.2.0.1. Springboot Redis 所有参数项
  • spring.redis.database: Database index used by the connection factory.
  • spring.redis.url: Connection URL. Overrides host, port, username, and password. Example: redis://user:password@example.com:6379
  • spring.redis.host: Redis server host. Default: localhost
  • spring.redis.username: Login username of the redis server.
  • spring.redis.password: Login password of the redis server.
  • spring.redis.port: Redis server port. Default: 6379.
  • spring.redis.ssl: Whether to enable SSL support.
  • spring.redis.timeout: Read timeout. Default: 60s.
  • spring.redis.connect-timeout: Connection timeout. Default: 10s.
  • spring.redis.client-name: Client name to be set on connections with CLIENT SETNAME.
  • spring.redis.client-type: Type of client to use. By default, auto-detected according to the classpath. 可选值:lettuce、jedis
  • spring.redis.sentinel.master: Name of the Redis server.
  • spring.redis.sentinel.nodes: Comma-separated list of "host:port" pairs.
  • spring.redis.sentinel.username: Login username for authenticating with sentinel(s).
  • spring.redis.sentinel.password: Database index used by the connection factory.
  • spring.redis.cluster.nodes: Comma-separated list of "host:port" pairs to bootstrap from. This represents an "initial" list of cluster nodes and is required to have at least one entry.
  • spring.redis.cluster.max-redirects: Maximum number of redirects to follow when executing commands across the cluster. Default: 5.
  • spring.redis.jedis.pool.enabled: Whether to enable the pool. Enabled automatically if "commons-pool2" is available. With Jedis, pooling is implicitly enabled in sentinel mode and this setting only applies to single node setup.
  • spring.redis.jedis.pool.max-idle: Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections. Default: 8.
  • spring.redis.jedis.pool.min-idle: 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. Default: 0.
  • spring.redis.jedis.pool.max-active: Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. Default: 8.
  • spring.redis.jedis.pool.max-wait: 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. Default: -1.
  • spring.redis.jedis.pool.time-between-eviction-runs: Time between runs of the idle object evictor thread. When positive, the idle object evictor thread starts, otherwise no idle object eviction is performed.
  • spring.redis.lettuce.shutdown-timeout: Shutdown timeout. Default: 100ms
  • spring.redis.lettuce.pool.enabled: Whether to enable the pool. Enabled automatically if "commons-pool2" is available. With Jedis, pooling is implicitly enabled in sentinel mode and this setting only applies to single node setup.
  • spring.redis.lettuce.pool.max-idle: Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections. Default: 8.
  • spring.redis.lettuce.pool.min-idle: 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. Default: 0.
  • spring.redis.lettuce.pool.max-active: Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. Default: 8.
  • spring.redis.lettuce.pool.max-wait: 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. Default: -1.
  • spring.redis.lettuce.pool.time-between-eviction-runs: Time between runs of the idle object evictor thread. When positive, the idle object evictor thread starts, otherwise no idle object eviction is performed.
  • spring.redis.lettuce.cluster.refresh.dynamic-refresh-sources: 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. Default: true
  • spring.redis.lettuce.cluster.refresh.period: Cluster topology refresh period.
  • spring.redis.lettuce.cluster.refresh.adaptive: Whether adaptive topology refreshing using all available refresh triggers should be used.

1.2.1. 最终参数配置

spring:
  redis:
    # 数据库索引
    database: 0
    # 读数据超时时间
    timeout: 5s
    # 连接超时时间
    connect-timeout: 3s
    # 客户端名称
    client-name: springboot-demo
    # 客户端类型
    client-type: lettuce
    cluster:
      # 集群地址
      nodes: 127.0.0.1:6379,127.0.0.1:7379,127.0.0.1:8379
      # 最大重定向次数。在 Redis 集群环境中,当一个节点接收到一个请求,但该请求应该由另一个节点处理时,该节点会返回一个重定向响应。客户端需要根据这个响应来重新定位到正确的节点。
      max-redirects: 5
    lettuce:
      # 关闭客户端连接前等待时间。Lettuce 底层使用 Netty 通信,在断开 Netty 连接时可能有活动的连接,适当设置等待时间以确保强制关闭前所有 Redis 任务都得到处理避免数据异常
      shutdown-timeout: 5s
      pool:
        # 开启连接池
        enabled: true
        # 最大空闲连接数
        max-idle: 25
        # 最小空闲连接数
        min-idle: 10
        # 最大活动连接数。Redis 内部使用单线程读写,连接数过多不仅不能提高性能,反而会引起响应超时、串包等问题,这里需要根据实际情况设置一个不太大的值。
        max-active: 100
        # 最大等待时间。当连接池所有连接耗尽时获取一个 Redis 连接将阻塞等待,超时之后抛出异常
        max-wait: 10s
        # 清理空闲连接的时间间隔。当设置一个正值时,将启动清理线程,每隔一段时间时隔清理空闲连接
        time-between-eviction-runs: 60s
      cluster:
        refresh:
          # 发现并查询所有 Redis 集群节点集群拓扑信息
          dynamic-refresh-sources: true
          # Redis 集群拓扑刷新时间
          period: 60s
          # 开启集群拓扑信息自适应刷新。Lettuce 客户端会根据集群的状态和与集群节点的交互情况来自动调整刷新集群信息的频率。
          adaptive: true

Jason
4 声望0 粉丝