• SpringBoot篇之集成Jedis、Lettuce、Redisson


    在这里插入图片描述


    前言

    大家好,我是AK,最近在做新项目,基于旧项目框架修改,正好最近也在整理springboot相关知识,项目中用到Redis,因此整理出来,帮助需要的小伙伴搞清楚到底选择哪个Redis客户端库。


    一、详解Jedis、Lettuce 和 Redisson的区别

    Jedis、Lettuce 和 Redisson 都是 Java 中与 Redis 数据库进行交互的客户端库。它们分别有各自的特点和用途。

    1. Jedis:

    Jedis 是一个常用的 Redis 客户端,通过直接操作 Redis 的命令实现与数据库的交互。
    Jedis 是单线程的,即在同一时间只能处理一个命令请求。这使得它在单线程环境下非常高效,并且适合于并发请求较少的情况。
    Jedis 使用简单、轻量,并且易于上手。

    2. Lettuce:

    Lettuce 是一个基于异步、事件驱动的 Redis 客户端。
    Lettuce 是基于 Netty 框架实现的,它使用异步非阻塞方式与 Redis 进行通信,并支持连接池和发布/订阅模式。
    Lettuce 支持并发请求和连接自动管理,适合处理高并发的环境。
    Lettuce 提供更多的配置选项和 Redis 特性的支持。

    3. Redisson:

    Redisson 是一个基于 Lettuce 和 Netty 的 Redis 客户端和数据结构库,提供了丰富的分布式和集群功能。
    Redisson 提供了更高层次的抽象,使开发者更容易使用 Redis 的分布式锁、分布式集合、分布式队列等功能。
    Redisson 支持集群模式、主从复制和哨兵模式,并提供了许多分布式解决方案和优化的数据结构。
    性能方面,Jedis 和 Lettuce 在不同的场景下可能会有不同的表现。由于 Jedis 是单线程的,适合于非常低的并发环境。而 Lettuce 通过其异步非阻塞的网络处理方式,可以更好地支持高并发环境。Redisson 则在性能方面与 Lettuce 类似,但它更注重于分布式功能和数据结构的操作。

    总结来说,Jedis 是一个简单、轻量的 Redis 客户端,适用于单线程环境下的低并发场景;Lettuce 是一个基于异步、事件驱动的 Redis 客户端,适用于高并发场景,并提供了更多的配置选项和功能支持;Redisson 是一个基于 Lettuce 和 Netty 的 Redis 客户端和数据结构库,提供了丰富的分布式和集群功能,并重点关注分布式解决方案和数据结构的优化。在选择合适的客户端时,需要根据具体需求、场景和性能要求进行综合考虑。

    二、SpringBoot集成

    2.1 集成Jedis

    要在Spring Boot中集成Jedis,您可以按照以下步骤进行操作:

    添加依赖:打开项目的 pom.xml 文件,并添加 Jedis 的依赖。您可以在 标签下添加以下代码:

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.11.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置 Redis 连接:在 Spring Boot 的配置文件(application.properties 或 application.yml)中,添加 Redis 的连接配置,包括主机、端口、密码等。例如,在 application.properties 文件中,您可以添加以下代码:

    Redis 连接配置

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=your_password (如果有密码的话)
    
    • 1
    • 2
    • 3

    创建 Jedis 实例:在 Java 代码中,您可以创建 Jedis 实例来与 Redis 进行交互。可以通过在需要使用的类中注入 JedisPool 对象,并使用它来获取 Jedis 实例。示例代码如下:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    @Configuration
    public class RedisConfig {
    
        @Autowired
        private RedisProperties redisProperties;
    
        @Bean
        public JedisPool jedisPool() {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxTotal(10);
    
            JedisPool jedisPool = new JedisPool(poolConfig, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), redisProperties.getPassword());
    
            return jedisPool;
        }
    
        @Bean
        public Jedis jedis(JedisPool jedisPool) {
            return jedisPool.getResource();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    这是一个 Redis 配置类的示例。您可以根据自己的需求进行相关配置,并在需要使用 Jedis 的地方注入 Jedis 对象。

    使用 Jedis 进行操作:现在您可以在其他类中使用注入的 Jedis 对象来执行 Redis 操作了。通过调用 Jedis 对象提供的方法,您可以执行各种 Redis 命令。以下是一个简单的示例:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import redis.clients.jedis.Jedis;
    
    @Service
    public class RedisService {
    
        @Autowired
        private Jedis jedis;
    
        public void set(String key, String value) {
            jedis.set(key, value);
        }
    
        public String get(String key) {
            return jedis.get(key);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在上述示例中,我们注入了 Jedis 对象,并使用 set 和 get 方法对 Redis 进行设置和获取操作。

    这样,您就成功地将 Jedis 集成到 Spring Boot 中了。您可以根据自己的需要进行更多的操作和配置。记得在完成后适当关闭 Jedis 连接以及释放资源。

    2.2 集成Lettuce

    要在Spring Boot中集成Lettuce,您可以按照以下步骤进行操作:

    添加依赖:打开项目的 pom.xml 文件,并添加 Lettuce 的依赖。您可以在 标签下添加以下代码:

    <dependency>
        <groupId>io.lettuce</groupId>
        <artifactId>lettuce-core</artifactId>
        <version>6.1.3.RELEASE</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置 Redis 连接:在 Spring Boot 的配置文件(application.properties 或 application.yml)中,添加 Redis 的连接配置,包括主机、端口、密码等。例如,在 application.properties 文件中,您可以添加以下代码:

    Redis 连接配置

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=your_password (如果有密码的话)
    spring.redis.lettuce.pool.max-active=10
    创建 Lettuce 实例:在需要使用 Redis 的代码中,您可以使用 LettuceConnectionFactory 来创建 Lettuce 的连接工厂,并配置连接池。示例代码如下:
    import io.lettuce.core.RedisClient;
    import io.lettuce.core.RedisURI;
    import io.lettuce.core.resource.ClientResources;
    import io.lettuce.core.resource.DefaultClientResources;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
    import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
    
    @Configuration
    public class RedisConfig {
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.password}")
        private String password;
    
        @Value("${spring.redis.lettuce.pool.max-active}")
        private int maxActive;
    
        @Autowired(required = false)
        private ClientResources clientResources;
    
        @Bean(destroyMethod = "shutdown")
        public ClientResources clientResources() {
            return DefaultClientResources.create();
        }
    
        @Bean
        public RedisConnectionFactory lettuceConnectionFactory() {
            RedisURI redisURI = RedisURI.builder()
                    .withHost(this.host)
                    .withPort(this.port)
                    .withPassword(this.password)
                    .build();
    
            LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
                    .clientResources(this.clientResources)
                    .poolConfig(getDefaultPoolConfig())
                    .build();
    
            RedisClient redisClient = RedisClient.create(redisURI);
            LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(getDefaultPoolConfig())
                    .commandTimeout(redisClient.getOptions().getTimeout())
                    .build();
            
            LettuceConnectionFactory factory = new LettuceConnectionFactory(redisClient, lettucePoolingClientConfiguration);
            factory.setShareNativeConnection(false);
    
            return factory;
        }
    
        private GenericObjectPoolConfig<?> getDefaultPoolConfig() {
            GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
            config.setMaxTotal(this.maxActive);
            return config;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    在上述示例中,我们通过 LettucePoolingClientConfiguration 和 RedisURI 配置了 Lettuce 的连接和连接池。在需要使用 Redis 的地方,只需注入 RedisConnectionFactory 对象即可。

    使用 Lettuce 进行操作:现在您可以在其他类中使用注入的 RedisConnectionFactory 对象来执行 Redis 操作了。Spring Boot 提供了各种 RedisTemplate 作为默认的 Redis 操作模板,您还可以使用 ReactiveRedisTemplate 进行响应式操作。以下是一个简单的示例:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Service;
    
    @Service
    public class RedisService {
    
        @Autowired
        private RedisTemplate<String, String> redisTemplate;
    
        public void set(String key, String value) {
            redisTemplate.opsForValue().set(key, value);
        }
    
        public String get(String key) {
            return redisTemplate.opsForValue().get(key);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在上述示例中,我们注入了 RedisTemplate 对象,并使用它来执行 Redis 的 set 和 get 操作。

    这样,您就成功地将 Lettuce 集成到 Spring Boot 中了。您可以根据自己的需要进行更多的操作和配置。记得在完成后适当关闭 Redis 连接以及释放资源。

    2.3 集成Redisson

    要在Spring Boot中集成Redisson,您可以按照以下步骤进行操作:

    添加依赖:打开项目的 pom.xml 文件,并添加 Redisson 的依赖。您可以在 标签下添加以下代码:

    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson-spring-boot-starter</artifactId>
        <version>3.16.1</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置 Redisson:在 Spring Boot 的配置文件(application.properties 或 application.yml)中,添加 Redisson 的配置。例如,在 application.properties 文件中,您可以添加以下代码:

    Redisson 配置

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=your_password (如果有密码的话)
    
    • 1
    • 2
    • 3

    使用 Redisson 进行操作:您可以在需要使用 Redis 的代码中注入 RedissonClient 对象,并使用它来执行 Redis 操作。以下是一个简单的示例:

    import org.redisson.api.RedissonClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class RedisService {
    
        @Autowired
        private RedissonClient redissonClient;
    
        public void set(String key, String value) {
            redissonClient.getBucket(key).set(value);
        }
    
        public String get(String key) {
            return (String) redissonClient.getBucket(key).get();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在上述示例中,我们注入了 RedissonClient 对象,并使用它来执行 Redis 的 set 和 get 操作。

    这样,您就成功地将 Redisson 集成到 Spring Boot 中了。Redisson 提供了各种功能强大的API,您可以根据自己的需要进行更多的操作和配置。记得在完成后适当关闭 Redis 连接以及释放资源。


    总结

    最后如果还是不清楚选择哪个,推荐使用Lettuce或Redisson,如果是单体应用选Lettuce,分布式场景就选择Redisson。




                                                                                                             ---- 永不磨灭的番号:我是AK



    在这里插入图片描述

  • 相关阅读:
    flask学生信息管理
    DirtyCow脏牛漏洞复现(CVE-2016-5195)
    SOC-hello world
    Vue3+TS+Vite+NaiveUI搭建一个项目骨架
    一文了解 Flutter 3.24 中的新功能和增强功能
    Servlet入门
    hive with tez:无法从链中的任何提供者加载aws凭据
    【Docker】学习笔记
    MFC使用正则表达式基础步骤
    【C++天梯计划】1.7 哈希算法(hash)
  • 原文地址:https://blog.csdn.net/weixin_36808034/article/details/133795566