文档
上github官方说明文档
https://github.com/xuxueli/xxl-job
需要准备的环境
Maven3+
Jdk1.8+
Mysql5.7+
下载安装包
解压后获取初始化sql
初始化数据库和表
解压后用idea打开
配置调度中心
改下xxl-job-admin模块的数据库地址和tomcat端口,然后logback日志路径可以自定义修改
然后打成jar包,放到服务上java -jar跑起来就行了
默认登录账号 “admin/123456”
配置xxl-job包里自带的执行器,一会可以做测试,已有项目的配置在下面
依旧打成jar包,然后跑起来
这个属性和配置执行器有关
xxl.job.executor.appname=xxl-job-executor-sample
已有项目配置xxl-job
引入依赖,和当前调度器执行器版本一致
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
配置XxlJobSpringExecutor参数
package com.fchan.springcloudstream.xxl;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
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;
@Configuration
public class XxlJobConfig {
private Logger logger = 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() {
logger.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;
}
/**
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
*
* 1、引入依赖:
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2、配置文件,或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}
参数demo
xxl:
job:
accessToken: ''
admin:
#这个ip+端口需要开启防火墙
addresses: http://xxx.xxx.xxx.xxx:30020/xxl-job-admin
executor:
ip: ''
address: ''
#这个appname就是一会配置执行器时候要填写的appname
appname: xxl-job-executor-sample
#执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
logpath: /home/ubuntu/app/xxljob/logs/jobhandler
# 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
logretentiondays: 30
#执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
#不要和当前tomcat端口相同
port: 30022
配置一个自定义任务
package com.fchan.springcloudstream.xxl;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class MyJob {
private Logger logger = LoggerFactory.getLogger(MyJob.class);
//test1JobHandler就是一会配置任务的时候填写的JobHandler输入框中的值
@XxlJob(value = "test1JobHandler")
public void test1(){
XxlJobHelper.log("my-JOB, Hello World.");
String command = XxlJobHelper.getJobParam();
XxlJobHelper.log("传入的参数");
logger.info("command:" + command);
}
}
配置执行器
配置任务
执行一波测试
成功~