欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù)(實(shí)例代碼)

 更新時(shí)間:2020年01月08日 14:04:41   作者:總有刁民想呀么想害朕  
這篇文章主要介紹了springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

github: https://github.com/jiasion/eslog 

wechat:minghui-666

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

pom.xml

<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.12.0</version>
</dependency>

Kernel.java - 重寫多線程調(diào)度

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) {
  //設(shè)定一個(gè)長(zhǎng)度10的定時(shí)任務(wù)線程池
  taskRegistrar.setScheduler(Executors.newScheduledThreadPool(4));
 }
}

RedissonManager.java  -  分布式鎖的實(shí)現(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
@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定時(shí)腳本掛靠實(shí)例
 * 多實(shí)例會(huì)有重復(fù)調(diào)用問(wèn)題 + 使用Redisson實(shí)現(xiàn)分布式鎖
 * 業(yè)務(wù)邏輯必須加鎖 + 且需要保證 tryLock 等待時(shí)間小于cron的最小間隔執(zhí)行時(shí)間
 * */
 @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自動(dòng)解鎖
    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("{}沒(méi)有獲取到鎖:{}", this.getClass().getName(), Thread.currentThread().getName());
    }
   } catch (InterruptedException e) {
    log.error("Redisson 獲取分布式鎖異常", e);
    if (!stat){
     return;
    }
    lock.unlock();
   }
  }
 }
}

kibana - 6個(gè)實(shí)例

總結(jié)

以上所述是小編給大家介紹的springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 淺析Java中StringBuffer和StringBuilder的使用

    淺析Java中StringBuffer和StringBuilder的使用

    當(dāng)對(duì)字符串進(jìn)行修改的時(shí)候,需要使用 StringBuffer 和 StringBuilder 類。本文就來(lái)和大家簡(jiǎn)單聊聊這二者的使用與區(qū)別吧,希望對(duì)大家有所幫助
    2023-04-04
  • 淺談MyBatis Plus主鍵設(shè)置策略

    淺談MyBatis Plus主鍵設(shè)置策略

    本文主要介紹了MyBatis Plus主鍵設(shè)置策略,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • spring-Kafka中的@KafkaListener深入源碼解讀

    spring-Kafka中的@KafkaListener深入源碼解讀

    本文主要通過(guò)深入了解源碼,梳理從spring啟動(dòng)到真正監(jiān)聽(tīng)kafka消息的這套流程,從spring啟動(dòng)開(kāi)始處理@KafkaListener,本文結(jié)合實(shí)例流程圖給大家講解的非常詳細(xì),需要的朋友參考下
    2023-02-02
  • spring是如何實(shí)現(xiàn)聲明式事務(wù)的

    spring是如何實(shí)現(xiàn)聲明式事務(wù)的

    這篇文章主要介紹了spring是如何實(shí)現(xiàn)聲明式事務(wù)的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 淺談java的byte數(shù)組的不同寫法

    淺談java的byte數(shù)組的不同寫法

    下面小編就為大家?guī)?lái)一篇淺談java的byte數(shù)組的不同寫法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • C#創(chuàng)建Web應(yīng)用程序代碼實(shí)例

    C#創(chuàng)建Web應(yīng)用程序代碼實(shí)例

    本文主要通過(guò)實(shí)例代碼介紹了C#創(chuàng)建Web應(yīng)用程序,需要的朋友可以參考下
    2017-04-04
  • Spring Boot統(tǒng)一返回體的踩坑記錄

    Spring Boot統(tǒng)一返回體的踩坑記錄

    這篇文章主要給大家介紹了關(guān)于Spring Boot統(tǒng)一返回體踩坑的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • java的jps命令使用詳解

    java的jps命令使用詳解

    這篇文章介紹了java的jps命令使用詳解,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • springboot實(shí)現(xiàn)熱部署操作方法

    springboot實(shí)現(xiàn)熱部署操作方法

    這篇文章主要介紹了springboot實(shí)現(xiàn)熱部署操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • nacos配置中心遠(yuǎn)程調(diào)用讀取不到配置文件的解決

    nacos配置中心遠(yuǎn)程調(diào)用讀取不到配置文件的解決

    這篇文章主要介紹了nacos配置中心遠(yuǎn)程調(diào)用讀取不到配置文件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01

最新評(píng)論