环境
springboot:2.7.2
xxl-job-admin:2.3.0
xxl-job:2.3.1
搭建xxl-job-admin
初始化数据库脚本:xxl-job/tables_xxl_job.sql at master · xuxueli/xxl-job (github.com)
使用docker-compose搭建,方便快捷,容易管理
version: '3' services: xxl-job-admin: restart: always image: xuxueli/xxl-job-admin:2.3.1 container_name: xxl-job-admin-test environment: PARAMS: "--spring.datasource.url=jdbc:mysql://192.168.10.89:21900/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=dba --spring.datasource.password=1234*Qwer@xYz --xxl.job.i18n=zh_CN --xxl.job.triggerpool.fast.max=200 --xxl.job.triggerpool.slow.max=100 --xxl.job.logretentiondays=30 --xxl.job.accessToken=U2FsdGVkX1/FYrJLirwjwyaOj0PphS0DQS0evsc4p80=" ports: - 9090:8080 networks: docker-host: external: true
默认账号密码 admin 123456
接入xxl-job
添加依赖
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.1</version> </dependency>
新建一个类来初始化一下xxljob的配置
package com.ych.client.xxljob; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; @Configuration @Component 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 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; } }
增加配置信息
xxl-job: admin: addresses: http://192.168.10.89:9080/xxl-job-admin accessToken: U2FsdGVkX1/FYrJLirwjwyaOj0PphS0DQS0evsc4p80= executor: # 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册 app-name: ${spring.application.name} address: # 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"; ip: 192.168.10.3 # 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口; port: 0 # 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径; log-path: opt/xxl-job # 执行器日志保存天数 [选填] :值大于3时生效,启用执行器Log文件定期清理功能,否则不生效; log-retention-days: 7
实现一个demojob
package com.ych.client.jobs; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.XxlJob; @Component public class DemoJobHandler extends IJobHandler { private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class); @Override @XxlJob(value = "ychDemoJob") public void execute() throws Exception { logger.info("XXL-JOB, Hello World."); for (int i = 0; i < 5; i++) { logger.info("beat at:" + i); TimeUnit.SECONDS.sleep(2); } } }
遇到的问题汇总
- xxljob需要预先手动在管理后台新增对应的执行器,需要保证配置结点里面的app-name结点与【管理后台】.【执行器管理】里面的 AppName 完全一致,需要先有执行器才能注册成功
- 定时任务需要自己去管理后台新建,不会自动生成
- xxljob2.0.0版本去掉了JobHandler注解,改为XxlJob注解
标签:执行器,Springboot,admin,private,job,import,xxl From: https://www.cnblogs.com/yuchenghao/p/16624162.html