調(diào)度與監(jiān)控-spring batch(7)結(jié)合xxl-job進(jìn)行批處理

tags: springbatch


1.引言

經(jīng)過前面幾篇文章對Spring Batch的介紹,同時(shí)結(jié)合示例,從最簡單的helloworld字符串輸出,到讀取文件到數(shù)據(jù)庫的數(shù)據(jù)同步,然后是數(shù)據(jù)庫到數(shù)據(jù)庫,接著結(jié)合BeetlSql進(jìn)一步簡化數(shù)據(jù)庫讀寫,再通過動態(tài)參數(shù)綁定實(shí)現(xiàn)增量同步,由淺到深,已經(jīng)可以基本滿足數(shù)據(jù)抽取,數(shù)據(jù)同步的工作了,下面是之前的文章列表:

批處理任務(wù),一般做為后臺服務(wù)定時(shí)運(yùn)行,服務(wù)運(yùn)維工作則是批處理服務(wù)之外的一項(xiàng)重要工作,包括服務(wù)運(yùn)行如何監(jiān)控,任務(wù)運(yùn)行統(tǒng)計(jì),任務(wù)執(zhí)行日志查看等等。前面已說過,Spring Batch是批處理框架,并不是調(diào)度框架。調(diào)度框架,簡單點(diǎn)可以使用quartz,crontab,而市面上已經(jīng)有比較多功能完善的調(diào)度框架,經(jīng)過比較,個(gè)人感覺xxl-job是比較容易上手,而且功能比較齊全,因此,這里介紹一下,使用xxl-jobSpring Batch的批處理任務(wù)進(jìn)行調(diào)度,實(shí)現(xiàn)監(jiān)控功能。

2.xxl-job介紹

xxl-job是一個(gè)輕量級的分布式任務(wù)調(diào)度平臺,其核心設(shè)計(jì)目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡單、輕量級、易擴(kuò)展。系統(tǒng)架構(gòu)設(shè)計(jì)合理,把調(diào)度系統(tǒng)與執(zhí)行器分離,調(diào)度系統(tǒng)負(fù)責(zé)調(diào)度相關(guān)的邏輯,執(zhí)行器則是具體任務(wù)的實(shí)現(xiàn)邏輯,開發(fā)者可以直接使用調(diào)度系統(tǒng),然后實(shí)現(xiàn)自己任務(wù)邏輯來做為執(zhí)行器即可,做到開箱即用。具體使用方式及詳細(xì)說明,可見其官方文檔

按照xxl-job的文檔,先安裝xxl-job的數(shù)據(jù)庫,然后把xxl-job-admin啟動起來,啟動起來的頁面如下:

3.編寫Spring Batch執(zhí)行器

本示例是基于上一篇文章所用到的數(shù)據(jù)庫增量同步的示例,只需要在原來的基礎(chǔ)上添加一個(gè)執(zhí)行器即可,本示例為spring-batch-xxl-executor。按照xxl-job的官方文檔,里面有說明如何配置部署“執(zhí)行器項(xiàng)目”。具體到本實(shí)例,配置,實(shí)現(xiàn)如下:

3.1 添加maven依賴

添加xxl-job的核心依賴,如下:

<!-- xxl-job-core -->
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.0.2</version>
</dependency>

3.2 添加執(zhí)行器配置文件executor.properties

執(zhí)行器配置需要填寫包括執(zhí)行器名稱、調(diào)度系統(tǒng)地址等,如下:

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8089/xxl-job-admin

### xxl-job executor address
xxl.job.executor.appname=${spring.application.name}
xxl.job.executor.ip=
xxl.job.executor.port=9999

### xxl-job, access token
xxl.job.accessToken=

### xxl-job log path
xxl.job.executor.logpath=logs
### xxl-job log retention days
xxl.job.executor.logretentiondays=-1

3.3 執(zhí)行器組件配置

添加java配置文件,獲取以上的executor.properties配置。如下:

@Configuration
@ConfigurationProperties(prefix = "xxl.job")
@PropertySource("classpath:/config/executor.properties")
@Slf4j
public class JobExecutorConfig {
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;
    
    ......略

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppName(appName);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;
    }
}

3.4 編寫執(zhí)行器

執(zhí)行器,其實(shí)就是繼承了IJobHandler類的實(shí)現(xiàn),在execute方法實(shí)現(xiàn)任務(wù)實(shí)現(xiàn)邏輯即可。本示例中,只需要按之前測試的啟動任務(wù)的邏輯實(shí)現(xiàn)即可。如下:

@JobHandler(value="incrementUserJobHandler")
@Component
public class JobIncrementUserHandler extends IJobHandler {
    @Autowired
    private JobLauncherService jobLauncherService;

    @Autowired
    private IncrementService incrementService;

    @Autowired
    private Job incrementJob;
    @Override
    public ReturnT<String> execute(String s) throws Exception {
        return JobUtil.runJob4Executor("incrementUser",incrementService,jobLauncherService,incrementJob);
    }
}

4.使用xxl-job進(jìn)行Spring Batch任務(wù)調(diào)度

xxl-job-adminspring-batch-xxl-executor進(jìn)行部署后(注意配置正確),就可以在xxl-job-admin界面中對任務(wù)進(jìn)行調(diào)度。具體添加流程可參照xxl-job文檔即可。簡單說就是添加執(zhí)行器spring-batch-xxl-executor,然后添加任務(wù)incrementUserJobHandler,指定執(zhí)行的cron表達(dá)式,啟動即可。執(zhí)行任務(wù)后輸出如下:

5. 總結(jié)

本文基于數(shù)據(jù)庫增量同步的示例,使用Spring Batch結(jié)合xxl-job,實(shí)現(xiàn)可監(jiān)控的任務(wù)調(diào)度,使得批處理任務(wù)可維護(hù)性更強(qiáng),希望需要使用Spring Batch進(jìn)行批處理任務(wù)開發(fā)的人員有幫助。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容