Spring-Boot中Redis的应用,lettuce与jedis

基本认识
lettuce是什么?

redis客户端,也是spring-boot中redis客户端的默认实现

jedis是什么?

redis客户端,如果在Spring-boot中使用jedis,需要额外添加jedis依赖,然后指定client-type为jedis

讲讲Spring-boot项目中redis相关依赖

一般来讲,Spring-boot项目中的redis都是spring-boot-starter-data-redis

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

点进去下层的依赖是这三个

<dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starterartifactId>
      <version>2.6.2version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>org.springframework.datagroupId>
      <artifactId>spring-data-redisartifactId>
      <version>2.6.0version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>io.lettucegroupId>
      <artifactId>lettuce-coreartifactId>
      <version>6.1.5.RELEASEversion>
      <scope>compilescope>
    dependency>
dependencies>
总结

上面的依赖可知,spring-boot-starter-data-redis默认的redis客户端是lettuce,而不是jedis,如果我们想用jedis,这个时候需要将lettuce的依赖排除掉,然后添加一个jedis的依赖。

客户端配置更换
方式一:依赖排除

同时,我们一般使用redis的时候一般都会使用连接池,首先,我们需要添加依赖(其实lettuce-core里面有这个依赖,里面还有很多netty的依赖,很有意思)

如果是lettuce,比如连接池,我们需要添加如下配置

# 最大连接数
spring.redis.lettuce.pool.max-active=10
# 最大空闲连接数
spring.redis.lettuce.pool.max-idle=10
# 最小空闲连接数
spring.redis.lettuce.pool.min-idle=0
# 最大等待阻塞时间,-1表示不限制
spring.redis.lettuce.pool.max-wait=-1ms

如果改成jedis,只需要将上面的lettuce改成jedis即可。

方式二:更改配置(spring.redis.client-type)更常用

依赖不排除,添加jedis的依赖,将这个client-type的值改成jedis或者lettuce。

你可能感兴趣的:(java,redis,spring-boot,缓存)