前言
xxl-job 是一個分布式任務(wù)調(diào)度平臺,它提供了強大的任務(wù)調(diào)度和執(zhí)行能力,可以幫助我們實現(xiàn)任務(wù)的自動化調(diào)度和執(zhí)行。本文將介紹如何在 Docker 環(huán)境下部署 xxl-job,并將其與 Spring Boot 進行整合。
初始化數(shù)據(jù)庫
數(shù)據(jù)庫腳本:tables_xxl_job-2.4.0.sql
# XXL-JOB v2.4.0
# Copyright (c) 2015-present, xuxueli.
CREATE database if NOT EXISTS `xxl_job` default character set utf8mb4 collate utf8mb4_unicode_ci;
use `xxl_job`;
SET NAMES utf8mb4;
CREATE TABLE `xxl_job_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_group` int(11) NOT NULL COMMENT '執(zhí)行器主鍵ID',
`job_desc` varchar(255) NOT NULL,
`add_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`author` varchar(64) DEFAULT NULL COMMENT '作者',
`alarm_email` varchar(255) DEFAULT NULL COMMENT '報警郵件',
`schedule_type` varchar(50) NOT NULL DEFAULT 'NONE' COMMENT '調(diào)度類型',
`schedule_conf` varchar(128) DEFAULT NULL COMMENT '調(diào)度配置,值含義取決于調(diào)度類型',
`misfire_strategy` varchar(50) NOT NULL DEFAULT 'DO_NOTHING' COMMENT '調(diào)度過期策略',
`executor_route_strategy` varchar(50) DEFAULT NULL COMMENT '執(zhí)行器路由策略',
`executor_handler` varchar(255) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)handler',
`executor_param` varchar(512) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)參數(shù)',
`executor_block_strategy` varchar(50) DEFAULT NULL COMMENT '阻塞處理策略',
`executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT '任務(wù)執(zhí)行超時時間,單位秒',
`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失敗重試次數(shù)',
`glue_type` varchar(50) NOT NULL COMMENT 'GLUE類型',
`glue_source` mediumtext COMMENT 'GLUE源代碼',
`glue_remark` varchar(128) DEFAULT NULL COMMENT 'GLUE備注',
`glue_updatetime` datetime DEFAULT NULL COMMENT 'GLUE更新時間',
`child_jobid` varchar(255) DEFAULT NULL COMMENT '子任務(wù)ID,多個逗號分隔',
`trigger_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '調(diào)度狀態(tài):0-停止,1-運行',
`trigger_last_time` bigint(13) NOT NULL DEFAULT '0' COMMENT '上次調(diào)度時間',
`trigger_next_time` bigint(13) NOT NULL DEFAULT '0' COMMENT '下次調(diào)度時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`job_group` int(11) NOT NULL COMMENT '執(zhí)行器主鍵ID',
`job_id` int(11) NOT NULL COMMENT '任務(wù),主鍵ID',
`executor_address` varchar(255) DEFAULT NULL COMMENT '執(zhí)行器地址,本次執(zhí)行的地址',
`executor_handler` varchar(255) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)handler',
`executor_param` varchar(512) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)參數(shù)',
`executor_sharding_param` varchar(20) DEFAULT NULL COMMENT '執(zhí)行器任務(wù)分片參數(shù),格式如 1/2',
`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失敗重試次數(shù)',
`trigger_time` datetime DEFAULT NULL COMMENT '調(diào)度-時間',
`trigger_code` int(11) NOT NULL COMMENT '調(diào)度-結(jié)果',
`trigger_msg` text COMMENT '調(diào)度-日志',
`handle_time` datetime DEFAULT NULL COMMENT '執(zhí)行-時間',
`handle_code` int(11) NOT NULL COMMENT '執(zhí)行-狀態(tài)',
`handle_msg` text COMMENT '執(zhí)行-日志',
`alarm_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警狀態(tài):0-默認(rèn)、1-無需告警、2-告警成功、3-告警失敗',
PRIMARY KEY (`id`),
KEY `I_trigger_time` (`trigger_time`),
KEY `I_handle_code` (`handle_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_log_report` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trigger_day` datetime DEFAULT NULL COMMENT '調(diào)度-時間',
`running_count` int(11) NOT NULL DEFAULT '0' COMMENT '運行中-日志數(shù)量',
`suc_count` int(11) NOT NULL DEFAULT '0' COMMENT '執(zhí)行成功-日志數(shù)量',
`fail_count` int(11) NOT NULL DEFAULT '0' COMMENT '執(zhí)行失敗-日志數(shù)量',
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `i_trigger_day` (`trigger_day`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_logglue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_id` int(11) NOT NULL COMMENT '任務(wù),主鍵ID',
`glue_type` varchar(50) DEFAULT NULL COMMENT 'GLUE類型',
`glue_source` mediumtext COMMENT 'GLUE源代碼',
`glue_remark` varchar(128) NOT NULL COMMENT 'GLUE備注',
`add_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_registry` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`registry_group` varchar(50) NOT NULL,
`registry_key` varchar(255) NOT NULL,
`registry_value` varchar(255) NOT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `i_g_k_v` (`registry_group`,`registry_key`,`registry_value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app_name` varchar(64) NOT NULL COMMENT '執(zhí)行器AppName',
`title` varchar(12) NOT NULL COMMENT '執(zhí)行器名稱',
`address_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '執(zhí)行器地址類型:0=自動注冊、1=手動錄入',
`address_list` text COMMENT '執(zhí)行器地址列表,多地址逗號分隔',
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL COMMENT '賬號',
`password` varchar(50) NOT NULL COMMENT '密碼',
`role` tinyint(4) NOT NULL COMMENT '角色:0-普通用戶、1-管理員',
`permission` varchar(255) DEFAULT NULL COMMENT '權(quán)限:執(zhí)行器ID列表,多個逗號分割',
PRIMARY KEY (`id`),
UNIQUE KEY `i_username` (`username`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `xxl_job_lock` (
`lock_name` varchar(50) NOT NULL COMMENT '鎖名稱',
PRIMARY KEY (`lock_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `xxl_job_group`(`id`, `app_name`, `title`, `address_type`, `address_list`, `update_time`) VALUES (1, 'xxl-job-executor-sample', '示例執(zhí)行器', 0, NULL, '2018-11-03 22:21:31' );
INSERT INTO `xxl_job_info`(`id`, `job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `schedule_type`, `schedule_conf`, `misfire_strategy`, `executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`) VALUES (1, 1, '測試任務(wù)1', '2018-11-03 22:21:31', '2018-11-03 22:21:31', 'XXL', '', 'CRON', '0 0 0 * * ? *', 'DO_NOTHING', 'FIRST', 'demoJobHandler', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN', '', 'GLUE代碼初始化', '2018-11-03 22:21:31', '');
INSERT INTO `xxl_job_user`(`id`, `username`, `password`, `role`, `permission`) VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 1, NULL);
INSERT INTO `xxl_job_lock` ( `lock_name`) VALUES ( 'schedule_lock');
commit;
Docker 部署 xxl-job
下載鏡像
Docker 鏡像地址: https://hub.docker.com/r/xuxueli/xxl-job-admin/
# 建議指定版本號
docker pull xuxueli/xxl-job-admin:2.4.0
創(chuàng)建容器并運行
創(chuàng)建日志掛載目錄
mkdir -p /logs/xxl-job
創(chuàng)建容器并運行
docker run -d --name xxl-job-admin \
-e PARAMS="--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=123456" \
-e JAVA_OPTS="-Xms512M -Xmx512m" \
-p 8080:8080 \
-v /logs/xxl-job:/data/applogs \
xuxueli/xxl-job-admin:2.4.0
訪問調(diào)度中心
調(diào)度中心管控臺地址:http://localhost:8080/xxl-job-admin
默認(rèn)登錄賬號密碼:admin/123456
登錄后運行界面如下圖所示:
SpringBoot 整合 xxl-job
pom.xml
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
application.yml
# xxl-job 定時任務(wù)配置
xxl:
job:
admin:
# 多個地址使用,分割
addresses: http://127.0.0.1:8080/xxl-job-admin
accessToken: default_token
executor:
appname: xxl-job-executor-${spring.application.name}
address:
ip:
port: 9999
logpath: /data/applogs/xxl-job/jobhandler
logretentiondays: 30
XxlJobConfig.java
XXL-JOB 自動裝配配置
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@Configuration
@Slf4j
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
執(zhí)行器注冊查看
需要 SpringBoot 應(yīng)用和調(diào)度中心在同一網(wǎng)絡(luò),網(wǎng)絡(luò)不互通則調(diào)度器無法調(diào)度執(zhí)行器
啟動 SpringBoot 工程 之后,前往調(diào)度中心查看執(zhí)行器注冊結(jié)果
定時任務(wù)測試
添加測試任務(wù)
在項目中添加名為 demoJobHandler
的任務(wù)處理器(Bean模式)
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* xxl-job 測試示例(Bean模式)
*/
@Component
@Slf4j
public class XxlJobSampleHandler {
@XxlJob("demoJobHandler")
public void demoJobHandler() {
log.info("XXL-JOB, Hello World.");
}
}
配置定時任務(wù)
調(diào)度中心新增測試任務(wù),運行模式為 BEAN
, 任務(wù)處理器為 demoJobHandler
配置 Corn 表達(dá)式,讓其每秒執(zhí)行一次任務(wù)
啟動定時任務(wù)
測試結(jié)果
成功,頻率為1s執(zhí)行一次。
結(jié)語
通過本文的實戰(zhàn)演示,我們成功地將 xxl-job 部署到 Docker 環(huán)境中,并與 Spring Boot 進行了整合。我們了解了 xxl-job 的基本概念和特點,并學(xué)習(xí)了如何配置調(diào)度器任務(wù)、開啟任務(wù),并調(diào)用執(zhí)行器執(zhí)行任務(wù)。xxl-job 提供了強大的任務(wù)調(diào)度和執(zhí)行能力,可以幫助我們實現(xiàn)任務(wù)的自動化調(diào)度和執(zhí)行,提高工作效率。希望本文對你理解和使用 xxl-job 有所幫助,歡迎你在實際項目中嘗試并應(yīng)用這些知識。
附錄
xxl-job 官方文檔
https://www.xuxueli.com/xxl-job/
xxl-job 源碼
https://gitee.com/xuxueli0323/xxl-job
測試項目源碼
本文涉及的依賴安裝、配置和測試用例代碼
Gitee: youlai-boot文章來源:http://www.zghlxwxcb.cn/news/detail-738532.html
Github: youlai-boot文章來源地址http://www.zghlxwxcb.cn/news/detail-738532.html
到了這里,關(guān)于Spring Boot 3 整合 xxl-job 實現(xiàn)分布式定時任務(wù)調(diào)度,結(jié)合 Docker 容器化部署(圖文指南)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!