溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot實現(xiàn)多實例crontab搶占定時任務代碼分享

發(fā)布時間:2021-08-31 15:00:13 來源:億速云 閱讀:280 作者:chen 欄目:編程語言

這篇文章主要介紹“springboot實現(xiàn)多實例crontab搶占定時任務代碼分享”,在日常操作中,相信很多人在springboot實現(xiàn)多實例crontab搶占定時任務代碼分享問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”springboot實現(xiàn)多實例crontab搶占定時任務代碼分享”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

利用redisson實現(xiàn)多實例搶占定時任務

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;@Configurationpublic class Kernel implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {  //設定一個長度10的定時任務線程池  taskRegistrar.setScheduler(Executors.newScheduledThreadPool(4)); }}
RedissonManager.java - 分布式鎖的實現(xiàn)
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@Slf4jpublic 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@Slf4jpublic class CronSynData { @Autowired RedissonManager redissonManager; @Autowired RedisUtil redisUtil; @Autowired DateFormatV1 dateFormatV1; private String lokFlag = ".handleKernel"; private Redisson redisson = null; /* * java定時腳本掛靠實例 * 多實例會有重復調用問題 + 使用Redisson實現(xiàn)分布式鎖 * 業(yè)務邏輯必須加鎖 + 且需要保證 tryLock 等待時間小于cron的最小間隔執(zhí)行時間 * */ @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實現(xiàn)多實例crontab搶占定時任務代碼分享”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI