是目前主流的分布式任务调度平台,许多公司的产品业务都有XXL-JOB的接入
准备: 在pull镜像创建容器之前先准备好mysql,后面配置需要,表的sql:[xxl_job],进入db目录,其中 tables_xxl_job.sql就是我们所需要的数据库表 一.docker部署xxl-job- docker拉取最新镜像,目前最新的版本 2.2.0
docker pull xuxueli/xxl-job-admin:2.2.0
- 创建并运行容器 (注意数据库的url、用户名、密码、库名对应上。端口不被占用就行)
docker run -di -e PARAMS="--spring.datasource.url=jdbc:mysql://localhost:3306/xxl_job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=root --spring.datasource.password=123456" -p 8086:8080 -v /tmp:/data/applogs --name xxl-job-admin xuxueli/xxl-job-admin:2.2.0
- 浏览器登录访问(http://ip:8086/xxl-job-admin/ , 默认用户名admin 密码 123456)
- 导入依赖(版本对应上)
com.xuxueli
xxl-job-core
2.2.0
- 配置文件
xxl.job.admin.addresses = http://127.0.0.1:8086/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken =
### 配置当前执行器的名字
xxl.job.executor.appname = ${spring.application.name}
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address =
xxl.job.executor.ip =
xxl.job.executor.port = 9999
# 配置日志文件的路径
xxl.job.executor.logpath = /usr/local/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays = 30
- 自定义xxl-job注解
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@import({XxlJobConfig.class})
public @interface EnableXxlJob {
}
- 读取配置文件并创建自动注册到admin管理平台的执行器 onLine 机器地址
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import groovy.util.logging.Log;
import groovy.util.logging.Slf4j;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Data
@Slf4j
@Configuration
public class XxlJobConfig {
private static final Logger log = LoggerFactory.getLogger(XxlJobConfig.class);
@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);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
- 在服务启动类上加自定义注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
@EnableCSFeignClients
@EnableXxlJob
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class);
System.out.println("(♥◠‿◠)ノ゙ 系统模块启动成功");
}
}
- 方法注解使用
@XxlJob("mywindTestJobHandler")
public ReturnT mywindTestJobHandler(String s) throws Exception {
logger.info(">>>>>>>>>>> xxl-job test."+s);
return new ReturnT(ReturnT.SUCCESS_CODE, "测试成功");
}
测试结果
- 新增任务管理
- 新增执行器
- 开始运行
- 结果及日志



