1. 前言簡介
XXL-JOB是一個分布式任務(wù)調(diào)度平臺,其核心設(shè)計目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡單、輕量級、易擴(kuò)展。現(xiàn)已開放源代碼并接入多家公司線上產(chǎn)品線,開箱即用。
1.1 xxl-job的作用及優(yōu)勢
1.1.1 xxl-job作用
統(tǒng)一管理定時調(diào)度任務(wù) 相比于 springboot的@Scheduled , 這個表達(dá)式可以隨意修改 也可以面對更加復(fù)雜的定時調(diào)度場景 如
集群
,容錯
,分片
等等
1.1.2 xxl-job優(yōu)勢
1、簡單:支持通過Web頁面對任務(wù)進(jìn)行CRUD操作,操作簡單,一分鐘上手;
2、動態(tài):支持動態(tài)修改任務(wù)狀態(tài)、啟動/停止任務(wù),以及終止運行中任務(wù),即時生效;
3、調(diào)度中心HA(中心式):調(diào)度采用中心式設(shè)計,“調(diào)度中心”自研調(diào)度組件并支持集群部署,可保證調(diào)度中心HA;
4、執(zhí)行器HA(分布式):任務(wù)分布式執(zhí)行,任務(wù)”執(zhí)行器”支持集群部署,可保證任務(wù)執(zhí)行HA;
5、注冊中心: 執(zhí)行器會周期性自動注冊任務(wù), 調(diào)度中心將會自動發(fā)現(xiàn)注冊的任務(wù)并觸發(fā)執(zhí)行。同時,也支持手動錄入執(zhí)行器地址;
6、彈性擴(kuò)容縮容:一旦有新執(zhí)行器機(jī)器上線或者下線,下次調(diào)度時將會重新分配任務(wù);
7、觸發(fā)策略:提供豐富的任務(wù)觸發(fā)策略,包括:Cron觸發(fā)、固定間隔觸發(fā)、固定延時觸發(fā)、API(事件)觸發(fā)、人工觸發(fā)、父子任務(wù)觸發(fā);
8、調(diào)度過期策略:調(diào)度中心錯過調(diào)度時間的補(bǔ)償處理策略,包括:忽略、立即補(bǔ)償觸發(fā)一次等;
9、阻塞處理策略:調(diào)度過于密集執(zhí)行器來不及處理時的處理策略,策略包括:單機(jī)串行(默認(rèn))、丟棄后續(xù)調(diào)度、覆蓋之前調(diào)度;
10、任務(wù)超時控制:支持自定義任務(wù)超時時間,任務(wù)運行超時將會主動中斷任務(wù);
等等等
1.2 資源位置及使用說明
1.2.1 文檔說明
=> 傳送門: xxl的使用文檔
1.2.2 docker鏡像位置
=> 傳送門: docker鏡像xxl-job 2.4.0
2. 安裝配置使用xxl-job(兩種)
2.0 公共操作: sql腳本(2.2免下載也用)
源碼中:
/xxl-job/doc/db/tables_xxl_job.sql
CREATE database if NOT EXISTS `xxl_job` default character set utf8mb4 collate utf8mb4_general_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;
2.1 方法一: 源碼搭建方式
2.1.1 源碼下載位置
=> 傳送門: github倉庫地址:
=> 傳送門: gitee倉庫地址
2.1.2 idea找到admin并發(fā)包
xxl-job-admin: 任務(wù)調(diào)度中心 就這個最關(guān)鍵
xxl-job-executor-samples 忽略 測試功能 本來就挺簡單 看這個沒啥必要
2.1.3 找到application.properties
主要修改mysql的參數(shù)
### 服務(wù)部署的端口,
server.port=8080
server.servlet.context-path=/xxl-job-admin
###調(diào)度中心JDBC鏈接:鏈接地址和之前所創(chuàng)建的調(diào)度數(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.cj.jdbc.Driver
## 調(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
2.1.4 maven打包生成jar文件
mvn clean compile package install
2.2 方法二: docker制作鏡像(*)
2.2.1 docker拉取配置
docker pull xuxueli/xxl-job-admin:2.4.0
2.2.2 docker鏡像創(chuàng)建并運行
-> (1) 運行命令
docker run -di -e PARAMS="--spring.datasource.url=jdbc:mysql://192.168.1.29:3306/xxl_job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=root --spring.datasource.password=pzy123 --xxl.job.accessToken=pingzhuyan.test" \
-p 9001:8080 \
-v /usr/local/src/docker/xxl-job:/data/applogs \
--name xxl-job \
--privileged=true \
xuxueli/xxl-job-admin:2.4.0
-> (2) 參數(shù)解釋
--privileged=true
container內(nèi)的root擁有真正的root
權(quán)限
-v 目錄掛載 這里實際是兩層xxl-job 也就是可以不寫xxl-job文件
-p 端口 左邊宿主機(jī) 右邊容器
–xxl.job.accessToken=pingzhuyan.testaccessToken
后面有用(上面就在配置文件內(nèi)改)
2.3 查看啟動結(jié)果
2.3.1 直接訪問地址
http://192.168.1.29:9001/xxl-job-admin
2.3.2 如遇訪問異常
(打開防火墻端口) systemctl操作
2.3.3 檢查服務(wù)啟動是否正常
數(shù)據(jù)庫報錯 改數(shù)據(jù)庫信息
文件沒權(quán)限 增加root權(quán)限
2.3.4 docker操作的相關(guān)指令
docker相關(guān)操作專欄 ===> 傳送門
上面專欄內(nèi) 記載了docker相關(guān)的操作
, 感興趣的可以看一看
# 查看日志
docker container logs xxl-job
# 查看所有容器
docker ps -a
# 刪除容器
docker container rm -f xxl-job
# 刪除images鏡像
docker images rmi -f 鏡像id
# 進(jìn)入容器bash操作
docker exec -it xxl-job bash
# 重啟服務(wù)
docker restart xxl-job
3. springboot整合xxl-job(新版2.4.0)
版本
2.3.0-2.4.0
寫法變化不大
3.1 配置文件
3.1.1 yml配置代碼
xxl:
job:
admin:
# 調(diào)度中心服務(wù)部署的地址
addresses: http://192.168.1.29:9001/xxl-job-admin
# 執(zhí)行器通訊TOKEN,要和調(diào)度中心服務(wù)部署配置的accessToken一致,要不然無法連接注冊
accessToken: pingzhuyan.test
executor:
# 執(zhí)行器AppName
appname: pzy-beta1
# 執(zhí)行器注冊 [選填]
address:
ip:
#執(zhí)行器端口號: 小于等于0則自動獲取 默認(rèn)端口為9999,單機(jī)部署多個執(zhí)行器時,注意要配置不同執(zhí)行器端口;
port: 0
# 執(zhí)行器運行日志文件存儲磁盤路徑 [選填] 需要對該路徑擁有讀寫權(quán)限;為空則使用默認(rèn)路徑;
logpath: D:/usr/local/src/xxl-job
# 執(zhí)行器日志文件保存天數(shù) [選填] 過期日志自動清理, 限制值大于等于3時生效 否則, 如-1, 關(guān)閉自動清理功能
logretentiondays: 15
3.1.2 properties配置文件
### 調(diào)度中心的地址 ,就是 xxl-job-admin 這個服務(wù)的地址
xxl.job.admin.addresses=http://192.168.1.29:9001/xxl-job-admin
### 要和xxl-job-admin 中的accessToken統(tǒng)一 (可以沒有)
xxl.job.accessToken=pingzhuyan.test
### 執(zhí)行器名稱,可自定義
xxl.job.executor.appname=pzy-beta1
### 會將該地址注冊到調(diào)度中心,調(diào)度中心會用該地址調(diào)度任務(wù), 可為空默認(rèn)就是 ip:port , 端口不可以和業(yè)務(wù)端口重復(fù)
xxl.job.executor.address=
### 可為空,默認(rèn)獲取本機(jī)ip
xxl.job.executor.ip=
xxl.job.executor.port=0
### 運行日志所保存的路徑
xxl.job.executor.logpath=D:/usr/local/src/xxl-job
### 日志存放時間
xxl.job.executor.logretentiondays=15
3.2 配置中部分參數(shù)解釋
3.2.1 調(diào)度中心地址
調(diào)度中心
服務(wù)部署
的地址
ps: 不要公網(wǎng)部署 內(nèi)網(wǎng)測試 還得用內(nèi)網(wǎng)穿透麻煩
3.2.2 accessToken由來
docker創(chuàng)建的時候有個參數(shù)
--xxl.job.accessToken=pingzhuyan.test
作用:執(zhí)行器通訊TOKEN,要和調(diào)度中心服務(wù)部署配置的accessToken一致,要不然無法連接注冊
如下圖
所示:
3.2.3 執(zhí)行器名稱由來
如圖所示 新增執(zhí)行器 復(fù)制appName
3.2.4 注冊地址 ip 及 端口解釋
- 優(yōu)先使用該配置作為注冊地址,為空時使用內(nèi)嵌服務(wù) ”IP:PORT“ 作為注冊地址。從而更靈活的支持容器類型執(zhí)行器動態(tài)IP和動態(tài)映射端口問題。
端口
必填 0自動分配默認(rèn)9999
集群啟動時候 這個端口寫固定會啟動失敗
3.2.5 logpath運行日志保存位置[選填]
windows
下這個路徑如圖所示, 每次執(zhí)行都會留下日志文件
3.2.6 logretentiondays日志存放時間[選填]
過期日志自動清理, 限制值大于等于3時生效; 否則, 如-1, 關(guān)閉自動清理功能
3.3 springboot代碼整合
3.3.1 配置config的寫法
XxlJobSpringExecutor
這塊大同小異, 寫法都差不多 bean模式的
最下面注釋的XxlJobExecutor.registJobHandler("pzyBetaHandler", new TaskDispatch());
是整個類進(jìn)行定時任務(wù) 模式是GLUE(java)不能Autowired
只能new
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;
/**
* @author pzy
* @version 0.1.0
*/
@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("===> pzy xxl-job Bean執(zhí)行開始");
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);
log.info("===> pzy xxl-job Bean執(zhí)行成功");
// XxlJobExecutor.registJobHandler("pzyBetaHandler", new TaskDispatch());
return xxlJobSpringExecutor;
}
}
3.3.2 測試代碼(bean模式)
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
/**
* @author pzy
* @version 0.1.0
*/
@Component
public class TestTask {
@XxlJob("pzyBetaHandler")
public void pzyBetaHandler() throws Exception {
System.out.println("hello---->xxl-job");
// default success
}
}
3.3.3 官方測試代碼(新版3.4.0)
舊版本的百度搜一下一大堆, 具體看
本文目錄:4.2.1
詳解文章來源:http://www.zghlxwxcb.cn/news/detail-714675.html
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
* 一個開發(fā)示例 已經(jīng)更新為2.4.0新版了
*
* XxlJob開發(fā)示例(Bean模式)
* <p>
* 開發(fā)步驟:
* 1、在Spring Bean實例中,開發(fā)Job方法,方式格式要求為 "public ReturnT<String> execute(String param)"
* 2、為Job方法添加注解 "@XxlJob(value="自定義jobhandler名稱", init = "JobHandler初始化方法", destroy = "JobHandler銷毀方法")",注解value值對應(yīng)的是調(diào)度中心新建任務(wù)的JobHandler屬性的值。
* 3、執(zhí)行日志:需要通過 "XxlJobHelper.log" 打印執(zhí)行日志;
*
* @author xuxueli 2019-12-11 21:52:51
*/
@Component
public class SampleXxlJob {
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
/**
* 1、簡單任務(wù)示例(Bean模式)
*/
@XxlJob("demoJobHandler")
public ReturnT<String> demoJobHandler(String param) throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) {
XxlJobHelper.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
return ReturnT.SUCCESS;
}
/**
* 2、分片廣播任務(wù)
*/
@XxlJob("shardingJobHandler")
public ReturnT<String> shardingJobHandler(String param) throws Exception {
// 分片參數(shù)
// ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); 2.3.0 更新了
XxlJobHelper.log("分片參數(shù):當(dāng)前分片序號 = {}, 總分片數(shù) = {}", XxlJobHelper.getShardIndex(), XxlJobHelper.getShardTotal());
// 業(yè)務(wù)邏輯
for (int i = 0; i < XxlJobHelper.getShardTotal(); i++) {
if (i == XxlJobHelper.getShardIndex()) {
XxlJobHelper.log("第 {} 片, 命中分片開始處理", i);
} else {
XxlJobHelper.log("第 {} 片, 忽略", i);
}
}
return ReturnT.SUCCESS;
}
/**
* 3、命令行任務(wù)
*/
@XxlJob("commandJobHandler")
public ReturnT<String> commandJobHandler(String param) throws Exception {
String command = param;
int exitValue = -1;
BufferedReader bufferedReader = null;
try {
// command process
Process process = Runtime.getRuntime().exec(command);
BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
// command log
String line;
while ((line = bufferedReader.readLine()) != null) {
XxlJobHelper.log(line);
}
// command exit
process.waitFor();
exitValue = process.exitValue();
} catch (Exception e) {
XxlJobHelper.log(e);
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
if (exitValue == 0) {
return ReturnT.SUCCESS;
} else {
return new ReturnT<String>(ReturnT.FAIL.getCode(), "command exit value(" + exitValue + ") is failed");
}
}
/**
* 4、跨平臺Http任務(wù)
* 參數(shù)示例:
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
@XxlJob("httpJobHandler")
public ReturnT<String> httpJobHandler(String param) throws Exception {
// param parse
if (param == null || param.trim().length() == 0) {
XxlJobHelper.log("param[" + param + "] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam : httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url == null || url.trim().length() == 0) {
XxlJobHelper.log("url[" + url + "] invalid.");
return ReturnT.FAIL;
}
if (method == null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobHelper.log("method[" + method + "] invalid.");
return ReturnT.FAIL;
}
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setReadTimeout(5 * 1000);
connection.setConnectTimeout(3 * 1000);
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
// do connection
connection.connect();
// data
if (data != null && data.trim().length() > 0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
}
// result
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
String responseMsg = result.toString();
XxlJobHelper.log(responseMsg);
return ReturnT.SUCCESS;
} catch (Exception e) {
XxlJobHelper.log(e);
return ReturnT.FAIL;
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (Exception e2) {
XxlJobHelper.log(e2);
}
}
}
/**
* 5、生命周期任務(wù)示例:任務(wù)初始化與銷毀時,支持自定義相關(guān)邏輯;
*/
@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
public ReturnT<String> demoJobHandler2(String param) throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
return ReturnT.SUCCESS;
}
public void init() {
logger.info("init");
}
public void destroy() {
logger.info("destory");
}
}
4. 總結(jié)及注意事項
4.1 本文總結(jié)
- 找到文檔和代碼位置
- 創(chuàng)建數(shù)據(jù)庫
- 拉取鏡像
- 運行容器
- 打開 http://192.168.1.100:9001/xxl-job-admin
- 輸入admin 123456
- 創(chuàng)建執(zhí)行器
- 創(chuàng)建任務(wù)管理(選擇bean模式) 記住bean的名字
- 在springboot工程中加入依賴(
最新版的2.4.0
)- 添加配置yml/properties
- 寫配置文件(上面的寫不寫都行 就是取個參數(shù))
- 書寫測試代碼@XxlJob(“
記住的bean的名字
”)- 啟動程序并在xxl-job-admin測試運行
- 一切正常 測試集群效果
到此結(jié)束
4.2 注意事項
4.2.1 新版本2.3.0版本+的寫法有變化
- 2.3.0版本修改使用方式了 用老版本的看一下
2.3.1 測試代碼@XxlJob即使是灰色 不用理會![]()
到此xxl-job全部結(jié)束 使用起來比較簡單 管理起來也比較方便 集群部署也很容易
作者: pingzhuyan
文章來源地址http://www.zghlxwxcb.cn/news/detail-714675.html
到了這里,關(guān)于微服務(wù): xxl-job的安裝(docker),使用及springboot整合[完整版詳解]的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!