当前位置:   article > 正文

SpringBoot集成Redis环境搭建及配置详解_spring redis 配置

spring redis 配置

前言


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 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;

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.4.5</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <groupId>org.pearl</groupId>
  8. <artifactId>spring-boot-redis-demo</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. <name>spring-boot-redis-demo</name>
  11. <description>Demo project for Spring Boot</description>
  12. <properties>
  13. <java.version>1.8</java.version>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-data-redis</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.apache.commons</groupId>
  22. <artifactId>commons-pool2</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>

3、创建启动类;

  1. @SpringBootApplication
  2. public class SpringBootRedisDemoApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringBootRedisDemoApplication.class, args);
  5. }
  6. }

4、添加基础配置;

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

配置类

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

  1. # 核心配置
  2. private int database = 0;
  3. private String url;
  4. private String host = "localhost";
  5. private String username;
  6. private String password;
  7. private int port = 6379;
  8. private boolean ssl;
  9. private Duration timeout;
  10. private Duration connectTimeout;
  11. private String clientName;
  12. private RedisProperties.ClientType clientType;
  13. private RedisProperties.Sentinel sentinel;
  14. private RedisProperties.Cluster cluster;
  15. private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
  16. private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
  17. # 连接池配置
  18. public static class Pool {
  19. private int maxIdle = 8;
  20. private int minIdle = 0;
  21. private int maxActive = 8;
  22. private Duration maxWait = Duration.ofMillis(-1L);
  23. private Duration timeBetweenEvictionRuns;
  24. }
  25. # 集群模式下Lettuce客户端配置
  26. private boolean dynamicRefreshSources = true;
  27. private Duration period;
  28. private boolean adaptive;

spring data redis全部配置项详解

  1. # redis 配置项
  2. # 连接URL,配置后会覆盖host、port等配置,eg: redis://user:password@example.com:6379
  3. spring.redis.url=
  4. # 连接地址
  5. spring.redis.host=127.0.0.1
  6. # Redis服务器连接端口
  7. spring.redis.port=6379
  8. # 连接工厂使用的数据库索引(0-15),默认为0
  9. spring.redis.database=0
  10. # Redis服务器连接用户
  11. spring.redis.username=
  12. # Redis服务器连接密码(默认为空)
  13. spring.redis.password=123456
  14. # 是否启用SSL支持
  15. spring.redis.ssl=false
  16. # 读取超时
  17. spring.redis.timeout=5000
  18. # 连接超时
  19. spring.redis.connect-timeout=10000
  20. # 在与CLIENT SETNAME的连接上设置的客户端名称
  21. spring.redis.client-name=
  22. # 要使用的客户端类型。默认情况下,根据类路径自动检测
  23. spring.redis.client-type=lettuce
  24. # Redis哨兵属性
  25. # Redis服务器名称
  26. spring.redis.sentinel.master=sentinel-redis
  27. # 哨兵节点,以逗号分隔的“ host:port”对列表
  28. spring.redis.sentinel.nodes=127.0.0.1:7000,127.0.0.1:7001
  29. # 用于使用哨兵进行身份验证的密码
  30. spring.redis.sentinel.password=123456
  31. # 集群属性
  32. # 以逗号分隔的“ host:port”对列表,这表示集群节点的“初始”列表,并且要求至少具有一个条目
  33. spring.redis.cluster.nodes=127.0.0.1:7000,127.0.0.1:7001
  34. # 在集群中执行命令时要遵循的最大重定向数
  35. spring.redis.cluster.max-redirects=1
  36. # 拓扑动态感应即客户端能够根据 redis cluster 集群的变化,动态改变客户端的节点情况,完成故障转移。
  37. spring.redis.lettuce.cluster.refresh.adaptive=true
  38. # 是否发现和查询所有群集节点以获取群集拓扑。设置为false时,仅将初始种子节点用作拓扑发现的源
  39. spring.redis.lettuce.cluster.refresh.dynamic-refresh-sources=false
  40. # 集群拓扑刷新周期
  41. spring.redis.lettuce.cluster.refresh.period=1000
  42. # 连接池配置
  43. # 连接池池中“空闲”连接的最大数量。使用负值表示无限数量的空闲连接,默认为8
  44. spring.redis.lettuce.pool.max-idle=8
  45. # 中维护的最小空闲连接数,默认为0
  46. spring.redis.lettuce.pool.min-idle=0
  47. # 连接池可以分配的最大连接数。使用负值表示没有限制,默认为8
  48. spring.redis.lettuce.pool.max-active=8
  49. # 当连接池耗尽时,在抛出异常之前,连接分配应阻塞的最长时间。使用负值无限期等待,默认为-1
  50. spring.redis.lettuce.pool.max-wait=-1
  51. # 空闲连接从运行到退出的时间间隔。当为正时,空闲连接回收线程启动,否则不执行空闲连接回收
  52. spring.redis.lettuce.pool.time-between-eviction-runs=
  53. # 宕机超时时间,默认100ms
  54. spring.redis.lettuce.shutdown-timeout=100

启动项目

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/892811
推荐阅读
相关标签
  

闽ICP备14008679号