XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码可以根据自己需要进行修改,开箱即用,非常爽;
首先去github上下载:https://github.com/xuxueli/xxl-job/
下载源码,然后在本地进行编译
修改配置xxl-job-admin中的application.properties
server.port=8080
server.servlet.context-path=/xxl-job-admin
### 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=xxx
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### xxl-job, email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
### xxl-job, access token
xxl.job.accessToken=xxxxxx
配置很多,但是只需要修改上面这几个,最好将xxl.job.accessToken添加上进行token验证
修改完打包部署
部署完可以通过地址查看管理页面:http://127.0.0.1:8080/xxl-job-admin/
默认账号密码admin 123456,可以登录进去看看
创建网关项目pom文件中引入jar包
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
在配置文件中增肌配置
xxl:
job:
accessToken: xxxxxx #修改的token秘钥
admin:
addresses: http://10.1.11.60:8080/xxl-job-admin/ #上面部署xxl-job-admin地址
executor:
appname: dayunmotor-tsp-gateway-xxl-job #本项目名称
ip: 10.1.5.27 #这个是本项目部署地址和端口
port: 30003 #注意这里不是本项目的端口是xxl-job执行请求的端口
logpath: /data/applogs/xxl-job/jobhandler
logretentiondays: 30
在项目中增加配置类
@Slf4j
@Configuration
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.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.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
增加定时任务
@Slf4j
@Component
public class RemoteUpgradeTask extends IJobHandler {
@XxlJob(value = "remoteUpgradeTask")
public ReturnT<String> execute(String s) throws Exception {
try {
log.info("执行任务");
return IJobHandler.SUCCESS;
} catch (Exception e) {
log.error("执行任务异常", e);
return new ReturnT<>(IJobHandler.FAIL.getCode(), "执行任务失败");
}
}
//执行初始化方法
@Override
public void init() throws InvocationTargetException, IllegalAccessException {
super.init();
}
//执行销毁方法
@Override
public void destroy() throws InvocationTargetException, IllegalAccessException {
super.destroy();
}
}
然后去xxl-job-admin中增加执行器,增加完启动项目,可以看到启动的执行器
然后创建定时任务:注意这里的JobHandler就是代码里的 @XxlJob(value = “remoteUpgradeTask”)
创建完启动任务,就可以执行了,这个就完成了
不过如果使用windows的时候注意关闭防火墙,不然没法注册上去
如果本地有多网络需要指定IP,而服务器只有一个网络,为了保证本地可以进行调试,可以进行兼容;
获取当前环境,根据环境进行设置IP
首先获取当前spring.profiles.active
@Configuration
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(null == ApplicationContextUtils.applicationContext) {
ApplicationContextUtils.applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static String getActiveProfile() {
return applicationContext.getEnvironment().getActiveProfiles()[0];
}
}
然后修改xxl-job配置,修改XxlJobConfig类
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
if(ApplicationContextUtils.getActiveProfile().equalsIgnoreCase("local")){
xxlJobSpringExecutor.setIp(ip);
}
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}