引用: https://www.icode9.com/content-3-1303414.html
引用: https://blog.csdn.net/new_daimond/article/details/125993932
中文文档:https://www.xuxueli.com/xxl-job/
下载路径: https://github.com/xuxueli/xxl-job/releases
通过mysql客户端工具执行脚本
xxl-job-2.3.1\doc\db\tables_xxl_job.sql
数据库名称为:xxl_job
,执行sql脚本成功后数据库表如下
xxl-job-2.3.1\xxl-job-admin\src\main\resources
目录下,编辑application.properties
和logback.xml
,见下图中红框圈住的配置项:
注意修改下面的token,保证连接的安全性。
调度中心通讯TOKEN [选填]:非空时启用;
这里只需要将
xxl-job-admin/src/main/resources
目录下的logback.xm
l文件中的
修改为
在.
xxl-job-2.3.1\xxl-job-admin
目录下执行cmd命令。mvn clean install
xxl-job-2.3.1\xxl-job-admin\target
目录下执行cmd
命令。
# Windows环境
java -jar xxl-job-admin-2.3.1.jar
# Mac和Linux环境
nohup java -jar xxl-job-admin-2.3.1.jar &
路径:
http://127.0.0.1:8081/xxl-job-admin
URL中的端口号需根据上述配置而定。
缺省账号密码:admin/123456。
则需要修改
application.properties
文件的连接信息,重新打包运行下。
1.// Docker地址:https://hub.docker.com/r/xuxueli/xxl-job-admin/ (建议指定版本号)
2.docker pull xuxueli/xxl-job-admin
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.xuxueligroupId>
<artifactId>xxl-job-coreartifactId>
<version>2.3.1version>
dependency>
dependencies>
package com.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class XxxJobApplication {
public static void main(String[] args) {
SpringApplication.run(XxxJobApplication.class, args);
}
}
配置说明:
### 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### 执行器通讯TOKEN [选填]:非空时启用;
xxl.job.accessToken=
### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
xxl.job.executor.appName=xxl-job-executor-sample
### 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
xxl.job.executor.addresses=
### 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
xxl.job.executor.ip=
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9999
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
xxl.job.executor.logPath=/data/applogs/xxl-job/jobhandler
### 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
xxl.job.executor.logRetentionDays=30
yaml配置:
xxl:
job:
admin:
addresses: http://127.0.0.1:8081/xxl-job-admin
accessToken: default_token
executor:
appName: xxx-test
addresses: ""
ip: ""
port: 9999
logPath: data/applogs/xxl-job/jobhandler
logRetentionDays: 1
package com.xxx.config;
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 XxlConfig {
private final Logger log = LoggerFactory.getLogger(XxlConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appName}")
private String appName;
@Value(value = "${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private String port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.logPath}")
private String logPath;
@Value("${xxl.job.executor.logRetentionDays}")
private String 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(Integer.parseInt(port));
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(Integer.parseInt(logRetentionDays));
return xxlJobSpringExecutor;
}
}
出现
>>>>>>>>>>> xxl-job remoting server start success, nettype = class com.xxl.job.core.server.EmbedServer, port = 9999
说明启动成功
查询任务调度中心
http://127.0.0.1:8081/xxl-job-admin/jobgroup
路径:http://127.0.0.1:8081/xxl-job-admin/jobinfo
任务管理器->选择执行器->新增
新增数据
修改执行器为测试执行器A
查看列表,发现状态是停止的
启动定时任务
路径:src/main/java/com/xxx/handle/TestHandler.java
package com.xxx.handle;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxx.config.XxlConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import static com.xxl.job.core.biz.model.ReturnT.FAIL;
import static com.xxl.job.core.biz.model.ReturnT.SUCCESS;
@Component
public class TestHandler {
private final Logger log = LoggerFactory.getLogger(XxlConfig.class);
//@XxlJob("myTestHandler")
public void myTestHandler() {
System.out.println("invoke myTestHandler....");
}
@XxlJob("myTestHandler")
public ReturnT<String> execute() throws Exception {
log.info(" >>用户等级更新任务开始执行....");
try {
//自定义调用接口
System.out.println("invoke execute....");
// userService.updateLevel();
return SUCCESS;
} catch (Exception e) {
log.info(String.valueOf(e));
return FAIL;
}
}
//@XxlJob("myTestHandler")
public ReturnT<String> demoJobHandler(String param) throws Exception {
System.out.println("来了!老弟:" + param);
return new ReturnT<String>(200, "demoJobHandler任务成功");
}
}
访问:http://127.0.0.1:8081/xxl-job-admin/joblog