xxl-job環(huán)境搭建以及入門

官方文檔:

- [中文文檔](https://www.xuxueli.com/xxl-job/)

- [English Documentation](https://www.xuxueli.com/xxl-job/en/)

源碼:

[https://github.com/xuxueli/xxl-job](https://github.com/xuxueli/xxl-job) | [Download](https://github.com/xuxueli/xxl-job/releases)

[http://gitee.com/xuxueli0323/xxl-job](http://gitee.com/xuxueli0323/xxl-job) | [Download](http://gitee.com/xuxueli0323/xxl-job/releases)


關(guān)于調(diào)度中心和執(zhí)行器的架構(gòu)圖


xxl-job主要是分為兩個部分,調(diào)度中心主要是進(jìn)行定時作業(yè)的配置,執(zhí)行器則是各個業(yè)務(wù)系統(tǒng)執(zhí)行定時作業(yè)的具體邏輯。

關(guān)于調(diào)度中心:

調(diào)度中心這塊比較簡單,直接拉源碼中admin模塊作為一個項(xiàng)目運(yùn)行,調(diào)度中心就完成了,需要修改的配置如下:

第一步:初始化數(shù)據(jù)庫,把源碼中/xxl-job/doc/db/tables_xxl_job.sql 的sql腳本運(yùn)行,數(shù)據(jù)庫就初始化完成了。

第二步:修改配置文件

### 調(diào)度中心JDBC鏈接:初始化數(shù)據(jù)地址

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai

spring.datasource.username=root

spring.datasource.password=root_pwd

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

### 報警郵箱 (不需要可以不配置)

spring.mail.host=smtp.qq.com

spring.mail.port=25

spring.mail.username=xxx@qq.com

spring.mail.password=xxx

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

### 調(diào)度中心通訊TOKEN [選填]:非空時啟用;

xxl.job.accessToken=

### 調(diào)度中心國際化配置 [必填]: 默認(rèn)為 "zh_CN"/中文簡體, 可選范圍為 "zh_CN"/中文簡體, "zh_TC"/中文繁體 and "en"/英文;

xxl.job.i18n=zh_CN

## 調(diào)度線程池最大線程配置【必填】

xxl.job.triggerpool.fast.max=200

xxl.job.triggerpool.slow.max=100

### 調(diào)度中心日志表數(shù)據(jù)保存天數(shù) [必填]:過期日志自動清理;限制大于等于7時生效,否則, 如-1,關(guān)閉自動清理功能;

xxl.job.logretentiondays=30

第三步,啟動項(xiàng)目,調(diào)度中心完成,調(diào)度中心訪問地址:http://localhost:8080/xxl-job-admin? ?,默認(rèn)登錄賬號 “admin/123456”


關(guān)于執(zhí)行器(筆者用的項(xiàng)目是基于springboot的,以下只是一個簡單的demo):

目前,筆者用的是spring容器方式自動掃描任務(wù)并注入執(zhí)行器,主要是文檔中提到的3.2 BEAN模式(方法形式)這種模式去構(gòu)建執(zhí)行器,這里有個坑是2.0.0及以下版本好像不支持@XxlJob注解。筆者再搭建執(zhí)行器主要用到的版本是2.3.0。

第一步:引入maven依賴

<dependency>

? ? <groupId>com.xuxueli</groupId>

? ? <artifactId>xxl-job-core</artifactId>

? ? <version>2.3.0</version>

</dependency>

第二步:執(zhí)行器的相關(guān)配置修改

### 調(diào)度中心部署跟地址 [選填]:如調(diào)度中心集群部署存在多個地址則用逗號分隔。執(zhí)行器將會使用該地址進(jìn)行"執(zhí)行器心跳注冊"和"任務(wù)結(jié)果回調(diào)";為空則關(guān)閉自動注冊;

xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

### 執(zhí)行器通訊TOKEN [選填]:非空時啟用,與上邊的調(diào)度中心保持一致

xxl.job.accessToken=

### 執(zhí)行器AppName [選填]:執(zhí)行器心跳注冊分組依據(jù);為空則關(guān)閉自動注冊

xxl.job.executor.appname=xxl-job-executor-sample

### 執(zhí)行器注冊 [選填]:優(yōu)先使用該配置作為注冊地址,為空時使用內(nèi)嵌服務(wù) ”IP:PORT“ 作為注冊地址。從而更靈活的支持容器類型執(zhí)行器動態(tài)IP和動態(tài)映射端口問題。

xxl.job.executor.address=

### 執(zhí)行器IP [選填]:默認(rèn)為空表示自動獲取IP,多網(wǎng)卡時可手動設(shè)置指定IP,該IP不會綁定Host僅作為通訊實(shí)用;地址信息用于 "執(zhí)行器注冊" 和 "調(diào)度中心請求并觸發(fā)任務(wù)";

xxl.job.executor.ip=

### 執(zhí)行器端口號 [選填]:小于等于0則自動獲??;默認(rèn)端口為9999,單機(jī)部署多個執(zhí)行器時,注意要配置不同執(zhí)行器端口;

xxl.job.executor.port=9999

### 執(zhí)行器運(yùn)行日志文件存儲磁盤路徑 [選填] :需要對該路徑擁有讀寫權(quán)限;為空則使用默認(rèn)路徑;

xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler

### 執(zhí)行器日志文件保存天數(shù) [選填] : 過期日志自動清理, 限制值大于等于3時生效; 否則, 如-1, 關(guān)閉自動清理功能;

xxl.job.executor.logretentiondays=30

第三步:添加XxlJobConfig.java,初始化xxl-job配置。初始化參數(shù)與上邊的properties參數(shù)一一對應(yīng),用不到的可以自行去掉。

@Bean

public XxlJobSpringExecutor xxlJobExecutor() {

? ? logger.info(">>>>>>>>>>> xxl-job config init.");

? ? XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();

? ? xxlJobSpringExecutor.setAdminAddresses(adminAddresses);

? ? xxlJobSpringExecutor.setAppname(appname);

? ? xxlJobSpringExecutor.setIp(ip);

? ? xxlJobSpringExecutor.setPort(port);

? ? xxlJobSpringExecutor.setAccessToken(accessToken);

? ? xxlJobSpringExecutor.setLogPath(logPath);

? ? xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

? ? return xxlJobSpringExecutor;

}

第四步:執(zhí)行器任務(wù)編碼


簡單的定時任務(wù)

通過@Component把javabean注入到spring容器中,然后@XxlJob("調(diào)度任務(wù)handler名稱"),調(diào)度中心新增定時任務(wù)時候會根據(jù)對應(yīng)的handler執(zhí)行對應(yīng)的定時任務(wù)。

第五步:項(xiàng)目打包并運(yùn)行,執(zhí)行器到此完成。

如何使用調(diào)度任務(wù):


新增執(zhí)行器

第一步:新增執(zhí)行器,AppName與執(zhí)行器配置屬性xxl.job.executor.appname對應(yīng),主要是用于分組管理以及自動注冊,名稱隨意寫。appname不為空,執(zhí)行器會自動注冊,如果為空則需手動錄入執(zhí)行器機(jī)器ip。


新增調(diào)度任務(wù)

第二步,新增調(diào)度任務(wù),其中JobHandler名稱與執(zhí)行器編碼過程中的命名一致,調(diào)度中心才能執(zhí)行到對應(yīng)的定時任務(wù)。配置完以上信息后,啟動定時任務(wù)。至此,調(diào)度中心以及執(zhí)行器就搭建完成。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容