一、简介
使用RedisConfig配置类的方式进行Redis standalone(单节点)、cluster(主从集群)、sentinel(哨兵集群)的灵活配置。
二、代码
2.1 依赖
<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>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!-- 常用文件IO转换 MultipartFile -> File -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 常用工具 StringUtils -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
2.2 YML配置文件
application.yml
spring:
profiles:
active: standalone
# active: cluster
# active: sentinel
application-cluster.yml
server:
port: 8080
servlet:
context-path: /testRedis
spring:
application:
name: TestRedis
redis:
database: 0
lettuce:
pool:
max-active