XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 官方文档:https://www.xuxueli.com/xxl-job/#%E4%BA%8C%E3%80%81%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8
img
下载源码导入idea,源码地址:https://gitee.com/xuxueli0323/xxl-job.git
数据库文件在源码doc/db目录下
调度中心配置文件地址:
/xxl-job/xxl-job-admin/src/main/resources/application.properties
## xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=xdclass.net
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
xxl.job.accessToken=javaxiaobear.cn
调度中心访问地址:http://localhost:8080/xxl-job-admin (该地址执行器将会使用到,作为回调地址)
默认登录账号 “admin/123456”, 登录后运行界面如下图所示。
image-20230630144053744
image-20230630144304198
<!-- http://repo1.maven.org/maven2/com/xuxueli/xxl-job-core/ -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.1</version>
</dependency>
xxl:
job:
admin:
addresses: http://127.0.0.1:8080/xxl-job-admin
# 执行器的名字
executor:
appname: javaxiaobear-xxl-job-test
accessToken: default_token
server:
port: 8081
@Configuration
@Slf4j
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.accessToken}")
private String accessToken;
//旧版的有bug
//@Bean(initMethod = "start", destroyMethod = "destroy")
@Bean
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;
}
}
image-20230630152704891
这里是因为在配置文件里面用的是自定义的执行器,所以我们需要新增,当然,你可以用默认的执行器
@Slf4j
@Component
public class MyXxlJobHandler {
@XxlJob("myXxlJobHandler")
public ReturnT<String> execute(String param){
log.info("小熊学Java 任务方法触发成功");
return ReturnT.SUCCESS;
}
}
image-20230630155107647
image-20230630155113771
需求
什么是分片任务
// 当前分片数,从0开始,即执行器的序号
int shardIndex = XxlJobHelper.getShardIndex();
//总分片数,执行器集群总机器数量
int shardTotal = XxlJobHelper.getShardTotal();
image-20230703140222054
解决思路
/**
* 100个用户,分片处理
*/
@XxlJob("myShardingJobHandler")
public void shardingJobHandler(){
// 当前分片数,从0开始,即执行器的序号
int shardIndex = XxlJobHelper.getShardIndex();
//总分片数,执行器集群总机器数量
int shardTotal = XxlJobHelper.getShardTotal();
XxlJobHelper.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);
List<Integer> allUserIds = getAllUserIds();
allUserIds.forEach(obj -> {
if (obj % shardTotal == shardIndex) {
log.info("第 {} 片, 命中分片开始处理用户id={}",shardIndex,obj);
}
});
}
private List<Integer> getAllUserIds() {
List<Integer> ids = new ArrayList<>();
for (int i = 0; i < 100; i++) {
ids.add(i);
}
return ids;
}
image-20230703141109854
image-20230703141221278
好了,本文就到这里了!如果觉得内容不错的话,希望大家可以帮忙点赞转发一波,这是对我最大的鼓励,感谢🙏🏻