目錄
1. 介紹
2. SpringBoot整合xxl-job
2.1. 配置數(shù)據(jù)庫
2.2. 運行調(diào)動中心
2.3. 整合業(yè)務(wù)服務(wù)
2.3.1. 引入maven依賴
2.3.2.?創(chuàng)建業(yè)務(wù)服務(wù)配置類
2.3.3.?創(chuàng)建定時任務(wù)
1. 介紹
xxl-job是一個分布式定時器任務(wù)派遣服務(wù),這個項目主要有以下三部分組成 :?
- xxl-job-admin: 調(diào)動中心,主要是提供任務(wù)管理平臺的頁面,需要把該模塊單獨打包作為一個服務(wù)部署,定時器再執(zhí)行時,是通過該服務(wù)去調(diào)用我們的業(yè)務(wù)服務(wù)完成任務(wù)執(zhí)行。
- xxl-job-core:? 公共依賴模塊,在整合業(yè)務(wù)服務(wù)的時候,需要引入該依賴。
- xxl-job-executor-samples:官方提供的demo。
2. SpringBoot整合xxl-job
2.1. 配置數(shù)據(jù)庫
點擊<xxl_job_gitee> 訪問gitee獲取該項目,在 /doc/db/tables_xxl_job.sql的路徑中有SQL文件,需要將他導(dǎo)入到你自己的MySQL中。
表介紹:?
2.2. 運行調(diào)動中心
修改??xxl-job-admin 模塊 application.properties?的配置信息。
修改MySQL為你數(shù)據(jù)庫的真實信息?
?修改郵箱服務(wù)配置(可選非必須)
以上配置都修改完成后,運行?xxl-job-admin?這個服務(wù),上面說過它是一個調(diào)度中心,主要是提供任務(wù)管理平臺的頁面。所以當(dāng)你的業(yè)務(wù)服務(wù)部署了,這個模塊也要作為一個服務(wù)單獨部署,這樣才可以管理你業(yè)務(wù)中定時任務(wù)。
2.3. 整合業(yè)務(wù)服務(wù)
接下來整合我們自己的業(yè)務(wù)服務(wù),xxl-job-executor-sample-springboot 這個模塊是官方提供的整合demo 模塊,可以參考它整合自己的業(yè)務(wù)。
2.3.1. 引入maven依賴
<!-- xxl-job依賴,maven倉庫有該依賴,不需要手動安裝 -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
2.3.2.?創(chuàng)建業(yè)務(wù)服務(wù)配置類
注意:一定要確保你的 業(yè)務(wù)服務(wù) 和 xxl-job-admin 這個服務(wù)連接的是同一個數(shù)據(jù)庫
application.properties配置
### 調(diào)度中心的地址 ,就是 xxl-job-admin 這個服務(wù)的地址
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### token (ke為空), 要和xxl-job-admin 中的accessToken統(tǒng)一
xxl.job.accessToken=default_token
### 執(zhí)行器名稱,可自定義
xxl.job.executor.appname=xxl-job-executor-sample
### 會將該地址注冊到調(diào)度中心,調(diào)度中心會用該地址調(diào)度任務(wù), 可為空默認(rèn)就是 ip:port , 端口不可以和業(yè)務(wù)端口重復(fù)
xxl.job.executor.address=
### 可為空,默認(rèn)獲取本機ip
xxl.job.executor.ip=
xxl.job.executor.port=9999
### 運行日志所保存的路徑
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### 日志存放時間
xxl.job.executor.logretentiondays=30
創(chuàng)建:FrameLessXxlJobConfig 配置類
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xxl-job config
*
* @author yazb
*/
@Configuration
public class FrameLessXxlJobConfig {
private Logger logger = LoggerFactory.getLogger(FrameLessXxlJobConfig.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;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.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;
}
/**
* 針對多網(wǎng)卡、容器內(nèi)部署等情況,可借助 "spring-cloud-commons" 提供的 "InetUtils" 組件靈活定制注冊IP;
*
* 1、引入依賴:
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2、配置文件,或者容器啟動變量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、獲取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}
2.3.3.?創(chuàng)建定時任務(wù)
創(chuàng)建一個測試任務(wù)
2.3.3.1:bean模式任務(wù)
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
/**
* @author yazb
* 測試
*/
@Component
public class TestTask {
@XxlJob("hello")
public void demoJobHandler() throws Exception {
System.out.println("hello---->xxl-job");
// default success
}
}
訪問 xxl-job-admin 這個服務(wù),在頁面中運行定時任務(wù)。
用戶:admin
密碼:123456
?創(chuàng)建定時任務(wù)
運行定時任務(wù)
?
2.3.3.2:分片廣播任務(wù)模式
分片廣播模式,用來運行創(chuàng)建集群環(huán)境中的定時任務(wù),所以需要我們再創(chuàng)建一個服務(wù)測試。
分片廣播模式,會同時運行你集群中所有節(jié)點服務(wù)中定時任務(wù)。要想保證避免任務(wù)重復(fù)執(zhí)行??梢圆捎?單機路由策略(如:第一臺、一致性哈希)” + “阻塞策略(如:單機串行、丟棄后續(xù)調(diào)度)” 來規(guī)避,最終避免任務(wù)重復(fù)執(zhí)行。
已經(jīng)注冊到調(diào)度中心的服務(wù)?
創(chuàng)建分布式廣播任務(wù)
文章來源:http://www.zghlxwxcb.cn/news/detail-638407.html
/**
* 2、分片廣播任務(wù)
*/
@XxlJob("shardingJobHandler")
public void shardingJobHandler() throws Exception {
// 分片參數(shù)
int shardIndex = XxlJobHelper.getShardIndex();
int shardTotal = XxlJobHelper.getShardTotal();
XxlJobHelper.log("分片參數(shù):當(dāng)前分片序號 = {}, 總分片數(shù) = {}", shardIndex, shardTotal);
System.out.println("測試服務(wù) 9998-----》");
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-638407.html
到了這里,關(guān)于Spring Boot整合 xxl-job的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!