search
HomeJavajavaTutorialHow SpringBoot integrates XxlJob distributed task scheduling platform

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定时任务。。。。");
    }
}

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>

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();
     */
}

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定时任务。。。。");
    }
}

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:{指定版本}
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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
怎么使用SpringBoot+Canal实现数据库实时监控怎么使用SpringBoot+Canal实现数据库实时监控May 10, 2023 pm 06:25 PM

Canal工作原理Canal模拟MySQLslave的交互协议,伪装自己为MySQLslave,向MySQLmaster发送dump协议MySQLmaster收到dump请求,开始推送binarylog给slave(也就是Canal)Canal解析binarylog对象(原始为byte流)MySQL打开binlog模式在MySQL配置文件my.cnf设置如下信息:[mysqld]#打开binloglog-bin=mysql-bin#选择ROW(行)模式binlog-format=ROW#配置My

Spring Boot怎么使用SSE方式向前端推送数据Spring Boot怎么使用SSE方式向前端推送数据May 10, 2023 pm 05:31 PM

前言SSE简单的来说就是服务器主动向前端推送数据的一种技术,它是单向的,也就是说前端是不能向服务器发送数据的。SSE适用于消息推送,监控等只需要服务器推送数据的场景中,下面是使用SpringBoot来实现一个简单的模拟向前端推动进度数据,前端页面接受后展示进度条。服务端在SpringBoot中使用时需要注意,最好使用SpringWeb提供的SseEmitter这个类来进行操作,我在刚开始时使用网上说的将Content-Type设置为text-stream这种方式发现每次前端每次都会重新创建接。最

SpringBoot怎么实现二维码扫码登录SpringBoot怎么实现二维码扫码登录May 10, 2023 pm 08:25 PM

一、手机扫二维码登录的原理二维码扫码登录是一种基于OAuth3.0协议的授权登录方式。在这种方式下,应用程序不需要获取用户的用户名和密码,只需要获取用户的授权即可。二维码扫码登录主要有以下几个步骤:应用程序生成一个二维码,并将该二维码展示给用户。用户使用扫码工具扫描该二维码,并在授权页面中授权。用户授权后,应用程序会获取一个授权码。应用程序使用该授权码向授权服务器请求访问令牌。授权服务器返回一个访问令牌给应用程序。应用程序使用该访问令牌访问资源服务器。通过以上步骤,二维码扫码登录可以实现用户的快

spring boot怎么对敏感信息进行加解密spring boot怎么对敏感信息进行加解密May 10, 2023 pm 02:46 PM

我们使用jasypt最新版本对敏感信息进行加解密。1.在项目pom文件中加入如下依赖:com.github.ulisesbocchiojasypt-spring-boot-starter3.0.32.创建加解密公用类:packagecom.myproject.common.utils;importorg.jasypt.encryption.pbe.PooledPBEStringEncryptor;importorg.jasypt.encryption.pbe.config.SimpleStrin

SpringBoot/Spring AOP默认动态代理方式是什么SpringBoot/Spring AOP默认动态代理方式是什么May 10, 2023 pm 03:52 PM

1.springboot2.x及以上版本在SpringBoot2.xAOP中会默认使用Cglib来实现,但是Spring5中默认还是使用jdk动态代理。SpringAOP默认使用JDK动态代理,如果对象没有实现接口,则使用CGLIB代理。当然,也可以强制使用CGLIB代理。在SpringBoot中,通过AopAutoConfiguration来自动装配AOP.2.Springboot1.xSpringboot1.xAOP默认还是使用JDK动态代理的3.SpringBoot2.x为何默认使用Cgl

使用Java SpringBoot集成POI实现Word文档导出使用Java SpringBoot集成POI实现Word文档导出Apr 21, 2023 pm 12:19 PM

知识准备需要理解ApachePOI遵循的标准(OfficeOpenXML(OOXML)标准和微软的OLE2复合文档格式(OLE2)),这将对应着API的依赖包。什么是POIApachePOI是用Java编写的免费开源的跨平台的JavaAPI,ApachePOI提供API给Java程序对MicrosoftOffice格式档案读和写的功能。POI为“PoorObfuscationImplementation”的首字母缩写,意为“简洁版的模糊实现”。ApachePOI是创建和维护操作各种符合Offic

springboot怎么整合shiro实现多验证登录功能springboot怎么整合shiro实现多验证登录功能May 10, 2023 pm 04:19 PM

1.首先新建一个shiroConfigshiro的配置类,代码如下:@ConfigurationpublicclassSpringShiroConfig{/***@paramrealms这儿使用接口集合是为了实现多验证登录时使用的*@return*/@BeanpublicSecurityManagersecurityManager(Collectionrealms){DefaultWebSecurityManagersManager=newDefaultWebSecurityManager();

Springboot如何实现视频上传及压缩功能Springboot如何实现视频上传及压缩功能May 10, 2023 pm 05:16 PM

一、定义视频上传请求接口publicAjaxResultvideoUploadFile(MultipartFilefile){try{if(null==file||file.isEmpty()){returnAjaxResult.error("文件为空");}StringossFilePrefix=StringUtils.genUUID();StringfileName=ossFilePrefix+"-"+file.getOriginalFilename(

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools