12、Spring Boot集成Redis环境搭建及配置详解

前言

Redis作为当前最火的NoSql数据库,支持很多语言客户端操作Redis。

 

而SpringBoot最为Java当前最火的开发框架,提供了spring-data-redis框架实现对Redis的各种操作。

在springboot 1.5.x版本的默认的Redis客户端是 Jedis实现的,springboot 2.x版本中默认客户端是用 lettuce实现的。

Lettuce 和 jedis 的都是连接 Redis Server的客户端,Jedis 在实现上是直连 redis server,多线程环境下非线程安全,除非使用连接池,为每个 redis实例增加物理连接。

Lettuce 是 一种可伸缩,线程安全,完全非阻塞的Redis客户端,多个线程可以共享一个RedisConnection,它利用Netty NIO 框架来高效地管理多个连接,从而提供了异步和同步数据访问方式,用于构建非阻塞的反应性应用程序。

 

spring-data-redis

Spring Data Redis

Spring Data Redis是较大的Spring Data系列的一部分,可轻松配置并从Spring应用程序访问Redis。它提供了与商店交互的低层和高层抽象,使用户摆脱了基础设施方面的顾虑。

官方文档

特性

  • 连接包是跨多个Redis驱动程序(Lettuce和Jedis)的低级抽象。
  • 将Redis驱动程序异常转换为Spring的可移植数据访问异常层次结构。
  • RedisTemplate提供了用于执行各种Redis操作,异常转换和序列化支持的高级抽象。
  • Pubsub支持(例如,消息驱动的POJO的MessageListenerContainer)。
  • Redis Sentinel和Redis Cluster支持。
  • 使用Lettuce驱动程序的反应性API。
  • JDK,String,JSON和Spring Object / XML映射序列化器。
  • Redis之上的JDK Collection实现。
  • 原子计数器支持类。
  • 排序和流水线功能。
  • 专门支持SORT,SORT / GET模式和返回的批量值。
  • Spring 3.1缓存抽象的Redis实现。
  • Repository接口的自动实现,包括使用的自定义查询方法的支持@EnableRedisRepositories。
  • CDI对存储库的支持。

Spring框架的核心功能

Spring Data使用Spring框架的核心功能,包括:

  • IoC容器
  • 类型转换系统
  • 表达语言
  • JMX整合
  • DAO异常层次结构。

虽然您不需要了解Spring API,但了解它们背后的概念很重要。至少,应该熟悉控制反转(IoC)的概念,并且您应该熟悉选择使用的任何IoC容器。

Redis支持的核心功能可以直接使用,而无需调用Spring容器的IoC服务。这很像JdbcTemplate,无需使用Spring容器的任何其他服务就可以“独立”使用。要利用Spring Data Redis的所有功能,例如存储库支持,您需要配置库的某些部分以使用Spring。

案例

创建项目

1、 创建一个普通SpringBoot项目;
2、 添加pom;

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.pearl</groupId>
<artifactId>spring-boot-redis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-redis-demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

1、 创建启动类;

@SpringBootApplication
public class SpringBootRedisDemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringBootRedisDemoApplication.class, args);
    }
}

1、 添加基础配置;

server.port=9000
spring.application.name=spring-boot-redis-demo

配置类

RedisProperties配置类,提供了Redis集成Boot的相关配置。

# 核心配置
    private int database = 0;
    private String url;
    private String host = "localhost";
    private String username;
    private String password;
    private int port = 6379;
    private boolean ssl;
    private Duration timeout;
    private Duration connectTimeout;
    private String clientName;
    private RedisProperties.ClientType clientType;
    private RedisProperties.Sentinel sentinel;
    private RedisProperties.Cluster cluster;
    private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
    private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
# 连接池配置
 public static class Pool {

        private int maxIdle = 8;
        private int minIdle = 0;
        private int maxActive = 8;
        private Duration maxWait = Duration.ofMillis(-1L);
        private Duration timeBetweenEvictionRuns;
}
# 集群模式下Lettuce客户端配置
        private boolean dynamicRefreshSources = true;
        private Duration period;
        private boolean adaptive;

spring data redis全部配置项详解

# redis 配置项
# 连接URL,配置后会覆盖host、port等配置,eg: redis://user:password@example.com:6379
spring.redis.url=
# 连接地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# 连接工厂使用的数据库索引(0-15),默认为0
spring.redis.database=0
# Redis服务器连接用户
spring.redis.username=
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# 是否启用SSL支持
spring.redis.ssl=false
# 读取超时
spring.redis.timeout=5000
# 连接超时
spring.redis.connect-timeout=10000
# 在与CLIENT SETNAME的连接上设置的客户端名称
spring.redis.client-name=
# 要使用的客户端类型。默认情况下,根据类路径自动检测
spring.redis.client-type=lettuce
# Redis哨兵属性
# Redis服务器名称
spring.redis.sentinel.master=sentinel-redis
# 哨兵节点,以逗号分隔的“ host:port”对列表
spring.redis.sentinel.nodes=127.0.0.1:7000,127.0.0.1:7001
# 用于使用哨兵进行身份验证的密码
spring.redis.sentinel.password=123456

# 集群属性
# 以逗号分隔的“ host:port”对列表,这表示集群节点的“初始”列表,并且要求至少具有一个条目
spring.redis.cluster.nodes=127.0.0.1:7000,127.0.0.1:7001
# 在集群中执行命令时要遵循的最大重定向数
spring.redis.cluster.max-redirects=1
# 拓扑动态感应即客户端能够根据 redis cluster 集群的变化,动态改变客户端的节点情况,完成故障转移。
spring.redis.lettuce.cluster.refresh.adaptive=true
# 是否发现和查询所有群集节点以获取群集拓扑。设置为false时,仅将初始种子节点用作拓扑发现的源
spring.redis.lettuce.cluster.refresh.dynamic-refresh-sources=false
# 集群拓扑刷新周期
spring.redis.lettuce.cluster.refresh.period=1000

# 连接池配置
# 连接池池中“空闲”连接的最大数量。使用负值表示无限数量的空闲连接,默认为8
spring.redis.lettuce.pool.max-idle=8
# 中维护的最小空闲连接数,默认为0
spring.redis.lettuce.pool.min-idle=0
# 连接池可以分配的最大连接数。使用负值表示没有限制,默认为8
spring.redis.lettuce.pool.max-active=8
# 当连接池耗尽时,在抛出异常之前,连接分配应阻塞的最长时间。使用负值无限期等待,默认为-1
spring.redis.lettuce.pool.max-wait=-1
# 空闲连接从运行到退出的时间间隔。当为正时,空闲连接回收线程启动,否则不执行空闲连接回收
spring.redis.lettuce.pool.time-between-eviction-runs=
# 宕机超时时间,默认100ms
spring.redis.lettuce.shutdown-timeout=100

启动项目

1、 添加配置,删除掉上面中sentinel及cluster的相关配置即可;
2、 启动项目,基础环境搭建完成;

&nbsp;