噹噹Elastic-Job —— 利用zk輕鬆開啓分佈式定時任務

Elastic-Job

Elastic-Job有什麼用

Elastic-job解決以下問題,我們開發定時任務一般都是使用quartz或者spring-task,無論是使用quartz還是spring-task,我們都會至少遇到兩個痛點:

  • 不敢輕易跟着應用服務多節點部署,可能會重複多次執行而引發系統邏輯的錯誤。
  • quartz的集羣僅僅只是用來HA,節點數量的增加並不能給我們的每次執行效率帶來提升,即不能實現水平擴展。

簡單來說也就是在當下分佈式、集羣盛行的背景下解決多臺機器重複執行定時任務或需求修改配置指定單獨固定的定時任務執行機器的煩惱,同時避免如果特定的某臺定時任務執行機器宕機導致定時任務無法執行的尷尬場面。

廢話不多說我想大家在其他地方都能看到這些介紹,我們直接上乾貨!

一、Mavne依賴

<dependency>
	<groupId>com.dangdang</groupId>
	<artifactId>elastic-job-lite-core</artifactId>
	<version>2.1.3</version>
</dependency>
<dependency>
	<groupId>com.dangdang</groupId>
	<artifactId>elastic-job-lite-spring</artifactId>
	<version>2.1.3</version>
</dependency>

二、創建定時作業業務類

package com.zhibo.elastic.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author zhibo.lv
 * @title: TestJob
 * @description: Test定時任務
 * @date 2019/12/2 14:15
 */
@Slf4j
@Component
public class TestJob implements SimpleJob {
    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("[elastic job TestJob] begin----------------");
        //如有設置分片獲取當前任務的分片編號
        int shardingItem = shardingContext.getShardingItem();
        System.out.println("Job 分片處理:" + shardingItem);
        //自行添加業務代碼 ----
        log.info("[elastic job TestJob] end----------------");
    }
}

三、配置elasticJob.xml

digest="zookeeper.username:{zookeeper.username}:{zookeeper.password}" 這個配置一般用不上 下文會有介紹

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd">

     <reg:zookeeper id="regCenter" server-lists="${job.registry.address}" namespace="zhibo-elastic-job"
                    base-sleep-time-milliseconds="10000" max-retries="3" max-sleep-time-milliseconds="30000"
                    digest="${zookeeper.username}:${zookeeper.password}"/>

     <job:simple id="activeAdminInfoJob" class="com.zhibo.elastic.job.TestJob"
                 registry-center-ref="regCenter"
                 sharding-total-count="1"
                 cron="0 * * * * ? "
                 sharding-item-parameters="0=0"
                 monitor-execution="false"
                 monitor-port="9888"
                 failover="true"
                 description="Test Job"
                 disabled="false"
                 overwrite="true"/>
</beans>

別忘了在applicationContext.xml裏面引入elasticJob.xml

reg:zookeeper 命名空間屬性詳細說明

屬性名 類型 是否必填 缺省值 描述
id String 註冊中心在Spring容器中的Id
server-lists String Zookeeper服務器地址及端口
多個地址用英文逗號隔開
namespace String Zookeeper的命名空間
建議每個項目定義一個唯一的名字用於區分
base-sleep-time-milliseconds int 1000 等待重試的間隔時間的初始值(毫秒)
max-sleep-time-milliseconds int 3000 等待重試的間隔時間的最大值(毫秒)
max-retries int 3 最大重試次數
session-timeout-milliseconds int 60000 會話超時時間(毫秒)
connection-timeout-milliseconds int 15000 連接超時時間(毫秒)
digest String 直連不鑑權 連接Zookeeper的權限令牌

job:simple 命名空間屬性詳細說明

屬性名 類型 是否必填 缺省值 描述
id String 作業名稱
同一個命名空間下保持唯一
registry-center-ref String 註冊中心Bean的引用
與reg:zookeeper 的id屬性的值一致即可
cron String cron表達式,用於配置作業觸發時間
隨便找個了表達式的學習地址
sharding-total-count int 作業分片總數
不需要分片就寫1
class String 作業實現類,需實現ElasticJob接口,腳本型作業不需要配置
sharding-item-parameters String 分片序列號和參數用等號分隔,多個鍵值對用逗號分隔分片序列號從0開始,不可大於或等於作業分片總數如:
0=a,1=b,2=c
job-parameter String 作業自定義參數
代碼中通過shardingContext.getJobParameter()獲取
monitor-execution boolean true 監控作業運行時狀態
每次作業執行時間和間隔時間均非常短的情況,建議不監控作業運行時狀態以提升效率。因爲是瞬時狀態,所以無必要監控。請用戶自行增加數據堆積監控。並且不能保證數據重複選取,應在作業中實現冪等性。
每次作業執行時間和間隔時間均較長的情況,建議監控作業運行時狀態,可保證數據不會重複選取。
monitor-port int -1 作業監控端口
max-time-diff-seconds int -1 最大允許的本機與註冊中心的時間誤差秒數
如果時間誤差超過配置秒數則作業啓動時將拋異常
配置爲-1表示不校驗時間誤差
failover boolean false 是否開啓失效轉移
僅monitorExecution開啓,失效轉移纔有效
misfire boolean true 是否開啓錯過任務重新執行
description String 作業描述信息
disabled boolean false 作業是否禁止啓動
可用於部署作業時,先禁止啓動,部署結束後統一啓動
overwrite boolean false 本地配置是否可覆蓋註冊中心配置
建議覆蓋避免配置修改後不生效
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章