一.源码编译(Windows)
1、拉取源码:xxl-job: 一个分布式任务调度平台
2、导入IDEA,查看一下目录
3、初始化数据库,配置数据库连接信息(在本地导入上图中的sql文件,并把配置文件给改好)
4.1、编译运行
访问http://localhost:8080/xxl-job-admin/toLogin
进入管理页面。默认账号/密码:admin/123456。
4.2、部署
(1).打成jar包,使用
java -jar jar包名字
运行。
(2).打开cmd,使用
java -jar xxl-job-admin-2.4.0-SNAPSHOT.jar
命令
(3).访问
http://localhost:8080/xxl-job-admin/toLogin
二.SpringBoot配置:
使用XXL-JOB(集成SpringBoot)(必须两个服务都在本地,或者都在服务器端)
1.导入maven依赖
<!-- xxl-job-core --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.1</version> </dependency>
2.yam配置
xxl: job: admin: addresses: http://192.168.35.113:18080/web/xxl-job-admin/ accessToken: i7pQX7Av5u61KeRR480XaCVDA7PdbvL7 executor: # ip: appname: ehcindexCollect port: 1000 # 日志地址 logpath: ./logs/xxl-job/jobhandler # 日志保存时间 logretentiondays: 30
3.XxlJobConfig配置类
package com.yhtech.ehcindex.svc.service.config; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @Slf4j public class XxlJobConfig { @Value("${xxl.job.admin.addresses}") private String adminAddresses; @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.accessToken}") private String accessToken; @Value("${xxl.job.executor.logpath}") private String logPath; @Value("${xxl.job.executor.logretentiondays}") private int logRetentionDays; @Bean //2.3版本以上 // @Bean(initMethod = "start", destroyMethod = "destroy") //2.3版本以下 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; } }
4.测试
@Component
public class XxlDemoHandler {
@XxlJob("Demo")
public ReturnT<String> demo(){
String param = XxlJobHelper.getJobParam();
XxlJobHelper.log("测试开始");
System.out.println();
System.out.println();
System.out.println();
System.out.println(param);
System.out.println("测试完成!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println();
System.out.println();
System.out.println();
XxlJobHelper.log("测试开结束");
return ReturnT.SUCCESS;
}
}