在做这个例子之前先安装好数据库,Zookeeper等软件,这里例子部署3个job执行。
一,使用SPRING INITIALIZR https://start.spring.io/ 生成项目,然后添加Elastic-job-lite相关依赖。pom如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<elastic-job.version>2.1.5</elastic-job.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>${elastic-job.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.dangdang/elastic-job-lite-spring -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>${elastic-job.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二,写job
@Slf4j
public class MyElasticJob implements SimpleJob {
@Override
public void execute(ShardingContext context) {
switch (context.getShardingItem()) {
case 0:
// do something by sharding item 0
log.info("" + context.getShardingItem() + "," + context.getShardingTotalCount() + "," + context.getShardingParameter());
break;
case 1:
// do something by sharding item 1
log.info("" + context.getShardingItem() + "," + context.getShardingTotalCount() + "," + context.getShardingParameter());
break;
case 2:
// do something by sharding item 2
log.info("" + context.getShardingItem() + "," + context.getShardingTotalCount() + "," + context.getShardingParameter());
break;
// case n: ...
}
}
}
自定义异常处理类
/**
* 自定义异常处理
* @author
*/
public class CustomJobExceptionHandler implements JobExceptionHandler {
private Logger logger = LoggerFactory.getLogger(CustomJobExceptionHandler.class);
@Override
public void handleException(String jobName, Throwable cause) {
logger.error(String.format("Job '%s' exception occur in job processing", jobName), cause);
//这里自定义异常处理逻辑
}
}
三,创建数据库
CREATE DATABASE IF NOT EXISTS `batch_log`
四,配置job.xml文件
<?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
">
<!--configure registry center -->
<reg:zookeeper id="regCenter" server-lists="localhost:2181" namespace="dd-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
<bean id="elasticJobLog" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="driverClassName" value="org.mariadb.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/batch_log"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--configure job -->
<job:simple id="myElasticJob" class="com.vcredit.elasticjobdemo.job.MyElasticJob"
registry-center-ref="regCenter"
cron="0/59 * * * * ?"
sharding-total-count="3"
sharding-item-parameters="0=上海,1=北京,2=深圳"
description="我的第一个简单作业"
overwrite="true"
event-trace-rdb-data-source="elasticJobLog"
job-exception-handler="com.vcredit.elasticjobdemo.job.CustomJobExceptionHandler"/>
</beans>
五,在启动类上引入配置文件
@ImportResource(value = {"classpath:job.xml"})
@SpringBootApplication
public class ElasticJobDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticJobDemoApplication.class, args);
}
}
六,配置yml文件
spring:
profiles:
active: peer1
---
spring:
profiles: peer1
server:
port: 8080
---
spring:
profiles: peer2
server:
port: 8081
---
spring:
profiles: peer3
server:
port: 8082
七,启动程序
java -jar elastic-job-lite-demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar elastic-job-lite-demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar elastic-job-lite-demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
八,这一步使用elastic-job-lite-console以图形化的方式查看job。
首先到https://gitee.com/elasticjob/elastic-job下载源码,然后打包elastic-job-lite-console,到target目录,可以看到生成了压缩文件,拷贝压缩文件,解压缩,执行bin目录里面的start.bat或者start.sh,账号配置在conf目录下的auth.properties中,默认管理员账号密码都是root。
首先在全局配置>注册中心配置页面添加zookeeper配置,配置好之后点击右边连接按钮。
然后在全局配置>事件追踪数据源配置页面添加数据库配置,配置好之后点击右边连接按钮。
1535026235(1).png 然后在作业操作>作业维度就可以看到你的job配置信息。点击修改按钮可以修改job配置。
在作业历史>历史轨迹中可以查看作业执行情况。
网友评论