在SpringBoot中集成Redis之Lettuce

文章展示了如何在SpringBoot项目中配置并使用Redis作为数据存储,引入了spring-boot-starter-data-redis和commons-pool2依赖。配置包括Redis连接参数和Lettuce连接池设置。此外,还提供了一个单元测试用例,用于存取List<Student>类型的对象到Redis并进行序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.12</version>
    <relativePath/>
</parent>

添加依赖

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

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <!-- 带上版本号可能会遇到问题 -->
</dependency>

配置文件

spring:
  redis:
    host: localhost
    port: 6379
    database: 0
    timeout: 10000
    
    lettuce:
      pool:
        max-active: 10
        max-wait: -1
        max-idle: 5
        min-idle: 0

单元测试示例:

package com.etoak;


import com.alibaba.fastjson.JSON;
import com.etoak.test.Student;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: TestApp
 * @Description: 类描述
 * @author: wsdhla
 * @date: 2023/06/29 18:50
 */
@SpringBootTest
public class TestAppTests {
    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testRedis() {
        List<Student> stuList = new ArrayList<>();
        stuList.add(new Student("name1", 100));
        stuList.add(new Student("name2", 60));

        redisTemplate.opsForValue().set("stuList", stuList);

        List<Student> stuList1 = (List<Student>) redisTemplate.opsForValue().get("stuList");

        System.out.println(JSON.toJSONString(stuList1));
    }
}

Spring Boot 3.2项目中集成Redis数据库,可以通过以下方式实现。首先,确保你的项目基于Maven构建,并且已经引入了Spring Boot的必要依赖。接下来需要添加`spring-boot-starter-data-redis`依赖以启用对Redis的支持。 ### 添加Maven依赖 在`pom.xml`文件中添加如下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 此外,如果你计划使用Lettuce作为Redis客户端(这是Spring Boot默认推荐的方式),则不需要额外添加任何依赖。但如果你想使用Jedis,可以手动加入其依赖。 ### 配置application.properties 接着,在`application.properties`或`application.yml`文件中配置Redis连接信息。以下是基于`application.properties`的示例: ```properties # Redis Configuration spring.redis.host=localhost spring.redis.port=6379 ``` 如果Redis服务器设置了密码,则可添加: ```properties spring.redis.password=yourpassword ``` 对于更复杂的场景,例如配置超时时间、池化设置等,可以进一步扩展这些属性[^1]。 ### 启用Redis支持 创建一个配置类来定义`RedisTemplate`和`StringRedisTemplate`,以便于操作Redis数据存储。下面是一个简单的例子: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); return template; } } ``` 此配置类确保键值对以字符串形式正确序列化/反序列化,便于后续操作。 ### 使用Redis 最后,在需要访问Redis的服务组件中注入并使用`RedisTemplate`或`StringRedisTemplate`进行读写操作。比如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { private final RedisTemplate<String, Object> redisTemplate; @Autowired public RedisService(RedisTemplate<String,Object> redisTemplate){ this.redisTemplate = redisTemplate; } // 示例方法:保存与获取数据 public void setData(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getData(String key) { return redisTemplate.opsForValue().get(key); } } ``` 通过上述步骤,可以在Spring Boot 3.2项目中成功集成Redis数据库,并利用其高效的数据处理能力提升应用性能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsdhla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值