[redis]springboot中快速使用redis
一、POM.xml (maven导入相应的包)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
二、Application.properties配置集群(只要配置master)
#配置redis---start---
#只配置一台
#spring.redis.host=55.6.75.252
#只配置一台
#spring.redis.port=6380
#配置集群
spring.redis.cluster.nodes=55.6.75.252:6380,55.6.75.252:6382,55.6.75.252:6384
spring.redis.cluster.max-redirects=3
spring.redis.password=Cmb@2020
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000
#配置redis---end---
三、编写@Service
@Service public class RedisService { @Autowired RedisTemplate redisTemplate; public void setKV(String key,Object value) { redisTemplate.opsForValue().set ( key,value ); } public Object getV(String key) { return redisTemplate.opsForValue ().get ( key ); } }
四、后续代码直接以下使用即可。
@Resource RedisService redisService;