1、环境搭建,下载源码
2、创建mysql数据库xxl-job,并导入xxl-job\doc\db\tables_xxl_job.sql
3、修改xxl-job-admin中配置文件 application.properties
国际化 xxl.job.i18n=en ,默认为空中文
4、导入idea,直接springboot启动
xxl-job-admin项目下XxlJobAdminApplication类即可
5、http://localhost:8080/xxl-job-admin即可访问
账号密码在 application.properties中
xxl.job.login.username=admin
xxl.job.login.password=123456
6、xxl-job打包
JRE_home是jdk路径
7、打包后启动java -jar xxl-job-admin-2.1.0-SNAPSHOT.jar
--------------------------------------------------------------------------------
8、项目中引入定时需要配置
xxl.job.executor.appname=
xxl.job.executor.port=
每个项目的配置不能一样,xxl.job.executor.appname必须和执行器name一样,端口则只需多个不重复即可
9、用户管理,默认管理员 admin/123456,可以修改其密码
可以创建用户并分配权限
10、需要初始化参数
package com.djbxlife.common;
import com.djbxlife.common.dao.DateBaseVOMapper;
import com.djbxlife.common.domain.GetByGroupCodeVOs;
import com.djbxlife.insure.enums.XxlJobParam;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration
@ComponentScan(basePackages = "com.djbxlife.job")
public class XxlJobConfig {
@Autowired
DateBaseVOMapper dateBaseVOMapper;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
String adminAddresses = null;
String appName = null;
String ip = null;
Integer port=null;
String accessToken = null;
String logpath = null;
Integer logRetentionDays=null;
List<GetByGroupCodeVOs> list =dateBaseVOMapper.selectByParentIds("XxlJobParam");
if(null != list && list.size()>0){
for(GetByGroupCodeVOs dateBaseVO:list){
//调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
if(XxlJobParam.ADDRESS.getCode().equals(dateBaseVO.getCode())){
adminAddresses =dateBaseVO.getName();
}
//执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
if(XxlJobParam.APPANME.getCode().equals(dateBaseVO.getCode())){
appName =dateBaseVO.getName();
}
//执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
if(XxlJobParam.IP.getCode().equals(dateBaseVO.getCode())){
ip =dateBaseVO.getName();
}
//执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
if(XxlJobParam.PORT.getCode().equals(dateBaseVO.getCode())){
String portStr= dateBaseVO.getName();;
if(!StringUtils.isEmptry(portStr)){
port = Integer.parseInt(portStr);
}
}
//执行器通讯TOKEN [选填]:非空时启用;
if(XxlJobParam.ACCESSTOKEN.getCode().equals(dateBaseVO.getCode())){
accessToken =dateBaseVO.getName();
}
//执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
if(XxlJobParam.LOGPATH.getCode().equals(dateBaseVO.getCode())){
logpath =dateBaseVO.getName();
}
//执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理
if(XxlJobParam.LOGRETEBTUIONDAYS.getCode().equals(dateBaseVO.getCode())){
String logRetentionDaysStr =dateBaseVO.getName();
if(!StringUtils.isEmptry(logRetentionDaysStr)){
logRetentionDays = Integer.parseInt(logRetentionDaysStr);
}
}
}
}
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;
}
}
11、用过注解,这种注入是起作用的
12、总结
启动admin服务后,只需要在定时服务引入core,即可