xxl-job学习笔记(一)——使用xxl-job搭建作业调度平台

前言

最近项目需要引入作业调度相关功能,打算引入xxl-job,因为之前在公司参与过作业调度相关的开发,打算系统的记录下学习使用和分析的过程

搭建调度中心

抛开架构和原理,拿到一个新东西,首先让我们能够使用起来,搭建一个自己的任务
第一步: 下载源码
下载地址:https://github.com/xuxueli/xxl-job/
第二步: 执行sql
文件地址:xxl-job/doc/db/tables_xxl_job.sql

第三步: 修改xxl-job-admin项目配置
配置文件:application.properties
配置项:

1
2
3
4
### xxl-job, datasource
spring.datasource.url=${datasource.url}
spring.datasource.username=${datasource.username}
spring.datasource.password=${datasource.password}

打包命令:

1
xxl-job-admin git:(master) ✗ mvn clean -U package -Dmaven.test.skip=true

启动命令:

1
2
➜  xxl-job-admin git:(master) ✗ cd target 
➜ target git:(master) ✗ java -jar xxl-job-admin-2.1.0.jar

访问http://localhost:18827/xxl-job-admin
账号:admin
密码:123456
看到如下页面即搭建成功

编写代码

第一步: 搭建一个springboot项目,application.properties加入如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# web port
server.port=8081
# log config
logging.config=classpath:logback.xml
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### xxl-job executor address
xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job, access token
xxl.job.accessToken=
### xxl-job log path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job log retention days
xxl.job.executor.logretentiondays=-1

第二步: 编写配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

@Value("${xxl.job.admin.addresses}")
private String adminAddresses;

@Value("${xxl.job.executor.appname}")
private String appName;

@Value("${xxl.job.executor.ip}")
private String ip;

@Value("${xxl.job.executor.port}")
private int port;

@Value("${xxl.job.accessToken}")
private String accessToken;

@Value("${xxl.job.executor.logpath}")
private String logPath;

@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;


@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.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;
}

第三步: 编写jobhandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@JobHandler(value="demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {

@Override
public ReturnT<String> execute(String param) throws Exception {
XxlJobLogger.log("XXL-JOB, Hello World.");

for (int i = 0; i < 5; i++) {
XxlJobLogger.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
return SUCCESS;
}

}

第四步: 启动springboot项目后,配置执行器

点击保存后

出现这个,即为配置成功
第五步: 配置jobhandler,新增任务

第六步: 点击执行一次看看效果

到此springboot整合xxl-job就完成了,在后续添加任务时,只需要编写相应的handler去继承IJobHandler即可

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×