Spring Data Redis切换底层Jedis 和 Lettuce实现

1 简介

Spring Data Redis是 Spring Data 系列的一部分,它提供了Spring应用程序对Redis的轻松配置和使用。它不仅提供了对Redis操作的高级抽象,还支持Jedis和Lettuce两种连接方式。

可通过简单的配置就能连接Redis,并且可以切换Jedis和Lettuce两个连接方式。下面先来看看我们该如何使用它。

2 使用

2.1 引入Redis依赖

使用Spring Boot 提供的spring-boot-starter-data-redis依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>3.1.0</version>
</dependency>

2.2 自定义配置类

自定义配置类,用于设置Key和Value的序列化。

@Configuration
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

2.3 修改Redis连接配置

配置由Spring Boot提供,如果不改,会使用默认配置。支持单节点、哨兵模式和集群模式,可自由切换。

spring:
  data:
    redis:
      host: localhost
      port: 6379
      database: 1

2.4 使用RedisTemplate

注入RedisTemplate后,就可操作Redis。

RedisTemplate有两个泛型:

  • K表示Redis中的Key值类型,一般 String 类型
  • V表示Redis中的Value值类型,V可以是一个对象
@SpringBootTest
public class RedisTemplateTest {

    @Resource
    private RedisTemplate<String, String> redisTemplate;
    
    @Test
    public void test_set() {
        redisTemplate.opsForValue().set("test-key", "test-value");
    }
    
    @Test
    public void test_get() {
        String value = redisTemplate.opsForValue().get("test-key");
        System.out.println(value);
    }
}

3 源码分析

从使用来看,不知道用Jedis or Lettuce或如何切换。

3.1 Redis自动配置类:RedisAutoConfiguration

熟悉Spring Boot家族的组件时,一般从它的自动配置类开始,Redis自动配置类RedisAutoConfiguration:

image-20231127155643648

它引入两个连接Redis配置类:

  • Lettuce使用LettuceConnectionConfiguration
  • Jedis使用JedisConnectionConfiguration

3.2 Lettuce连接配置类:LettuceConnectionConfiguration

条件注解控制当前配置类能否生效:

  • @ConditionalOnClass:指定一个或多个目标类,若目标类在类路径下能找到,则当前配置类生效;只要有一个目标类未找到,则不生效
  • @ConditionalOnProperty:通过与配置文件的配置项,进行匹配来控制是否生效

这两个条件注解,只要有一个不生效,则当前配置类不生效。当该配置类生效后,会使用Lettuce相关依赖,来生成一个RedisConnectionFactory的Bean,用于获取Redis连接。

package org.springframework.boot.autoconfigure.data.redis;

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisClient.class)
@ConditionalOnProperty(name = "spring.data.redis.client-type", havingValue = "lettuce", matchIfMissing = true)
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
  ...
}

io.lettuce.core.RedisClient类能找到,说明已引入Lettuce相关依赖。spring-boot-starter-data-redis确实就有lettuce-core依赖,即 Spring Data Redis 默认用Lettuce。

image-20231127161706490

3.3 Jedis连接配置类:JedisConnectionConfiguration

同理,会校验类路径中是否有Jedis相关依赖类。

同样该配置类生效后,会使用Jedis相关依赖,来生成一个RedisConnectionFactory的Bean,用于获取Redis连接。

该类中还有一个注解@ConditionalOnMissingBean,用于保证只有一个RedisConnectionFactoryBean被注入。

redis.clients.jedis.Jedis类未找到,即无Jedis相关依赖包,则当前配置类无效。

3.4 如何将连接方式切换为Jedis

有两种方式,但前提条件是先引入Jedis相关依赖。

① 利用@ConditionalOnClass注解

排除Lettuce依赖,只保留Jedis依赖。通过@ConditionalOnClass注解的校验,切换成Jedis。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

② 利用@ConditionalOnProperty注解

不排除Lettuce的依赖,即这两种方式的依赖同时存在。

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

通过@ConditionalOnProperty注解的校验,将配置spring.redis.client-type设为jedis,这样也可以切换至Jedis方式。

③ 评估

第一种优点:不加载多余的依赖包,推荐。

第二种优点:可通过修改配置文件,自由切换连接方式。

本文由博客一文多发平台 OpenWrite 发布!

OpenAI 面向所有用户免费开放 ChatGPT Voice 程序员篡改 ETC 余额,一年私吞 260 余万元 Spring Boot 3.2.0 正式发布 谷歌员工离职后抨击大老板,曾深度参与 Flutter 项目、制定 HTML 相关标准 微软 Copilot Web AI 将于12月1日正式上线,支持中文 微软开源 Terminal Chat Rust Web 框架 Rocket 发布 v0.5:支持异步、SSE、WebSockets 等 Redis 之父用纯 C 语言代码实现 Telegram Bot 框架 假如你是开源项目维护者,遇到这种回复能忍到哪步? PHP 8.3 GA
{{o.name}}
{{m.name}}

猜你喜欢

转载自my.oschina.net/u/3494859/blog/10293842