简介 在springboot使用搭建好的redis集群 添加redis和连接池依赖 !--redis连接池 start-- dependency groupIdorg.apache.commons/groupId artifactIdcommons-pool2/artifactId /dependency !--redis连接池 end-- !--redis start-- d
简介
在springboot使用搭建好的redis集群
添加redis和连接池依赖
<!--redis连接池 start--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!--redis连接池 end--> <!--redis start--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--redis end-->
在配置文件中配置连接池和sentinel
spring: redis: lettuce: pool: max-active: 8 max-idle: 8 max-wait: -1ms min-idle: 0 sentinel: master: mymaster nodes: 192.168.145.128:26379,192.168.145.128:26380,192.168.145.128:26381
创建redis服务
@Service public class RedisServiceImpl implements RedisService { @Autowired private RedisTemplate redisTemplate; @Override public void put(String key, Object value, long seconds) { redisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS); } @Override public Object get(String key) { return redisTemplate.opsForValue().get(key); } }
创建redisController测试redis服务
@RestController public class RedisController { @Autowired private RedisService redisService; @PostMapping(value = "put") public String put(String key,String value,long seconds){ redisService.put(key,value,seconds); return "ok"; } @GetMapping(value = "get") public Object get(String key){ Object o=redisService.get(key); if(o!=null){ return String.valueOf(o); } return "not_ok"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。