xxjob distributed task scheduling

Preface

I used scheduled tasks at work and selected xxjob by searching for information. The following is the introduction and basic use of xxjob.

Introduction to xxjob

XXL-JOB is a distributed task scheduling platform. Its core design goals are rapid development, easy learning, lightweight, and easy expansion.
The scheduling behavior is abstracted into a "dispatch center" public platform, and the platform itself does not bear business logic. The "dispatch center" is responsible for initiating scheduling requests.
The tasks are abstracted into scattered JobHandlers and handed over to the "executor" for unified management. The "executor" is responsible for receiving scheduling requests and executing the corresponding business logic in the JobHandler.
Refer to the official website: https://github.com/xuxueli/xxl-job
Chinese documentation: https://www.xuxueli.com/xxl-job/# "Distributed Task Scheduling Platform XXL-JOB"

System composition

1. Scheduling center
● Responsible for managing scheduling information, issuing scheduling requests according to scheduling configuration, and not responsible for business codes;
● The scheduling system is decoupled from tasks, which improves system availability and stability, and the performance of the scheduling system is no longer limited by the task module ;
● Supports visual, simple and dynamic management of scheduling information, including task creation, update, deletion, GLUE development and task alarms, etc. All the above operations will take effect in real time; ● Supports monitoring of scheduling results and execution logs, and supports executor Failover
.
2 .Executor
● Responsible for receiving scheduling requests and executing task logic. The task module focuses on task execution and other operations, making development and maintenance simpler and more efficient;
● Receive execution requests, termination requests and log requests from the "Scheduling Center", etc.

Download source code

Source code warehouse address:
https://github.com/xuxueli/xxl-job
http://gitee.com/xuxueli0323/xxl-job

maven warehouse address

<!-- http://repo1.maven.org/maven2/com/xuxueli/xxl-job-core/ -->
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>${
    
    最新稳定版本}</version>
</dependency>

Initialize "Scheduling Database"

Download the project source code and unzip it, obtain the "Scheduling Database Initialization SQL Script" and execute it.
The location of "Scheduling Database Initialization SQL Script" is: /xxl-job/doc/db/tables_xxl_job.sql
The dispatch center supports cluster deployment. In the case of a cluster, each node must be connected to the same mysql instance;
if mysql is the master-slave, the dispatch center cluster node must be Force the main library to be taken;

Compile source code

Unzip the source code, import the source code into the IDE according to the maven format, and use maven to compile. The source code structure is as follows:
xxl-job-admin: dispatch center
xxl-job-core: public dependency
xxl-job-executor-samples: executor Sample (Choose the appropriate version of the executor, which can be used directly, or you can refer to it and transform the existing project into an executor)
: xxl-job-executor-sample-springboot: Springboot version, manage the executor through Springboot, this method is recommended ;
:xxl-job-executor-sample-frameless: frameless version;
Insert image description here

Dispatch center configuration

Open the idea tool, modify the dispatch center, modify the database address, user, and password to use your own local database.
Dispatch center configuration file address:
/xxl-job/xxl-job-admin/src/main/resources/application.properties
Insert image description here
Dispatch center configuration content description:

### 调度中心JDBC链接:链接地址请保持和 2.1章节 所创建的调度数据库的地址一致
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_pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
### 报警邮箱
spring.mail.host=smtp.qq.com
spring.mail.port=25
[email protected]
spring.mail.password=xxx
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
### 调度中心通讯TOKEN [选填]:非空时启用;
xxl.job.accessToken=
### 调度中心国际化配置 [必填]: 默认为 "zh_CN"/中文简体, 可选范围为 "zh_CN"/中文简体, "zh_TC"/中文繁体 and "en"/英文;
xxl.job.i18n=zh_CN
## 调度线程池最大线程配置【必填】
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100
### 调度中心日志表数据保存天数 [必填]:过期日志自动清理;限制大于等于7时生效,否则, 如-1,关闭自动清理功能;
xxl.job.logretentiondays=30

Start the dispatch center service

xxl-job-admin service, select the XxlJobAdminApplication class
Insert image description here
to start the service. After starting, log in to the service. The default login account password is admin 123456
http://localhost:8888/xxl-job-admin/jobinfo
Insert image description here

Actuator project

"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)
Role: Responsible for receiving and executing the schedule from the "Scheduling Center"; the executor can be deployed directly or integrated into existing business projects.

maven dependency

Confirm that the maven dependency of "xxl-job-core" is introduced in the pom file;

Actuator configuration

Executor configuration, configuration file address:
/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties
content:

#调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
#执行器通讯TOKEN [选填]:非空时启用;
xxl.job.accessToken=
#执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
xxl.job.executor.appname=xxl-job-executor-sample
#执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
xxl.job.executor.address=
#执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
xxl.job.executor.ip=
#执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9999
#执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
#执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
xxl.job.executor.logretentiondays=30

Actuator component configuration

Executor component, configuration file address:
/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig .java
executor component, configuration content description:

@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
    
    
    logger.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;
}

After the startup is completed, log in to the dispatch center to view the executor.
Insert image description here

Task creation

Step 1: Create a new task:
Log in to the dispatch center and click the "New Task" button as shown below to create a new sample task. Then, refer to the parameter configuration of the task in the screenshot below and click Save.
Enter the picture description.
Insert image description here
Insert image description here
The above task is created in bean mode and runs by default as specified by cron.

Create task code

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;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * XxlJob开发示例(Bean模式)
 *
 * 开发步骤:
 *      1、任务开发:在Spring Bean实例中,开发Job方法;
 *      2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
 *      3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志;
 *      4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
 *
 * @author xuxueli 2019-12-11 21:52:51
 */
@Component
public class SampleXxlJob {
    
    
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);

    /**
     * 1、简单任务示例(Bean模式)
     */
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
    
    
        XxlJobHelper.log("XXL-JOB, Hello World.");
        System.out.println("demoJobHandler------------------");
        for (int i = 0; i < 5; i++) {
    
    
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }
}

After the task creation is completed, click on the page to start task execution and view the later output log.

Guess you like

Origin blog.csdn.net/mcband/article/details/129199698