Spring Boot 实现分布式定时任务锁 shedlock
分布式定时任务有好几种实现,我这里用了mysql和redis来实现
我们在项目中经常要用到定时任务,当有天服务器压力过大,我们就会增加服务器的数量,这时每台服务器的定时任务都在同一时间运行,导致我们需要同一时间运行一次的定时任务跑了多次,这时就需要解决这个问题,就用到了 shedlock
下面这是我项目里写的定时任务,里面的方法没有写出来
@Component
public class TimeOutSMSScheduledLock {
@Autowired
public TimeOutSMSService timeOutSMSService;
//这时定时任务
@Scheduled(cron = "0 0 9,14 * * ?")
public void sendSMS(){
//具体的方法我就不写了,你可以随便写也可以打印日志
timeOutSMSService.timeOutSendSMS();
}
}
1.mysql实现分布式定时任务锁
导包
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<