本篇文章會對Elastic-job與spring-·boot進行整合,同時搭建任務監控平臺經過elastic-job-console以便對任務進行管理,至於涉及到的分片sharding
及配置項等核心概念能夠參考官網文檔,本文主要是快速搭建工程,讓定時任務先跑起來,文末附源碼地址。php
Elastic-job官網中文 elasticjob.io/index_zh.ht…html
github地址 github.com/elasticjob/…java
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
複製代碼
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
複製代碼
package com.morning.morningshiro.jobs;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.morning.morningshiro.dao.entity.UserEntity;
import javax.annotation.Resource;
import java.util.List;
public class MyElasticJob implements SimpleJob {
@Override
public void execute(ShardingContext context) {
// 根據分片項執行任務
System.out.println(context.toString());
switch (context.getShardingItem()) {
case 0:
break;
case 1:
// do something by sharding item 1
break;
case 2:
// do something by sharding item 2
break;
// case n: ...
}
}
}
複製代碼
package com.morning.morningshiro.jobs;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
public class JobListener implements ElasticJobListener {
@Override
public void beforeJobExecuted(ShardingContexts shardingContexts) {
System.out.println(shardingContexts.toString());
}
@Override
public void afterJobExecuted(ShardingContexts shardingContexts) {
System.out.println(shardingContexts.toString());
}
}
複製代碼
resources
文件下<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:reg="http://www.dangdang.com/schema/ddframe/reg" xmlns:job="http://www.dangdang.com/schema/ddframe/job" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.dangdang.com/schema/ddframe/reg http://www.dangdang.com/schema/ddframe/reg/reg.xsd http://www.dangdang.com/schema/ddframe/job http://www.dangdang.com/schema/ddframe/job/job.xsd ">
<!--配置做業註冊中心 -->
<!--namespace爲註冊到zk的名稱-->
<reg:zookeeper id="regCenter" server-lists="127.0.0.1:2181" namespace="dd-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3"/>
<bean id="myElasticJob" class="com.morning.morningshiro.jobs.MyElasticJob"/>
<!-- 配置做業-->
<!--overwrite 修改後覆蓋原有定時任務-->
<!--id 任務ID-->
<job:simple id="job1" job-ref="myElasticJob" overwrite="true" registry-center-ref="regCenter" cron="0/30 * * * * ?" sharding-total-count="4" sharding-item-parameters="0=USER,1=TWO" description="test">
<job:listener class="com.morning.morningshiro.jobs.JobListener"/>
</job:simple>
</beans>
複製代碼
@SpringBootApplication
@ImportResource(locations = {"classpath:spring-elastic-job.xml"})
public class MorningShiroApplication {
public static void main(String[] args) {
SpringApplication.run(MorningShiroApplication.class, args);
}
}
複製代碼
經過elastic-job-console工程來進行監控任務執行, github.com/elasticjob/…git
下載工程,導入Idea啓動elastic-job-lite-console工程,啓動以前修改父pom文件中的spring框架版本爲5.1.9.RELEASE
,而後啓動,瀏覽器輸入localhost:8899
便可進入控制檯。github
4.1 控制檯界面,配置zk服務地址 web
4.2 查看任務類表 spring
4.3 查看服務列表 api
4.4 修改任務 瀏覽器
關於zk集羣搭建查看: juejin.im/post/5cee0d…bash