springboot实现多实例crontab抢占定时任务(实例代码)
程序员文章站
2023-11-13 15:57:28
github:
wechat:minghui-666
利用redisson实现多实例抢占定时任务
pom.xml
<...
github:
wechat:minghui-666
利用redisson实现多实例抢占定时任务
pom.xml
<dependency> <groupid>org.redisson</groupid> <artifactid>redisson</artifactid> <version>3.12.0</version> </dependency>
kernel.java - 重写多线程调度
package com.brand.log.scheduler; import org.springframework.context.annotation.configuration; import org.springframework.scheduling.annotation.schedulingconfigurer; import org.springframework.scheduling.config.scheduledtaskregistrar; import java.util.concurrent.executors; @configuration public class kernel implements schedulingconfigurer { @override public void configuretasks(scheduledtaskregistrar taskregistrar) { //设定一个长度10的定时任务线程池 taskregistrar.setscheduler(executors.newscheduledthreadpool(4)); } }
redissonmanager.java - 分布式锁的实现
package com.brand.log.util; import lombok.extern.slf4j.slf4j; import org.redisson.redisson; import org.redisson.config.config; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; import javax.annotation.postconstruct; @component @slf4j public class redissonmanager { @value("${spring.redis.host}") private string host; @value("${spring.redis.port}") private int port; private redisson redisson = null; private config config = new config(); @postconstruct private void init() { try { config.usesingleserver().setaddress("redis://" + host + ":" + port); log.info("redisson address {} {}", host, port); redisson = (redisson) redisson.create(config); log.info("redisson 初始化完成"); } catch (exception e) { log.error("init redisson error ", e); } } public redisson getredisson() { return redisson; } }
cronsyndata.java
package com.brand.log.scheduler; import com.brand.log.util.dateformatv1; import com.brand.log.util.redisutil; import com.brand.log.util.redissonmanager; import lombok.extern.slf4j.slf4j; import org.redisson.redisson; import org.redisson.api.rlock; import org.springframework.beans.factory.annotation.autowired; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component; import java.util.concurrent.timeunit; @component @slf4j public class cronsyndata { @autowired redissonmanager redissonmanager; @autowired redisutil redisutil; @autowired dateformatv1 dateformatv1; private string lokflag = ".handlekernel"; private redisson redisson = null; /* * java定时脚本挂靠实例 * 多实例会有重复调用问题 + 使用redisson实现分布式锁 * 业务逻辑必须加锁 + 且需要保证 trylock 等待时间小于cron的最小间隔执行时间 * */ @scheduled(cron = "*/10 * * * * *") public void handlekernel() { redisson = redissonmanager.getredisson(); if (redisson != null) { rlock lock = redisson.getlock(this.getclass().getname() + lokflag); boolean stat = false; try { // 尝试加锁,立即返回,最多等待5s自动解锁 stat = lock.trylock(0, 5, timeunit.seconds); if (stat) { log.info("{} 取锁成功!{}",this.getclass().getname(), thread.currentthread().getname()); redisutil.checkcount("log:limit_", dateformatv1.getdate("hh", "gmt+8"), 60*10, 1000); } else { log.info("{}没有获取到锁:{}", this.getclass().getname(), thread.currentthread().getname()); } } catch (interruptedexception e) { log.error("redisson 获取分布式锁异常", e); if (!stat){ return; } lock.unlock(); } } } }
kibana - 6个实例
总结
以上所述是小编给大家介绍的springboot实现多实例crontab抢占定时任务,希望对大家有所帮助