一、获取XXL-JOB分布式任务调度系统项目源代码
1、项目地址:https://github.com/xuxueli/xxl-job
2、使用git克隆至本地:$ git clone https://github.com/xuxueli/xxl-job.git
二、整合
1、 在IDEA上创建一个Maven工程,命名为xxl-job-demo
① 点击 File => New => Project,新建项目,选择Maven
② 点击界面上的Next按钮,出现如下界面,输入项目名和项目保存位置,点击Finish按钮即可创建项目
③ 删除xxl-job-demo项目下的src目录,项目结构如下图
2、引入xxl-job源码中的xxl-job-admin模块至xxl-job-demo项目中
① 选中xxl-job-demo项目名,点击File => Project Structure,打开如下界面,导入xxl-job-admin模块
② 导入xxl-job-admin模块之后,引入依赖会报错,先不用管,将xxl-job-admin手动移入xxl-job-demo项目下,作为它的子模块
③ 在xxl-job-demo项目pom.xml添加如下依赖
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.5.RELEASEversion>
<relativePath/>
parent>
<properties>
<spring-boot.version>2.2.5.RELEASEspring-boot.version>
<mybatis-spring-boot-starter.version>2.1.2mybatis-spring-boot-starter.version>
<mysql-connector-java.version>8.0.20mysql-connector-java.version>
properties>
<modules>
<module>xxl-job-adminmodule>
modules>
④ 修改xxl-job-admin项目pom.xml如下依赖
<parent>
<groupId>com.xuxueligroupId>
<artifactId>xxl-jobartifactId>
<version>2.2.1-SNAPSHOTversion>
parent>
将如上依赖修改为:
<parent>
<groupId>com.baigegroupId>
<artifactId>xxl-job-demoartifactId>
<version>1.0-SNAPSHOTversion>
parent>
将如下依赖的版本号换成2.2.0
<dependency>
<groupId>com.xuxueligroupId>
<artifactId>xxl-job-coreartifactId>
<version>2.2.0version>
dependency>
⑤ 执行克隆项目中的xxl-job\doc\db\tables_xxl_job.sql脚本文件创建数据库
⑥ 修改xxl-job-admin模块下的application.properties文件中的数据源配置信息,将用户名、密码换成自己的
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=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
⑦ 启动XxlJobAdminApplication,在浏览器中输入http://localhost:8080/xxl-job-admin,初次访问,会跳转到登录页面,默认用户名admin,密码123456
3、创建执行器xxl-job-executor模块
① 新建一个Maven工程,选中xxl-job-demo右击 => New => Module
② 在xxl-job-executor模块的pom.xml添加如下依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.xuxueligroupId>
<artifactId>xxl-job-coreartifactId>
<version>2.2.0version>
dependency>
dependencies>
③ 在Resource静态资源文件夹下创建application.properties配置文件,或者从xxl-job项目的xxl-job\xxl-job-executor-samples\xxl-job-executor-sample-springboot\src\main\resources文件夹进行复制
server.port=8081
logging.config=classpath:logback.xml
xxl.job.admin.addresses = http://127.0.0.1:8080/xxl-job-admin
xxl.job.executor.appname = xxl-job-executor
xxl.job.executor.port = 9999
xxl.job.executor.logpath = /data/applogs/xxl-job/jobhandler
xxl.job.executor.logretentiondays = 30
④ 在Resource静态资源文件夹下添加logback.xml文件,从xxl-job项目的xxl-job\xxl-job-executor-samples\xxl-job-executor-sample-springboot\src\main\resources文件夹进行复制
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<contextName>logbackcontextName>
<property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%npattern>
encoder>
appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zipfileNamePattern>
rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
pattern>
encoder>
appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
root>
configuration>
⑤ 创建XxlJobConfig.java配置类,可以从xxl-job项目的xxl-job\xxl-job-executor-samples\xxl-job-executor-sample-springboot\src\main\java\com\xxl\job\executor\core\config文件夹下进行复制
package com.baige.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
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.executor.appname}")
private String appName;
@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.setPort(port);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
⑥ 创建HelloHandler.java执行器类,可以从xxl-job项目的xxl-job\xxl-job-executor-samples\xxl-job-executor-sample-springboot\src\main\java\com\xxl\job\executor\service\jobhandler文件夹下进行复制
package com.baige.handler;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.stereotype.Component;
import com.xxl.job.core.handler.annotation.XxlJob;
@Component
public class HelloHandler {
@XxlJob("helloHandler")
public ReturnT<String> demoJobHandler(String param) throws Exception {
System.out.println("Hello,World");
XxlJobLogger.log("XXL-JOB, Hello World.");
return ReturnT.SUCCESS;
}
}
⑦ 创建执行器的启动类XxlJobExecutorApplication.java
package com.baige;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class XxlJobExecutorApplication {
public static void main(String[] args) {
SpringApplication.run(XxlJobExecutorApplication.class, args);
}
}
⑧ 启动执行器
4、在xxl-job-admin后台管理的web界面,配置执行器,进行测试
① 新增执行器,如下图所示
可能配置好执行器注册不上,重启xxl-job-executor模块的启动类,刷新页面,即可注册上
② 新增任务
③ 启动任务
④ 在IDEA控制台中查看打印信息
至此,spring boot整合xxl-job分布式任务调度系统完成。