xxl-job分布式定时任务
一、概述
1、简介
XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。
2、学习资料
官方地址中文版:http://www.xuxueli.com/xxl-job
原理与详细配置推荐观看此视频 https://www.bilibili.com/video/BV1WE411f7St
3、原理图
二、项目搭配与配置
1、执行数据库脚本
执行iids-msa-job工程下 DB目录下的sql脚本 tables_xxl_job.sql,创建数据库所需表
2、配置iids-msa-job相关配置
注:其他配置安装默认配置即可,随意修改可能导致项目无法启动)
### 设置端口与全局访问路径
server.port=9001
server.servlet.context-path= /iids-msa-job
### 调度中心JDBC链接
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl-job?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### 报警邮箱
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
### 调度中心通讯TOKEN,用于调度中心和执行器之间的通讯进行数据加密,非空时启用
xxl.job.accessToken=
### 调度中心国际化设置,默认为中文版本,值设置为“en”时切换为英文版本
xxl.job.i18n=
3、执行器相关配置
(1)pom添加依
<dependency>
<groupId>com.xuxueli
</groupId>
<artifactId>xxl-job-core
</artifactId>
<version>2.2.0
</version>
</dependency>
(2)执行器配置
### 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
xxl.job.executor.appname=xxl-job-executor-athena
### 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
xxl.job.executor.ip=
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9999
### 执行器通讯TOKEN [选填]:非空时启用;
xxl.job.accessToken=
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### 执行器日志保存天数 [选填] :值大于3时生效,启用执行器Log文件定期清理功能,否则不生效;
xxl.job.executor.logretentiondays=-1
(3)执行器配置类
@Slf4j
@Configuration
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses
;
@Value("${xxl.job.accessToken}")
private String accessToken
;
@Value("${xxl.job.executor.appname}")
private String appname
;
@Value("${xxl.job.executor.address}")
private String address
;
@Value("${xxl.job.executor.ip}")
private String ip
;
@Value("${xxl.job.executor.port}")
private int port
;
@Value("${xxl.job.executor.logpath}")
private String logPath
;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays
;
@Bean
public XxlJobSpringExecutor
xxlJobExecutor() {
log
.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor
= new XxlJobSpringExecutor();
xxlJobSpringExecutor
.setAdminAddresses(adminAddresses
);
xxlJobSpringExecutor
.setAppname(appname
);
xxlJobSpringExecutor
.setAddress(address
);
xxlJobSpringExecutor
.setIp(ip
);
xxlJobSpringExecutor
.setPort(port
+50);
xxlJobSpringExecutor
.setAccessToken(accessToken
);
xxlJobSpringExecutor
.setLogPath(logPath
);
xxlJobSpringExecutor
.setLogRetentionDays(logRetentionDays
);
return xxlJobSpringExecutor
;
}
}
(4)执行器任务
@Slf4j
@Component
public class JobDemo {
@XxlJob(value
= "sampleJob",init
= "init",destroy
= "destroy")
public ReturnT
<String> execute(String params
) throws Exception
{
XxlJobLogger
.log("xxl-job : hello world!");
log
.info("params:{}",params
);
for (int i
= 0; i
< 5; i
++) {
XxlJobLogger
.log("beat at:" + i
);
TimeUnit
.SECONDS
.sleep(2);
}
return ReturnT
.SUCCESS
;
}
@XxlJob("shardingJobHandler")
public ReturnT
<String> shardingJobHandler(String param
) throws Exception
{
ShardingUtil
.ShardingVO shardingVO
= ShardingUtil
.getShardingVo();
XxlJobLogger
.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO
.getIndex(), shardingVO
.getTotal());
for (int i
= 0; i
< shardingVO
.getTotal(); i
++) {
if (i
== shardingVO
.getIndex()) {
XxlJobLogger
.log("第 {} 片, 命中分片开始处理", i
);
} else {
XxlJobLogger
.log("第 {} 片, 忽略", i
);
}
}
return ReturnT
.SUCCESS
;
}
public void init(){
System
.out
.println("任务初始化");
}
public void destroy(){
System
.out
.println("任务销毁");
}
}
(5)调度中心admin配置
注册执行器 配置执行任务 任务配置详解 操作任务 查看执行日志