Home > Java > javaTutorial > How SpringBoot integrates XxlJob distributed task scheduling platform

How SpringBoot integrates XxlJob distributed task scheduling platform

WBOY
Release: 2023-05-14 18:58:06
forward
1256 people have browsed it

1. Introduction to XxlJob

XXL-JOB is a distributed task scheduling platform. Its core design goals are rapid development, easy learning, lightweight, and easy to expand. The source code is now open and connected to the online product lines of many companies, ready to use out of the box.

Why use distributed task scheduling? Whether it is for distributed projects or nginx load balancing, traditional scheduled task implementation methods are very slow to satisfy, such as

How SpringBoot integrates XxlJob distributed task scheduling platform

##2.XxlJob Quick Start

2.1 Download source code warehouse address

##Source code warehouse addresshttps://github.com /xuxueli/xxl-job##http://gitee.com/xuxueli0323/xxl-jobDownload

2.2 Scheduled task implementation steps

1. Execute the sql script in the project in the database

How SpringBoot integrates XxlJob distributed task scheduling platform

2. Directory structure description

xxl-job-admin: Scheduling center
xxl-job-core: Public dependency
xxl-job-executor-samples: Executor Sample (select the appropriate version of the executor, which can be used directly, You can also refer to it and transform existing projects into executors)
: xxl-job-executor-sample-springboot: Springboot version, manage executors through Springboot, this method is recommended;
: xxl-job- executor-sample-frameless: frameless version;

3. Modify the dispatch center configuration file


web
server.port=8080

server.servlet.context-path=/xxl-job-admin



actuator

management.server.servlet.context-path=/actuator

management.health.mail.enabled= false


resources

spring.mvc.servlet.load-on-startup=0

spring.mvc.static-path-pattern=/static/**
spring. resources.static-locations=classpath:/static/


freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/

spring.freemarker.suffix=.ftl

spring .freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.





# mybatis

mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml

#mybatis.type-aliases-package=com.xxl.job.admin.core.model


xxl-job, datasource
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


datasource -pool

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max -lifetime=900000
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=1000

xxl-job, email alarm mailbox, if the scheduled task execution fails, a message will be pushed to the mailbox
spring.mail.host=smtp.qq.com
spring.mail.port= 25

spring.mail.username=XXX@qq.com

spring.mail.from=XXX@qq.com
spring.mail.password=Authorization code
spring.mail.properties.mail .smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties .mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

xxl-job, access token dispatch center communication TOKEN [optional]: enabled when not empty;


If a communication token is configured, the accessToken of the two services of the dispatch center and the executor must be consistent
xxl.job.accessToken=

xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en")

# Dispatch center international configuration [required]: The default is "zh_CN"/Chinese Simplified, the optional range is "zh_CN"/Chinese Simplified, " zh_TC"/Chinese Traditional and "en"/English;

xxl.job.i18n=zh_CN

## xxl-job, triggerpool max size

# Scheduling thread pool maximum thread configuration [required] 】

xxl.job.triggerpool.fast.max=200How SpringBoot integrates XxlJob distributed task scheduling platformxxl.job.triggerpool.slow.max=100

xxl-job, log retention days

#Scheduling Number of days to save central log table data [required]: expired logs are automatically cleaned; it takes effect when the limit is greater than or equal to 7, otherwise, such as -1, the automatic cleaning function is turned off;
xxl.job.logretentiondays=30

If the above configuration has been performed correctly, the project can be compiled, packaged and deployed.


Scheduling center access address: http://localhost:8080/xxl-job-admin (This address will be used by the executor as the callback address)

The default login account is “admin/ 123456", the running interface after logging in is as shown below.

############4. Configure the executor project ######xxl-job-executor-sample-frameless native way (not recommended) ### "Executor" project :xxl-job-executor-sample-springboot (Multiple versions of executors are provided to choose from. The springboot version is taken as an example. It can be used directly, or you can refer to it and transform existing projects into executors)###Function: Responsible for receiving and executing the schedule from the "Scheduling Center"; the executor can be deployed directly or integrated into existing business projects. ########## web port###server.port=8081#### no web####spring.main.web-environment=false###

# log config
logging.config=classpath:logback.xml
# Scheduling center deployment root address [optional]: If there are multiple addresses for dispatching center cluster deployment, separate them with commas. The executor will use this address for "executor heartbeat registration" and "task result callback"; if it is empty, automatic registration will be turned off;
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl -job-admin

# Executor communication TOKEN [optional]: enabled when non-empty;
xxl.job.accessToken=

# Executor AppName [optional]: execution Grouping basis for executor heartbeat registration; if empty, automatic registration will be turned off
xxl.job.executor.appname=xxl-job-executor-llp
# Executor registration [optional]: Prioritize using this configuration as the registration address. If empty, use the embedded service "IP:PORT" as the registration address. This provides more flexible support for container-type executor dynamic IP and dynamic mapping port issues.
xxl.job.executor.address=
# Executor IP [optional]: The default is empty to automatically obtain the IP. When there are multiple network cards, you can manually set the specified IP. The IP will not be bound to the Host and is only used for communication. Practical; address information is used for "executor registration" and "scheduling center request and trigger tasks";
xxl.job.executor.ip=
# Executor port number [optional]: automatic if less than or equal to 0 Obtain; the default port is 9999. When deploying multiple executors on a single machine, be careful to configure different executor ports;
xxl.job.executor.port=0
# Executor running log file storage disk path [optional] [ Optional]: Automatic cleaning of expired logs, effective when the limit value is greater than or equal to 3; otherwise, such as -1, close the automatic cleaning function;
xxl.job.executor.logretentiondays=30

5 .Add an executor

How SpringBoot integrates XxlJob distributed task scheduling platform6.Add a scheduled task

How SpringBoot integrates XxlJob distributed task scheduling platform7.Write a test program

package com.xxl.job.executor.service.jobhandler;
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 TestXxlJob {
    private static Logger logger = LoggerFactory.getLogger(TestXxlJob.class);
    @XxlJob(value = "testJobHandler", init = "init", destroy = "destroy")
    public void testJobHandler() throws Exception {
        logger.info("进入xxlJob定时任务。。。。");
    }
    public void init(){
        logger.info("init");
    }
    public void destroy(){
        logger.info("destroy");
    }
    @XxlJob("testJobHandler02")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");
        logger.info("进入testJobHandler02定时任务。。。。");
    }
}
Copy after login

8. Execute scheduled tasks for testing

How SpringBoot integrates XxlJob distributed task scheduling platformView scheduling log

How SpringBoot integrates XxlJob distributed task scheduling platform3.SpringBoot integrates XxlJob

3.1 Create SpringBoot project and introduce dependencies

<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>${稳定版}</version>
</dependency>
Copy after login

3.2 Write properties configuration file

# web port
server.port=8081

# no web
# spring.main.web-environment=false

# log config

logging.config=classpath:logback.xml

# Scheduling center deployment root address [optional]: If the dispatching center cluster deployment exists Multiple addresses are separated by commas. The executor will use this address for "executor heartbeat registration" and "task result callback"; if it is empty, automatic registration will be turned off;
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl -job-admin

# Executor communication TOKEN [optional]: enabled when non-empty;

xxl.job.accessToken=llp


# Executor AppName [optional]: Executor heartbeat registration grouping basis; if empty, automatic registration is turned off

xxl.job.executor.appname=xxl-job-executor-llp

# Executor registration [optional]: Prioritize using this configuration as the registration address , when empty, use the embedded service "IP:PORT" as the registration address. This provides more flexible support for container-type executor dynamic IP and dynamic mapping port issues.
xxl.job.executor.address=
# Executor IP [optional]: The default is empty to automatically obtain the IP. When there are multiple network cards, you can manually set the specified IP. The IP will not be bound to the Host and is only used for communication. Practical; address information is used for "executor registration" and "scheduling center request and trigger tasks";
xxl.job.executor.ip=127.0.0.1
# Executor port number [optional]: less than or equal to 0 is obtained automatically; the default port is 9999. When deploying multiple executors on a single machine, be careful to configure different executor ports;
xxl.job.executor.port=9999
# Executor running log file storage disk path [Optional]: You need to have read and write permissions on the path; if it is empty, the default path will be used;
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
# Executor log file Number of days to save [optional]: Expired logs are automatically cleaned, and it takes effect when the limit value is greater than or equal to 3; otherwise, such as -1, the automatic cleaning function is turned off;
xxl.job.executor.logretentiondays=30

Note that when creating a task in the dispatch center, the appname and the appname configured in the executor must be consistent; it is recommended that the ip and port of the executor be configured

3.3 Write the xxljob configuration class

@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: &#39;xxx.xxx.xxx.&#39;
     *
     *      3、获取IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
     */
}
Copy after login

3.4 Write job class for testing

@Component
public class TestXxlJob {
    private static Logger logger = LoggerFactory.getLogger(TestXxlJob.class);
    @XxlJob(value = "testJobHandler", init = "init", destroy = "destroy")
    public void testJobHandler() throws Exception {
        logger.info("进入xxlJob定时任务。。。。");
    }
    public void init(){
        logger.info("init");
    }
    public void destroy(){
        logger.info("destroy");
    }
    @XxlJob("testJobHandler02")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");
        logger.info("进入testJobHandler02定时任务。。。。");
    }
}
Copy after login

Create executor

How SpringBoot integrates XxlJob distributed task scheduling platformCreate task

查看后台执行日志

How SpringBoot integrates XxlJob distributed task scheduling platform

如果需要xxlJob邮件报警功能,则需要在xxl-job-admin中进行配置邮件信息,并在创建任务时指定配置的邮箱地址

### xxl-job, email报警邮箱,如果定时任务执行失败会推送消息给该邮箱
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=XXX@qq.com
spring.mail.from=XXX@qq.com
spring.mail.password=授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

ps:如果定时任务执行频率很高,频繁失败的话,那收邮件就是一个噩梦~

How SpringBoot integrates XxlJob distributed task scheduling platform

4.XxlJob部署

4.1 jar包部署方式

jar包部署的方式比较简单,将项目编译打包部署到服务器上,其他服务和xxljob调度器之间网络、接口相通即可

4.2 Docker 镜像方式搭建调度中心

下载镜像

# Docker地址:https://hub.docker.com/r/xuxueli/xxl-job-admin/     (建议指定版本号)
docker pull xuxueli/xxl-job-admin
# 如需自定义 mysql 等配置,可通过 "-e PARAMS" 指定,参数格式 PARAMS="--key=value  --key2=value2" ;
# 配置项参考文件:/xxl-job/xxl-job-admin/src/main/resources/application.properties
# 如需自定义 JVM内存参数 等配置,可通过 "-e JAVA_OPTS" 指定,参数格式 JAVA_OPTS="-Xmx512m" ;
docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai" -p 8080:8080 -v /tmp:/data/applogs --name xxl-job-admin  -d xuxueli/xxl-job-admin:{指定版本}
Copy after login
Release Download
Download

The above is the detailed content of How SpringBoot integrates XxlJob distributed task scheduling platform. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template