本文是參考官方文檔自己實踐一次,純享版,大致也是作者邊寫博客邊去跟著官方文檔實現
?一、前期準備
1、官網地址
- GitHub地址:
GitHub - xuxueli/xxl-job: A distributed task scheduling framework.(分布式任務調度平臺XXL-JOB)A distributed task scheduling framework.(分布式任務調度平臺XXL-JOB) - xuxueli/xxl-jobhttps://github.com/xuxueli/xxl-job
- 文檔地址:
分布式任務調度平臺XXL-JOBXXL-JOB是一個輕量級分布式任務調度平臺,其核心設計目標是開發迅速、學習簡單、輕量級、易擴展。現已開放源代碼并接入多家公司線上產品線,開箱即用。https://www.xuxueli.com/xxl-job/#%E4%B8%80%E3%80%81%E7%AE%80%E4%BB%8B來到官網GitHub地址克隆xxl-job項目:
git clone https://github.com/xuxueli/xxl-job.git
2、最新版本需要的環境
- Maven3+
- Jdk1.8+
- Mysql8.0+
3、目前最新依賴
https://mvnrepository.com/artifact/com.xuxueli/xxl-job-corehttps://mvnrepository.com/artifact/com.xuxueli/xxl-job-core
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
<dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.4.2</version>
</dependency>
二、快速入門
1、初始化“調度數據庫”
可以從官方文檔的指導中找到對應數據庫信息,官網其他信息也需要注意一下:
下面我給出這里的sql,僅作參考:
#
# 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=InnoDB DEFAULT 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`)
) ENGINE=InnoDB DEFAULT 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=InnoDB DEFAULT 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=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`),UNIQUE KEY `i_g_k_v` (`registry_group`,`registry_key`,`registry_value`) USING BTREE
) ENGINE=InnoDB DEFAULT 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=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 '權限:執行器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', '示例執行器', 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;
執行sql:
2、編譯源碼
各個模塊詳情如下:
3、配置部署“調度中心”
調度中心項目:xxl-job-admin
作用:統一管理任務調度平臺上調度任務,負責觸發調度執行,并且提供任務管理平臺。
調度中心配置:
上面這些信息修改和自己的一致,其中配置信息里面下面兩個是需要修改的,端口自定義修改:
### web
server.port=8010
server.servlet.context-path=/xxl-job-admin### actuator
management.server.base-path=/actuator
management.health.mail.enabled=false### resources
spring.mvc.servlet.load-on-startup=0
spring.mvc.static-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/static/
啟動項目測試:
修改完成后可以啟動項目:
訪問下面網址查看是否成功:
http://localhost:8010/xxl-job-admin
默認賬號密碼為:
賬號:admin
密碼:123456
成功登錄管理端網頁!
服務器部署:
如果需要部署到服務器需要將此項目打包,然后部署到服務器上面,這里我是使用寶塔進行項目部署,參考下面操作:
部署到服務器需要開啟對應的端口,端口和上面你的配置文件的配置有關,還需要初始化數據庫,這里我就不演示了。
打包項目還是這幾個步驟:
隨便放在服務器一個目錄:
寶塔部署比較方便,如果想知道比較詳細的部署可以看完之前的博客,下面報了個錯:
Failed to create parent directories for [/data/applogs/xxl-job/xxl-job-admin.log]
看了一些博客好像說是根目錄是不能創建目錄的,所以下面修改一下項目的配置
./data/applogs/xxl-job/xxl-job-admin.log
加一個./ ,重新部署
啟動成功,訪問客戶端:
http://ip:8011/xxl-job-admin/
至此“調度中心”項目已經部署成功。
測試執行器:
下面只是測試,如果希望直接實戰可以跳到業務代碼改造那邊
注意配置文件,這個也是很重要的:
1.本地測試
執行器就是設置定時任務的地方,這里我們使用官方文檔提供的一個springboot執行器案例去測試:
運行該項目,在admin控制臺可以看到:
進入任務管理,編輯任務,下面有個默認的任務,對應的是官方代碼里面的一個定時任務,注意保證項目中@XxlJob注解里面的值和admin里面保持一致才能正常使用項目的定時任務,如果需要添加定時任務,只需要在admin控制臺中繼續添加任務,注意和代碼中@XxlJob的值保持一致。
開始執行一次測試
由于我們的執行器目前只注冊了一個,默認從執行器管理中獲取,所以不填也沒事,但是如果你有多個執行器,就去配置一下,因為xxl-job是一個分布式的定時任務管理,可以有很多執行器。
以上是本地測試,admin也是本地的,如果是服務器測試的話,也需要把測試項目部署上去,然后
注意修改下面配置信息,和之前admin一個原理:
2.服務器測試
保證機器地址正確,然后進行測試:
注意不要使用服務器的admin測試本地的執行器,否則就會報錯。
三、業務代碼改造
其實這里的原理和測試執行器原理是一樣的,無非就是把執行器關鍵配置信息放在業務代碼中
1、項目中加入依賴
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
<dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.4.2</version>
</dependency>
2、拷貝修改配置文件
這里我將這些配置配置在yml中
xxl:job:# xxl_job_admin配置admin:addresses: http://???ip:8011/xxl-job-admin# 認證tokenaccessToken: default_token# 執行器executor:### xxl-job executor appnameappname: xxl-job-executor-quick### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is nulladdress:### xxl-job executor server-infoip:port: 9999### xxl-job executor log-pathlogpath: ./data/applogs/xxl-job/jobhandler### xxl-job executor log-retention-dayslogretentiondays: 30
3、將config拷貝到項目中
4、新建定時任務測試類來測試
package com.quick.task;import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;/*** @program: quick-pick* @description: 測試定時任務類* @author: bluefoxyu* @date: 2024-12-25 22:12**/
@Component
public class testTask {/*** <p>* description: xxl-job第一個測試案例* </p>** @return: void* @author: bluefoxyu* @date: 2024-12-25 22:13:20*/@XxlJob(value = "testFirstTask")public void testFirstTask(){XxlJobHelper.log("定時任務執行了" + LocalDateTime.now());}}
5、去xxl-job-admin上執行器管理上新增執行器
http://服務器ip:8011/xxl-job-admin/
6、新增定時任務進行一次測試
執行一次任務:
至此,定時任務測試成功!