首页 > 数据库 >Springboot Redis 性能优化(基于 Lettuce)

Springboot Redis 性能优化(基于 Lettuce)

时间:2024-05-27 15:44:14浏览次数:27  
标签:Springboot lettuce Default Redis redis Lettuce idle spring pool

1. Springboot Redis 性能优化(基于 Lettuce)

1.1. 为什么是 Lettuce

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:[email protected]: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: 64
        # 最小空闲连接数
        min-idle: 24
        # 最大活动连接数
        max-active: 64
        # 最大等待时间。当连接池所有连接耗尽时获取一个 Redis 连接将阻塞等待,超时之后抛出异常
        max-wait: 10s
        # 清理空闲连接的时间间隔。当设置一个正值时,将启动清理线程,每隔一段时间时隔清理空闲连接
        time-between-eviction-runs: 60s
      cluster:
        refresh:
          # 发现并查询所有 Redis 集群节点集群拓扑信息
          dynamic-refresh-sources: true
          # Redis 集群拓扑刷新时间
          period: 60s
          # 开启集群拓扑信息自适应刷新。Lettuce 客户端会根据集群的状态和与集群节点的交互情况来自动调整刷新集群信息的频率。
          adaptive: true

标签:Springboot,lettuce,Default,Redis,redis,Lettuce,idle,spring,pool
From: https://www.cnblogs.com/jason207010/p/18215440

相关文章

  • 前端程序员快速学会 Redis
    由于在学习Docker的过程中,使用到了Redis,但是不会,就快速补课一下Redis,以能会基础使用就够了,不求甚解。由于这篇会使用到上一篇文章Docker入门(一),没看的可以看看:前端程序员如何学习Docker(一)-掘金(juejin.cn),下面开始Redis入门:什么是Redis?官方说:"Redisisanin......
  • 基于SpringBoot+Vue的实验室管理系统设计与实现毕设(文档+源码)
            目录一、项目介绍二、开发环境三、功能介绍四、核心代码五、效果图六、源码获取:        大家好呀,我是一个混迹在java圈的码农。今天要和大家分享的是一款基于SpringBoot+Vue的实验室管理系统,项目源码请点击文章末尾联系我哦~目前有各类成品......
  • 基于SpringBoot+Vue的火车订票管理系统设计与实现毕设(文档+源码)
            目录一、项目介绍二、开发环境三、功能介绍四、核心代码五、效果图六、源码获取:        大家好呀,我是一个混迹在java圈的码农。今天要和大家分享的是一款基于SpringBoot+Vue的火车订票管理系统,项目源码请点击文章末尾联系我哦~目前有各类成品......
  • Shiro+Jwt+Redis
    如何整合Shiro+Jwt+Redis,以及为什么要这么做我个人认为①为什么用shiro:“Shiro+Jwt+Redis”模式和“单纯的shiro”模式相比,主要用的是shiro里面的登录认证和权限控制功能②为什么用jwt:“Shiro+Jwt”模式和“Shiro+Cookie”模式相比,后者的用户登录信息是存储在服务器的......
  • 如何远程访问Redis?
    远程访问Redis是一种常见的需求,特别是在分布式系统或跨地域网络中。通过远程访问,我们可以轻松地对远程的Redis数据库进行操作和管理。天联保障数据安全对于远程访问Redis的安全性问题,我们可以借助天联来保障数据的安全。天联是一种基于私有通道的远程访问解决方案,可以让异......
  • 计算机毕业设计springboot+vue学生档案学籍信息管理系统java
    本文所设计的学籍系统的设计与实现拥有前端和后端,前端使用Vue.js框架和创建,后端使用Springboot框架创建,开发语言采用Java,使用Mysql数据库对后台数据进行存储。将IDEA作为主要的开发工具。接着进行系统的需求分析、功能设计、数据库设计,最后进行编码实现。技术栈ide工具:IDEA......
  • 1915springboot VUE 宠物寄养平台系统开发mysql数据库web结构java编程计算机网页源码m
    一、源码特点 springbootVUE宠物寄养平台系统是一套完善的完整信息管理类型系统,结合springboot框架和VUE完成本系统,对理解JSPjava编程开发语言有帮助系统采用springboot框架(MVC模式开发),系统具有完整的源代码和数据库,系统主要采用B/S模式开发。springbootVUE宠物寄养......
  • springboot宠物领养管理系统论文
    目录摘要IAbstractII第1章绪论31.1项目研究的背景31.2开发目的和意义31.3国内外研究现状4第2章系统开发工具52.1Java编程语言52.2B/S模式52.3MySQL数据库62.4后端框架介绍72.4.1SpringBoot介绍72.4.2Mybatis介绍72.4.3SpringMvc介......
  • Redis教程(十七):Redis的Redisson分布式锁
    Redis分布式锁 Redis分布式锁的主要作用是在分布式系统环境下提供一种机制,用于确保在同一时间只有一个进程(或线程)能够执行某个关键代码段或访问特定的资源。这主要用于控制对共享资源的并发访问,以避免因多个进程同时修改同一数据而导致的数据不一致或其他竞争条件问题。 ......
  • redis
    redis介绍https://redis.io/downloads/win安装rediswin版本下载地址#最新5.x版本https://github.com/tporadowski/redis/releases/#最新3.x版本https://github.com/microsoftarchive/redis/releases-关系型数据库 PostgreSQL,MySQL,sqlserver,oracle.........