xxl-job任务调度中心部署整合SpringBoot
创始人
2025-05-28 15:05:15
0

使用Docker部署调度中心

1、拉取镜像

docker pull xuxueli/xxl-job-admin:2.3.1

2、从gitee下载sql文件
https://gitee.com/xuxueli0323/xxl-job/tree/master/doc/db
在这里插入图片描述在数据库中运行sql文件
在这里插入图片描述
3、运行xxl-job镜像

docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://182.40.202.xxx:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=UTC --spring.datasource.username=root --spring.datasource.password=123456" -p 8090:8080 -p 9999:9999  -v /root/sifan/xxl-job:/data/applogs --name=xxl-job-admin -d xuxueli/xxl-job-admin:2.3.1

注意:数据库ip、用户名、密码配置成自己的;需要开放两个端口一个供调度器使用,一个供执行器使用;端口值自己可以定义。我这里使用了8090端口和9999端口。
4、访问控制面板
http://182.40.202.xxx:8090/xxl-job-admin/jobinfo
默认用户名:admin 密码:123456
注意:有设置防火墙和安全组的需要开放端口。
在这里插入图片描述

SpringBoot整合xxl-job

pom依赖

        com.xuxuelixxl-job-core2.3.1

yml配置

# 任务调度中心
api:initListen:###开启配置xxl: true
xxl:job:admin:addresses: http://182.40.202.xxx:8090/xxl-job-admin/userName: adminpassword: 123456accessToken: default_tokenexecutor:appname: core### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is nulladdress:### xxl-job executor server-infoip:### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;port: 9999### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径logpath: logs/xxl-job/jobhandler### xxl-job executor log-retention-dayslogretentiondays: 7

在yml中配置了一个执行器appname为core
配置类

@Configuration
@ConditionalOnProperty(prefix = "api", name = "initListen.xxl")
@Slf4j
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;@Beanpublic 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);xxlJobSpringExecutor.setAccessToken(accessToken);xxlJobSpringExecutor.setLogPath(logPath);xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);return xxlJobSpringExecutor;}}

SpringBoot连接任务调度中心

上面在yml配置了一个执行器
接下来去调度中心配置,点击新增
在这里插入图片描述这里appname写yml定义的appname的值。我这里是core
在这里插入图片描述
启动SpringBoot,成功注册
在这里插入图片描述任务调度中心也出现注册的信息
在这里插入图片描述

SpringBoot创建定时任务

在SpringBoot中创建一个Job用于测试,处理器的名字为helloWordHandler

@Component
@Slf4j
public class XxlJob {@com.xxl.job.core.handler.annotation.XxlJob("helloWordHandler")public void demoJobHandler() throws Exception {XxlJobHelper.log("XXL-JOB, Hello World.");log.info("XXL-JOB, Hello World.");for (int i = 0; i < 5; i++) {System.out.println("Hello World");XxlJobHelper.log("beat at:" + i);TimeUnit.SECONDS.sleep(1);}}
}

在调度中心创建定时任务
指定创建的执行器
在这里插入图片描述在这里插入图片描述写上cron和处理器,处理器是自己在代码中@XxlJob注解中的值
在这里插入图片描述
启动任务
在这里插入图片描述
把应用部署之后可以看到定时任务运行了

在这里插入图片描述调度日志也ok
停掉任务后台也不再打印

相关内容

热门资讯

成都百亿疫苗龙头欧林生物赴港上... 微成都报道1月12日,欧林生物(688319)公告,公司于2025年11月25日向香港联合交易所有限...
千亿出海+天价猴!CRO板块扎... 1月13日,A股CRO板块拉升,成分股诺思格(301333.SZ)、鸿博医药(301230.SZ)、...
“隐形”代工厂登上TOP10,... 对于平湖众多的羽绒服工厂主而言,一年到头几乎没有真正闲下来的日子。上半年做外贸,为海外市场的秋冬季备...
顺灏股份:拟7498万元增资参... 新京报贝壳财经讯 顺灏股份公告称,公司拟向参股子公司轨道辰光增资7498.04万元,增资完成后持股比...
“6000亿金矿”资产装入倒计... 新年伊始,疯狂了一年的黄金故事仍在继续。1月12日,湖南黄金(002155.SZ)披露将注入来自控股...
闻泰科技再起风波!与立讯就印度... 除了半导体业务陷入纠纷,闻泰科技(600745.SH)剥离集成业务的事项也遇到纠纷。据闻泰科技公告,...
一字跌停,2.5万股民“踩雷”... 红星资本局1月13日消息,昨日晚间,*ST岩石(即上海贵酒)公告,经公司财务部门初步测算,预计公司2...
拟赴港上市,袁记食品披露:20... 记者从香港联交所获悉,2025年1月12日,袁记食品集团股份有限公司(以下简称“袁记食品”)已递交上...
“2025寂寞股”榜单出炉:6... 随着2025年的收官,市场的焦点都汇集到了那些暴涨的明星股之下。但是,芸芸众生的日子同样值得推敲。这...
2026,大家都是木头姐 文|小卢鱼编辑|杨旭然刚刚过去的2025年,曾饱受质疑的“木头姐”凯茜·伍德(Cathie Wood...