Spring Boot 3 整合 xxl-job 实现分布式定时任务调度,结合 Docker 容器化部署(图文指南)
整合Spring Boot 3和xxl-job实现分布式定时任务调度,并结合Docker进行容器化部署,可以参考以下步骤:
- 使用Maven或Gradle创建Spring Boot项目,并添加xxl-job的依赖。
<!-- 以Maven为例,添加xxl-job的依赖 -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>版本号</version>
</dependency>
- 在Spring Boot项目中配置xxl-job。
@Configuration
public class XxlJobConfig {
@Value("${xxl.job.admin.addres}")
private String adminAddresses;
@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
public XxlJobExecutor xxlJobExecutor() {
XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
// 配置管理地址
xxlJobExecutor.setAdminAddresses(adminAddresses);
// 执行器IP
xxlJobExecutor.setIp(ip);
// 执行器端口
xxlJobExecutor.setPort(port);
// 访问令牌
xxlJobExecutor.setAccessToken(accessToken);
// 日志文件保存地址
xxlJobExecutor.setLogPath(logPath);
// 日志保留天数
xxlJobExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobExecutor;
}
}
- 创建定时任务处理类,实现
JobHandler
接口。
@Component
public class SampleXxlJob implements JobHandler {
@Override
public void execute(String param) throws Exception {
// 任务逻辑
System.out.println("执行分布式定时任务...");
}
}
- 在
application.properties
或application.yml
中配置xxl-job相关属性。
# xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://xxl-job-admin-url
# executor address
xxl.job.executor.ip=
xxl.job.executor.port=9999
# executor log path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
# executor log retention days
xxl.job.executor.logretentiondays=30
# access token
xxl.job.accessToken=
# executor registry center span
xxl.job.executor.registry.retry=30
- 使用Dockerfile构建Docker镜像并结合Docker Compose进行容器化部署。
FROM openjdk:17-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
version: '3.8'
services:
xxl-job-executor:
build:
context
评论已关闭