XXL-JOB
?XXL-JOB是一個分布式任務調度平臺(XXL是作者徐雪里姓名拼音的首字母),其核心設計目標是開發迅速、學習簡單、輕量級、易擴展。
源碼倉庫地址:https://github.com/xuxueli/xxl-job
源碼結構:
系統架構
在xxl-job中,有2個角色:
xxl-job-admin調度中心
統一管理任務調度平臺上的調度任務,負責觸發調度執行,并且提供任務管理平臺。xxl-job-executor執行器
執行器通常是我們的業務系統,如示例中的springboot項目。xxl-job就是一個中心化管理系統,系統主要通過MySQL管理各種定時任務信息,當到了定時任務的觸發時間,就把任務信息從數據庫中拉進內存,對任務執行器發起調度請求。
1.首先拉取xxl-job鏡像
docker run --restart=always --privileged=true -e PARAMS="--spring.datasource.username=root --spring.datasource.password=123456--spring.datasource.url=jdbc:mysql://192.168.2.3:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai" -p 8040:8080 -v /usr/local/docker/xxl-job/logs:/data/applogs --name xxl-job-admin -d xuxueli/xxl-job-admin:2.4.0
注意所連接的數據庫xxl_job的權限要為%,并且允許所有ip連接
檢查C:\ProgramData\MySQL\MySQL Server 8.0的my.ini文件
也可以通過docker-compose.yml文件拉取
version: '3.3'
services:xxl-job-admin:image: xuxueli/xxl-job-admin:2.4.1ports:- "8040:8080"environment:PARAMS: '--spring.datasource.url=jdbc:mysql://192.168.2.3:3306/xxl_job?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true--spring.datasource.username=root--spring.datasource.password=123456--xxl.job.accessToken=xxl-job'volumes:- ./logs:/data/applogs
配置調度中心
-
初始化數據庫
# # XXL-JOB # 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 '執行器主鍵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 '調度類型',`schedule_conf` varchar(128) DEFAULT NULL COMMENT '調度配置,值含義取決于調度類型',`misfire_strategy` varchar(50) NOT NULL DEFAULT 'DO_NOTHING' COMMENT '調度過期策略',`executor_route_strategy` varchar(50) DEFAULT NULL COMMENT '執行器路由策略',`executor_handler` varchar(255) DEFAULT NULL COMMENT '執行器任務handler',`executor_param` varchar(512) DEFAULT NULL COMMENT '執行器任務參數',`executor_block_strategy` varchar(50) DEFAULT NULL COMMENT '阻塞處理策略',`executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT '任務執行超時時間,單位秒',`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失敗重試次數',`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 '子任務ID,多個逗號分隔',`trigger_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '調度狀態:0-停止,1-運行',`trigger_last_time` bigint(13) NOT NULL DEFAULT '0' COMMENT '上次調度時間',`trigger_next_time` bigint(13) NOT NULL DEFAULT '0' COMMENT '下次調度時間',PRIMARY KEY (`id`) ) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;CREATE TABLE `xxl_job_log` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`job_group` int(11) NOT NULL COMMENT '執行器主鍵ID',`job_id` int(11) NOT NULL COMMENT '任務,主鍵ID',`executor_address` varchar(255) DEFAULT NULL COMMENT '執行器地址,本次執行的地址',`executor_handler` varchar(255) DEFAULT NULL COMMENT '執行器任務handler',`executor_param` varchar(512) DEFAULT NULL COMMENT '執行器任務參數',`executor_sharding_param` varchar(20) DEFAULT NULL COMMENT '執行器任務分片參數,格式如 1/2',`executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT '失敗重試次數',`trigger_time` datetime DEFAULT NULL COMMENT '調度-時間',`trigger_code` int(11) NOT NULL COMMENT '調度-結果',`trigger_msg` text COMMENT '調度-日志',`handle_time` datetime DEFAULT NULL COMMENT '執行-時間',`handle_code` int(11) NOT NULL COMMENT '執行-狀態',`handle_msg` text COMMENT '執行-日志',`alarm_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警狀態:0-默認、1-無需告警、2-告警成功、3-告警失敗',PRIMARY KEY (`id`),KEY `I_trigger_time` (`trigger_time`),KEY `I_handle_code` (`handle_code`),KEY `I_jobid_jobgroup` (`job_id`,`job_group`),KEY `I_job_id` (`job_id`) ) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;CREATE TABLE `xxl_job_log_report` (`id` int(11) NOT NULL AUTO_INCREMENT,`trigger_day` datetime DEFAULT NULL COMMENT '調度-時間',`running_count` int(11) NOT NULL DEFAULT '0' COMMENT '運行中-日志數量',`suc_count` int(11) NOT NULL DEFAULT '0' COMMENT '執行成功-日志數量',`fail_count` int(11) NOT NULL DEFAULT '0' COMMENT '執行失敗-日志數量',`update_time` datetime DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `i_trigger_day` (`trigger_day`) USING BTREE ) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;CREATE TABLE `xxl_job_logglue` (`id` int(11) NOT NULL AUTO_INCREMENT,`job_id` int(11) NOT NULL COMMENT '任務,主鍵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 = InnoDBDEFAULT 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`),UNIQUE KEY `i_g_k_v` (`registry_group`, `registry_key`, `registry_value`) USING BTREE ) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;CREATE TABLE `xxl_job_group` (`id` int(11) NOT NULL AUTO_INCREMENT,`app_name` varchar(64) NOT NULL COMMENT '執行器AppName',`title` varchar(12) NOT NULL COMMENT '執行器名稱',`address_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '執行器地址類型:0=自動注冊、1=手動錄入',`address_list` text COMMENT '執行器地址列表,多地址逗號分隔',`update_time` datetime DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE = InnoDBDEFAULT 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 '權限:執行器ID列表,多個逗號分割',PRIMARY KEY (`id`),UNIQUE KEY `i_username` (`username`) USING BTREE ) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;CREATE TABLE `xxl_job_lock` (`lock_name` varchar(50) NOT NULL COMMENT '鎖名稱',PRIMARY KEY (`lock_name`) ) ENGINE = InnoDBDEFAULT CHARSET = utf8mb4;## —————————————————————— init data ——————————————————INSERT INTO `xxl_job_group`(`id`, `app_name`, `title`, `address_type`, `address_list`, `update_time`) VALUES (1, 'xxl-job-executor-sample', '示例執行器', 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, '測試任務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;
表名 | 作用 |
---|---|
xxl_job_group | 執行器信息表:維護任務執行器信息 |
xxl_job_info | 調度擴展信息表:用于保存xxl-job調度任務的擴展信息,如任務分組、任務名、機器地址、執行器、執行入參和報警郵件等等 |
xxl_job_lock | 任務調度鎖表 |
xxl_job_log | 調度日志表:用于保存xxl-job調度任務的歷史信息,如調度結果、執行結果、調度入參、調度機器和執行器等等 |
xxl_job_log_report | 調度日志報表:用戶存儲xxl-job任務調度日志的報表,調度中心報表功能頁面會用到 |
xxl_job_logglue | 任務GLUE日志:用于保存GLUE更新歷史,用于支持GLUE的版本回溯功能 |
xxl_job_registry | 執行器注冊表:維護在線的執行器和調度中心機器地址信息 |
xxl_job_user | 系統用戶表 |
調度中心支持集群部署,集群情況下各節點務必連接同一個mysql實例;如果mysql做主從,調度中心集群節點務必強制走主庫。
在docker destop中啟動xxl-job容器
訪問調度中心:http://192.168.2.3:8040/xxl-job-admin/joblog
初始登錄賬號密碼為:admin/123456
2.springboot整合xxljob
導入依賴
<!-- pom.xml --><dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.4.0</version></dependency>
編寫application.properties文件,引入調度中心配置
# 調度中心地址配置
xxl.job.admin.addresses=http://192.168.2.3:8040/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=xxl-job### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9991
### xxl-job executor log-path
xxl.job.executor.logpath=
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
編寫XxlJobConfig配置文件:
@Configuration
@Slf4j
public class XxlJobConfig {private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);@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;@Beanpublic 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;}
}