环境:SpringBoot2.x
maven增加配置
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springboot2.0的redis整合包多出lettuce连接池,需要增加commons-pool2包
1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce
spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.6.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.25.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
增加yml文件配置
redis:
timeout: 6000ms
password: '123456'
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.182.129:7001
- 192.168.182.129:7002
- 192.168.182.129:7003
- 192.168.182.129:7004
- 192.168.182.129:7005
- 192.168.182.129:7006
lettuce:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
增加一个RedisConfigProperties用于读取配置文件信息
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; @Component @ConfigurationProperties(prefix = "spring.redis")