Spring Boot3整合Redis

⛰️个人主页: 蒾酒

🔥系列专栏:《spring boot实战》

🌊山高路远,行路漫漫,终有归途。


目录

前置条件

1.导依赖

2.配置连接信息以及连接池参数

3.配置序列化方式

4.编写测试


前置条件

已经初始化好一个spring boot项目且版本为3X,项目可正常启动。

作者版本为3.2.2

初始化教程:

新版idea(2023)创建spring boot3项目-CSDN博客https://blog.csdn.net/qq_62262918/article/details/135785412?spm=1001.2014.3001.5501

1.导依赖

pom.xml:

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

如果还没安装redis可以参照这篇:

阿里云ECS使用docke搭建redis服务-CSDN博客https://blog.csdn.net/qq_62262918/article/details/135707725?spm=1001.2014.3001.5502

2.配置连接信息以及连接池参数

application.yml:

XML 复制代码
server:
  port: 8080
spring:
  data:
    redis: # Redis连接配置
      host: localhost  # Redis主机地址
      port: 6379  # Redis端口号
      password: 123456  # 访问Redis所需密码
      database: 0  # 使用的数据库编号
      lettuce: #Lettuce客户端配置
        pool: # 连接池配置
          max-active: 8  # 最大活跃连接数
          max-wait: -1  # 最大等待时间(-1表示无限等待)
          max-idle: 8  # 最大空闲连接数
          min-idle: 0  # 最小空闲连接数

修改为你的连接信息即可。

这里要说的是:

Lettuce和Jedis两者都是Java连接Redis的客户端

选择使用Lettuce而不是Jedis的原因如下:

线程安全性:

  • Lettuce 是基于 Netty 构建的,它使用异步和事件驱动的方式处理连接。因此,它可以在多个线程之间共享一个连接而不需要额外的同步,因此在高并发环境下更高效。

  • Jedis 是基于阻塞 I/O 的,并且不是线程安全的,如果在多个线程中共享同一个 Jedis 实例,需要使用连接池进行同步管理,这可能引入额外的复杂性。
    连接方式:

  • Lettuce 支持基于 Reactive Streams 的响应式编程模型,能够更好地与 Spring Reactor、Project Reactor 等框架集成,提供异步和非阻塞的操作。

  • Jedis 是同步的,并且在执行某些操作时会阻塞线程,这可能会影响应用程序的性能和响应性。
    性能和扩展性:

  • Lettuce 的设计目标是高性能和扩展性,它可以更好地利用 Redis 4.0 中引入的一些新特性(如 Redis Sentinel 和 Redis Cluster)。

  • Jedis 的设计目标更偏向于简单易用,对于一些特殊的 Redis 集群模式可能支持不够完善。
    维护和更新:

  • Lettuce 是一个活跃的项目,并且持续地得到更新和改进。

  • Jedis 在某些方面已经相对稳定,并且在一段时间内没有大的更新。

3.配置序列化方式

config目录下新建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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author mijiupro
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        // 设置key和value的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // 设置key的序列化器为StringRedisSerializer
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // 设置value的序列化器为JdkSerializationRedisSerializer
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // 设置hash key的序列化器为StringRedisSerializer
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); // 设置hash value的序列化器为JdkSerializationRedisSerializer
        redisTemplate.afterPropertiesSet(); // 初始化RedisTemplate
        return redisTemplate; // 返回配置好的RedisTemplate
    }
}

4.编写测试

java 复制代码
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTest {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    //测试redis
    void contextLoads2() {
        //添加缓存键值对name:mijiu并设置过期时间为1小时
        stringRedisTemplate.opsForValue().set("name","mijiu",10, TimeUnit.SECONDS);
        System.out.println(stringRedisTemplate.opsForValue().get("name"));

    }
}

运行测试

测试成功,整合完毕!

相关推荐
hello早上好10 分钟前
JDK 代理原理
java·spring boot·spring
PanZonghui15 分钟前
Centos项目部署之运行SpringBoot打包后的jar文件
linux·spring boot
PanZonghui15 分钟前
Centos项目部署之Java安装与配置
java·linux
GreatSQL社区29 分钟前
用systemd管理GreatSQL服务详解
数据库·mysql·greatsql
掘根29 分钟前
【MySQL进阶】错误日志,二进制日志,mysql系统库
数据库·mysql
weixin_4383354032 分钟前
基础知识:mysql-connector-j依赖
数据库·mysql
沉着的码农1 小时前
【设计模式】基于责任链模式的参数校验
java·spring boot·分布式
小明铭同学1 小时前
MySQL 八股文【持续更新ing】
数据库·mysql
zyxzyx6661 小时前
Flyway 介绍以及与 Spring Boot 集成指南
spring boot·笔记
Mr_Xuhhh1 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构