Deploy locally and connect xxl-job to the project
1. Introduction to xxl-job
XXL-JOB is a distributed task scheduling platform. Its core design goals are rapid development, easy learning, lightweight, and easy expansion. The source code is now open and connected to the online product lines of many companies, out of the box.
What is Distributed Task Scheduling
Usually, the task scheduling program is integrated in the application. For example, the coupon service includes a scheduler for regularly issuing coupons, and the settlement service includes a task scheduler for periodically generating reports. Due to the use of a distributed architecture, a service often deploys multiple redundant instances to run our business. Running task scheduling in this distributed system environment is called distributed task scheduling .
2. Deploy xxl-job locally
1. Pull the project
gitee address : https://gitee.com/xuxueli0323/xxl-job
Open the project after cloning the project locally
- xxl-job-admin
The admin module is the scheduling center of xxl-job, which is mainly responsible for unified management of scheduling tasks on the task scheduling platform, triggering scheduling execution, and providing a task management platform. - The xxl-job-core
core module stores the executors in xxl-job. If there is a project that needs to be connected to xxl-job, then the xxl-job-core dependency needs to be introduced - xxl-job-executor-samples
store demo examples here, you can refer to the deployment
2. Initialize the database
sql file location : /doc/db
After executing the sql file, the effect is as follows
3. Modify the configuration file
First find the configuration file in the admin module
modify database link
Modify the port number and access address
4. Start the project
Start dispatch center
access
Default account password
admin
123456
3. Access xxl-job in your own project
1. Introduce dependencies
Introduce dependencies in maven of the local project
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
2. Write configuration class
package com.example.springboot.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;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@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();
*/
}
3. Modify the configuration file
xxl:
job:
admin:
addresses: http://127.0.0.1:8089/xxl-job-admin
accessToken: default_token
executor:
appname: spring-boot
address:
ip:
port: 6666
logpath: /data/applogs/xxl-job/jobhandler
logretentiondays: 30
4. Enter the project in the dispatch center
Enter the executor management of the dispatch center
Add a new local project
![Insert picture description here](https://img-blog.csdnimg.cn/2f5e52e6fbb34d8188e3c12e1ef8a070.png
4. How to use it in local projects
1. Test case
package com.example.springboot.job;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
@Component
public class XxlJobDemo {
@XxlJob("test")
public void xxlJobTest(){
XxlJobHelper.log("输出日志");
// 参数
System.out.println(XxlJobHelper.getJobParam());
// 多少台机器
System.out.println(XxlJobHelper.getShardTotal());
// 现在是第几台机器
System.out.println(XxlJobHelper.getShardIndex());
}
}
2. Configure the test case to the dispatch center
What is written in the JobHandler is the content in the @XxlJob annotation
3. Test
Start the local project first, then start the task
test successfully