Redis客户端 Jedis 与 Lettuce

程序员大白

共 2381字,需浏览 5分钟

 · 2021-07-02

点击上方“程序员大白”,选择“星标”公众号

重磅干货,第一时间送达


Lettuce 和 Jedis 的定位都是 Redis 的 client,所以它们可以直接连接redis server。

Jedis 在实现上是直接连接的 redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接

Lettuce 的连接是基于 Netty 的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为 StatefulRedisConnection 是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

从Springboot 2.X 开始已经默认使用 Luttuce,但仍然提供两种客户端供使用。

  • SpringBoot 1.x 默认采用 Jedis 作为 redis 客户端连接池

  • SpringBoot 2.x spring-data-redis 默认采用 Lettuce 作为 redis 客户端驱动连接池


从 spring-boot-starter-redis 1.4.7.RELEASE 是该依赖的最后一个版本,迁移到 spring-boot-starter-data-redis

在 springboot1.4.7 之前的版本配置客户端连接池如下:

spring:
  redis:
    pool:
      maxActive: 5000
      maxIdle: 30
      minIdle: 5
      max-wait: 2000

在 1.4.7 版本之后如果仍使用以上配置,会提示你:Deprecated configuration property 'spring.redis.pool.max-active' ,即使配了也是失效配置。在RedisProperties(springboot autoconfig jar包)新增了 Jedis 和 Lettuce 内部类用来配置客户端连接池,如下

Jedis

spring:
  redis:
    jedis:
      pool:
        maxActive: 5000
        maxIdle: 30
        minIdle: 5
        max-wait: 2000

Jedis 可替换为 Lettuce

spring:
  redis:
    lettuce:
      pool:
        maxActive: 5000
        maxIdle: 30
        minIdle: 5
        max-wait: 2000

建议 Lettuce,但是使用 Lettuce 后,就不能使用 jedis pipeline 模式。Lettuce 的批量异步模式可以参考以下代码段

https://blog.csdn.net/huangjinjin520/article/details/118255621


另外,spring-boot-starter-redis 默认是不使用连接池的,只有配置 spring.redis.lettuce.pool 下的属性的时候才可以使用到 redis 连接池。在高并发的项目中请注意配置。


国产小众浏览器因屏蔽视频广告,被索赔100万(后续)

年轻人“不讲武德”:因看黄片上瘾,把网站和786名女主播起诉了

中国联通官网被发现含木马脚本,可向用户推广色情APP

张一鸣:每个逆袭的年轻人,都具备的底层能力




西[]


浏览 35
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报