前言:写这篇文章的目的主要是这方面的知识我是第一次实际运用到,在项目里面有个功能,需要登录的时候根据手机号发送短信验证码,我看了公司的代码是用redis过期key监控实现的,由于之前没有接触过这类,现在写下来记一下,主要是简单的实现对redis过期key的监控。
为了避免redis与spring存在版本冲突导致的不必要的麻烦,我所使用的spring版本是 2.2.2.RELEASE版本,使用自带的redis-starter,pom依赖最简单化为:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.example
demo
0.0.1-SNAPSHOT
domeOne
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-maven-plugin
配置application.yml为:
spring:
redis:
port: 6379
host: 127.0.0.1
password: redis
database: 1
timeout: 30000
server:
port: 8081
@Slf4j
@RestController
public class Controller {
@Autowired
private RedisTemplate
这些操作较为简单,一般不会出错,访问127.0.0.1:8081/test 正常打印日志则说明连接正常。
在该配置类里面添加监控Bean,设置监控topic(全部key过期皆会被监测到),这里写得简单了,正常还需要设置线程池以及封装RedisTemplate。
@Configuration
public class RedisConfig implements ApplicationContextAware {
@Autowired
private RedisTemplate
onMessage方法来自于MessageListener接口,主要就是对过期key进行监控
@Slf4j
@Component
public class MyMessageListener implements MessageListener, ApplicationContextAware {
private ApplicationContext applicationContext;
@Autowired
private IMessageHandler messageHandler;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public MyMessageListener() {
}
@Override
public void onMessage(Message message, byte[] bytes) {
log.info("进入监控方法....");
String body = new String(message.getBody());
log.info("监控到的body为: {}", body);
messageHandler.handleMessage(body);
}
}
public interface IMessageHandler {
/**
* 处理redis的key值过期事件
*/
void handleMessage(String body);
}
@Slf4j
@Service
public class MessageServiceImpl implements IMessageHandler {
@Override
public void handleMessage(String body) {
log.info("开始处理消息");
}
}
这里为了方便,将过期key设置为10秒过期,执行方法10秒后,代码能够自动处理消息则说明成功
@Slf4j
@RestController
public class Controller {
@Autowired
private RedisTemplate
redis需要开启监控过期key需要修改redis.conf文件(windows的文件名叫做redis.windows.conf),修改后需重启redis,否则不生效。
设置为 notify-keyspace-events "Ex"