简介
在springboot使用搭建好的redis集群
添加redis和连接池依赖
org.apache.commons
commons-pool2
org.springframework.boot
spring-boot-starter-data-redis
在配置文件中配置连接池和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";
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。