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ù)同步的工作了,下面是之前的文章列表:
- 數(shù)據(jù)批處理神器-Spring Batch(1)簡介及使用場景
- 快速了解組件-spring batch(2)之helloworld
- 快速使用組件-spring batch(3)讀文件數(shù)據(jù)到數(shù)據(jù)庫
- 決戰(zhàn)數(shù)據(jù)庫-spring batch(4)數(shù)據(jù)庫到數(shù)據(jù)庫
- 便捷的數(shù)據(jù)讀寫-spring batch(5)結(jié)合beetlSql進(jìn)行數(shù)據(jù)讀寫
- 增量同步-spring batch(6)動態(tài)參數(shù)綁定與增量同步
批處理任務(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-job
對Spring 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-admin
和spring-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ā)的人員有幫助。