溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)

發(fā)布時(shí)間:2023-02-24 13:45:32 來(lái)源:億速云 閱讀:131 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

    一、背景

    在項(xiàng)目服務(wù)是集群部署的時(shí)候,代碼在每個(gè)人都會(huì)有定時(shí)任務(wù),但是如果讓每個(gè)節(jié)點(diǎn)都去跑定時(shí)任務(wù)是不大合適的。SpringBoot 中的 ShedLock 可以很好解決這個(gè)問(wèn)題,下面我將為大家詳細(xì)介紹 SpringBoot 如何集成 ShedLock,而 ShedLock 又是如何實(shí)現(xiàn)分布式定時(shí)的。

    二、ShedLock是什么

    以下是ShedLock鎖提供者,通過(guò)外部存儲(chǔ)實(shí)現(xiàn)鎖,由下圖可知外部存儲(chǔ)集成的庫(kù)還是很豐富:

    SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)

    三、落地實(shí)現(xiàn)

    1.1 引入依賴包

    shedlock所需依賴包:

    <!-- web工程依賴包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>net.javacrumbs.shedlock</groupId>
        <artifactId>shedlock-spring</artifactId>
        <version>4.2.0</version>
    </dependency>
     <!--每個(gè)外部存儲(chǔ)實(shí)例所需依賴包不一樣,這里是jdbc-->
    <dependency>
        <groupId>net.javacrumbs.shedlock</groupId>
        <artifactId>shedlock-provider-jdbc-template</artifactId>
        <version>4.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    依賴包樹(shù)形圖:

    SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)

    1.2 配置數(shù)據(jù)庫(kù)連接信息

    server:
      port: 8105
    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.mysql.cj.jdbc.MysqlDataSource

    1.3 創(chuàng)建Mysql數(shù)據(jù)表

    CREATE TABLE `shedlock` (
    	`name`  varchar(64) NOT NULL COMMENT 'name' ,
    	`lock_until`  timestamp(3) NULL DEFAULT NULL ,
    	`locked_at`  timestamp(3) NULL DEFAULT NULL ,
    	`locked_by`  varchar(255) NULL DEFAULT NULL ,
    	PRIMARY KEY (`name`)
    )
    ENGINE=InnoDB
    DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
    ROW_FORMAT=DYNAMIC
    ;

    1.4 配置LockProvider

    ShedLockConfig.java:

    import net.javacrumbs.shedlock.core.LockProvider;
    import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    import javax.annotation.Resource;
    import javax.sql.DataSource;
    /**
     * @description: Shedlock集成Jdbc配置類
     */
    @Component
    public class ShedLockConfig {
        @Resource
        private DataSource dataSource;
        @Bean
        private LockProvider lockProvider() {
            return new JdbcTemplateLockProvider(dataSource);
        }
    }

    springboot主啟動(dòng)類MerakQuartzApplication:

    import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
    import org.mybatis.spring.annotation.MapperScan;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ThreadPoolExecutor;
    /**
     * @version 1.0
     * @ClassName: MerakQuartzApplication
     * @description: 工單任務(wù)調(diào)度
     */
    // 開(kāi)啟定時(shí)器
    @EnableScheduling
    // 開(kāi)啟定時(shí)任務(wù)鎖,指定一個(gè)默認(rèn)的鎖的時(shí)間30秒
    @EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
    @EnableAsync
    @MapperScan(basePackages = {"com.merak.hyper.automation.persist.**.mapper"})
    @SpringBootApplication(scanBasePackages = {"com.merak.hyper.automation.**"}, exclude = {SecurityAutoConfiguration.class})
    public class MerakQuartzApplication {
        public static final Logger log = LoggerFactory.getLogger(MerakQuartzApplication.class);
        public static void main(String[] args) {
            SpringApplication.run(MerakQuartzApplication.class, args);
        }
        private int taskSchedulerCorePoolSize = 15;
        private int awaitTerminationSeconds = 60;
        private String threadNamePrefix = "taskExecutor-";
        /**
         * @description: 實(shí)例化ThreadPoolTaskScheduler對(duì)象,用于創(chuàng)建ScheduledFuture<?> scheduledFuture
         */
        @Bean
        public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
            ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
            taskScheduler.setPoolSize(taskSchedulerCorePoolSize);
            taskScheduler.setThreadNamePrefix(threadNamePrefix);
            taskScheduler.setWaitForTasksToCompleteOnShutdown(false);
            taskScheduler.setAwaitTerminationSeconds(awaitTerminationSeconds);
            /**需要實(shí)例化線程*/
            taskScheduler.initialize();
    //        isinitialized = true;
            log.info("初始化ThreadPoolTaskScheduler ThreadNamePrefix=" + threadNamePrefix + ",PoolSize=" + taskSchedulerCorePoolSize
                    + ",awaitTerminationSeconds=" + awaitTerminationSeconds);
            return taskScheduler;
        }
        /**
         * @description: 實(shí)例化ThreadPoolTaskExecutor對(duì)象,管理線程
         */
        @Bean("asyncTaskExecutor")
        public Executor taskExecutor() {
            ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
            taskExecutor.setCorePoolSize(5);
            taskExecutor.setMaxPoolSize(50);
            taskExecutor.setQueueCapacity(200);
            taskExecutor.setKeepAliveSeconds(60);
            taskExecutor.setThreadNamePrefix("asyncTaskExecutor-");
            taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
            taskExecutor.setAwaitTerminationSeconds(60);
            //修改拒絕策略為使用當(dāng)前線程執(zhí)行
            taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            //初始化線程池
            taskExecutor.initialize();
            return taskExecutor;
        }
    }

    1.5 創(chuàng)建定時(shí)Job

    DigitalEmpTask:

    package com.merak.hyper.automation.quartz.task;
    import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import java.util.List;
    /**
     * @version 1.0
     * @ClassName: BizOrderTask
     * @description: 任務(wù)隊(duì)列服務(wù)調(diào)度
     */
    @Component
    public class DigitalEmpTask {
        public static final Logger log = LoggerFactory.getLogger(DigitalEmpTask.class);
        @Scheduled(cron = "0/30 * * * * ?")
        @SchedulerLock(name = "digitalEmpTaskScheduler", lockAtMostFor = "PT25S", lockAtLeastFor = "PT25S")
        protected void digitalEmpTaskScheduler() {
            log.info("云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:" + DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS));
            try { 
            } catch (Exception e) {
                log.error("云執(zhí)行調(diào)度中心1調(diào)度失敗,原因:" + e.getMessage());
            }  
        }
    }

    四、結(jié)果分析

    1.分別啟動(dòng)兩個(gè)服務(wù)節(jié)點(diǎn),配置如下:

    server:
      port: 12105
      servlet:
        context-path: /automation-quartz-one

    server:
      port: 12106
      servlet:
        context-path: /automation-quartz-two

    2.運(yùn)行日志(片斷)

    節(jié)點(diǎn)automation-quartz-one 運(yùn)行日志:
    2023-02-22 12:01:00.143 [taskExecutor-1] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:01:00
    2023-02-22 12:05:00.114 [taskExecutor-3] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:05:00
    2023-02-22 12:05:30.122 [taskExecutor-6] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:05:30
    2023-02-22 12:19:30.110 [taskExecutor-3] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:19:30

    節(jié)點(diǎn)automation-quartz-two運(yùn)行日志:
    2023-02-22 12:01:30.109 [taskExecutor-3] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:01:30
    2023-02-22 12:02:00.101 [taskExecutor-1] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:02:00
    2023-02-22 12:02:30.105 [taskExecutor-2] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:02:30
    2023-02-22 12:03:00.118 [taskExecutor-3] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:03:00
    2023-02-22 12:03:30.101 [taskExecutor-4] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:03:30
    2023-02-22 12:04:00.110 [taskExecutor-1] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:04:00
    2023-02-22 12:04:30.111 [taskExecutor-5] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:04:30
    2023-02-22 12:06:00.114 [taskExecutor-13] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:06:00
    2023-02-22 12:06:30.108 [taskExecutor-14] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:06:30
    2023-02-22 12:07:00.114 [taskExecutor-15] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:07:00
    2023-02-22 12:07:30.115 [taskExecutor-1] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:07:30
    2023-02-22 12:08:00.102 [taskExecutor-5] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:08:00
    2023-02-22 12:08:30.103 [taskExecutor-11] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:08:30
    2023-02-22 12:09:00.099 [taskExecutor-6] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:09:00
    2023-02-22 12:09:30.113 [taskExecutor-3] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:09:30
    2023-02-22 12:10:00.107 [taskExecutor-7] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:10:00
    2023-02-22 12:10:30.110 [taskExecutor-15] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:10:30
    2023-02-22 12:11:00.111 [taskExecutor-1] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:11:00
    2023-02-22 12:11:30.100 [taskExecutor-5] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:11:30
    2023-02-22 12:12:00.112 [taskExecutor-11] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:12:00
    2023-02-22 12:12:30.102 [taskExecutor-6] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:12:30
    2023-02-22 12:13:00.097 [taskExecutor-3] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:13:00
    2023-02-22 12:13:30.107 [taskExecutor-14] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:13:30
    2023-02-22 12:14:00.111 [taskExecutor-4] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:14:00
    2023-02-22 12:14:30.106 [taskExecutor-8] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:14:30
    2023-02-22 12:15:00.095 [taskExecutor-9] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:15:00
    2023-02-22 12:15:30.101 [taskExecutor-10] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:15:30
    2023-02-22 12:16:00.105 [taskExecutor-2] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:16:00
    2023-02-22 12:16:30.130 [taskExecutor-12] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:16:30
    2023-02-22 12:17:00.107 [taskExecutor-13] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:17:00
    2023-02-22 12:17:30.113 [taskExecutor-7] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:17:30
    2023-02-22 12:18:00.104 [taskExecutor-15] INFO <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:18:00
    2023-02-22 12:18:30.112 [taskExecutor-1] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:18:30
    2023-02-22 12:19:00.103 [taskExecutor-5] INFO  <DigitalEmpTask:46> - 云執(zhí)行調(diào)度中心1:任務(wù)開(kāi)始執(zhí)行,時(shí)間:2023-02-22 12:19:00

    3、shedlock表記錄信息:

    SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)

    讀到這里,這篇“SpringBoot怎么集成ShedLock實(shí)現(xiàn)分布式定時(shí)任務(wù)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問(wèn)一下細(xì)節(jié)

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

    AI