赞
踩
Redis作为当前最火的NoSQL数据库,支持很多语言客户端操作Redis。
而SpringBoot作为java当前最火的开发框架,提供了Spring-data-redis框架实现对Redis的各种操作。
在springboot1.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系列的一部分,可轻松配置并从Spring应用程序访问Redis。它提供了与商店交互的低层和高层抽象,使用户摆脱了基础设施方面的顾虑。
Spring Data使用Spring框架的核心功能,包括:
虽然你不需要了解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>

3、创建启动类;
- @SpringBootApplication
- public class SpringBootRedisDemoApplication {
-
-
- public static void main(String[] args) {
-
-
- SpringApplication.run(SpringBootRedisDemoApplication.class, args);
- }
- }
4、添加基础配置;
- 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;

- # 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、 启动项目,基础环境搭建完成;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。