Construction and use of distributed task scheduling platform XXL-JOB

Reference documents:

xxl official document  https://www.xuxueli.com/xxl-job/

xxl build tutorial  https://www.cnblogs.com/ysocean/p/10541151.html#_label0

xxl guide document  https://cloud.tencent.com/developer/article/1710315

 

Overview:

XXL-JOB is a distributed task scheduling platform, an open source project from Dianping Xu Xueli (xxl is the initials of the author's name in pinyin). 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.

The main functions of the distributed task scheduling platform:

  • When the tasks of the same service multiple instances are mutually exclusive, they can be scheduled uniformly.
  • Task scheduling supports high availability, monitoring, and fault alarms.
  • To manage and track the task scheduling results of each service node in a unified manner, it is necessary to record and save task attribute information, etc.

Currently, the mainstream distributed task scheduling platforms are elasticjob and xxl-job.

Comparison between xxl-job and elasticjob:

The original intention of elasticjob is to face high-concurrency and complex businesses. Even when the business volume is large and there are many servers, it can do a good job of task scheduling and use server resources as much as possible. Using ZooKeeper makes it highly available, consistent, and scalable. It is written on the official website that elasticjob is decentralized. The main server is elected through the ZooKeeper election mechanism. If the main server hangs up, a new main server will be re-elected. Therefore, elasticjob has good scalability and availability, but it is somewhat complicated to use and operate .

xxl-job is the opposite. It uses a central scheduling platform to schedule multiple executors to execute tasks. The scheduling center uses DB locks to ensure the consistency of cluster distributed scheduling. In this way, expanding executors will increase the pressure on DB, but if In fact, the database here is only responsible for the scheduling and execution of tasks. But if there are not a large number of executors and tasks, it will not cause pressure on the database. In fact, most companies have fewer tasks and fewer executors (although interviews often ask some high-concurrency questions).

Relatively speaking, the xxl-job central scheduling platform is lightweight, out-of-the-box, easy to operate, quick to use, and has a very good integration with SpringBoot, and the monitoring interface is integrated in the scheduling center, and the interface is simple . The cost of maintenance is not high, and there are email alerts for failures and so on. This makes many companies choose xxl-job as the scheduling platform.

In addition, the xxl-job environment depends on mysql, not ZooKeeper.

 

Main features :

1、简单:支持通过Web页面对任务进行CRUD操作,操作简单,一分钟上手;
2、动态:支持动态修改任务状态、启动/停止任务,以及终止运行中任务,即时生效;
3、调度中心HA(中心式):调度采用中心式设计,“调度中心”自研调度组件并支持集群部署,可保证调度中心HA;
4、执行器HA(分布式):任务分布式执行,任务”执行器”支持集群部署,可保证任务执行HA;
5、注册中心: 执行器会周期性自动注册任务, 调度中心将会自动发现注册的任务并触发执行。同时,也支持手动录入执行器地址;
6、弹性扩容缩容:一旦有新执行器机器上线或者下线,下次调度时将会重新分配任务;
7、触发策略:提供丰富的任务触发策略,包括:Cron触发、固定间隔触发、固定延时触发、API(事件)触发、人工触发、父子任务触发;
8、调度过期策略:调度中心错过调度时间的补偿处理策略,包括:忽略、立即补偿触发一次等;
9、阻塞处理策略:调度过于密集执行器来不及处理时的处理策略,策略包括:单机串行(默认)、丢弃后续调度、覆盖之前调度;
10、任务超时控制:支持自定义任务超时时间,任务运行超时将会主动中断任务;
11、任务失败重试:支持自定义任务失败重试次数,当任务失败时将会按照预设的失败重试次数主动进行重试;其中分片任务支持分片粒度的失败重试;
12、任务失败告警;默认提供邮件方式失败告警,同时预留扩展接口,可方便的扩展短信、钉钉等告警方式;
13、路由策略:执行器集群部署时提供丰富的路由策略,包括:第一个、最后一个、轮询、随机、一致性HASH、最不经常使用、最近最久未使用、故障转移、忙碌转移等;
14、分片广播任务:执行器集群部署时,任务路由策略选择”分片广播”情况下,一次任务调度将会广播触发集群中所有执行器执行一次任务,可根据分片参数开发分片任务;
15、动态分片:分片广播任务以执行器为维度进行分片,支持动态扩容执行器集群从而动态增加分片数量,协同进行业务处理;在进行大数据量业务操作时可显著提升任务处理能力和速度。
16、故障转移:任务路由策略选择”故障转移”情况下,如果执行器集群中某一台机器故障,将会自动Failover切换到一台正常的执行器发送调度请求。
17、任务进度监控:支持实时监控任务进度;
18、Rolling实时日志:支持在线查看调度结果,并且支持以Rolling方式实时查看执行器输出的完整的执行日志;
19、GLUE:提供Web IDE,支持在线开发任务逻辑代码,动态发布,实时编译生效,省略部署上线的过程。支持30个版本的历史版本回溯。
20、脚本任务:支持以GLUE模式开发和运行脚本任务,包括Shell、Python、NodeJS、PHP、PowerShell等类型脚本;
21、命令行任务:原生提供通用命令行任务Handler(Bean任务,”CommandJobHandler”);业务方只需要提供命令行即可;
22、任务依赖:支持配置子任务依赖,当父任务执行结束且执行成功后将会主动触发一次子任务的执行, 多个子任务用逗号分隔;
23、一致性:“调度中心”通过DB锁保证集群分布式调度的一致性, 一次任务调度只会触发一次执行;
24、自定义任务参数:支持在线配置调度任务入参,即时生效;
25、调度线程池:调度系统多线程触发调度运行,确保调度精确执行,不被堵塞;
26、数据加密:调度中心和执行器之间的通讯进行数据加密,提升调度信息安全性;
27、邮件报警:任务失败时支持邮件报警,支持配置多邮件地址群发报警邮件;
28、推送maven中央仓库: 将会把最新稳定版推送到maven中央仓库, 方便用户接入和使用;
29、运行报表:支持实时查看运行数据,如任务数量、调度次数、执行器数量等;以及调度报表,如调度日期分布图,调度成功分布图等;
30、全异步:任务调度流程全异步化设计实现,如异步调度、异步运行、异步回调等,有效对密集调度进行流量削峰,理论上支持任意时长任务的运行;
31、跨语言:调度中心与执行器提供语言无关的 RESTful API 服务,第三方任意语言可据此对接调度中心或者实现执行器。除此之外,还提供了 “多任务模式”和“httpJobHandler”等其他跨语言方案;
32、国际化:调度中心支持国际化设置,提供中文、英文两种可选语言,默认为中文;
33、容器化:提供官方docker镜像,并实时更新推送dockerhub,进一步实现产品开箱即用;
34、线程池隔离:调度线程池进行隔离拆分,慢任务自动降级进入”Slow”线程池,避免耗尽调度线程,提高系统稳定性;
35、用户管理:支持在线管理系统用户,存在管理员、普通用户两种角色;
36、权限控制:执行器维度进行权限控制,管理员拥有全量权限,普通用户需要分配执行器权限后才允许相关操作;

 

The core problem faced:

How can an xxl server be compatible with modules and versions that depend on different languages?

  • Solution 1: Add a new interface to the original project service, and call the api from the scheduled script.
    • The execution process cannot be collected in real time (the execution result can only be recovered from the return value)
    • Unable to manage the service in real time (for example, if the service is down, it will only be perceived when the interface is called next time)
  • Solution 2: shell xxx/xxx.js
    • Each service also needs to clone a copy of the project's code on the xxl server, and write shell commands at the same time.
    • Building a distributed system will become more cumbersome.

 

Main types of services provided:

  • GLUE mode (shell script execution, due to dependent package reasons, it is not recommended to use native code)
  • BEAN mode (api interface call)

 

Several types of scheduled tasks and access schemes:

  • Standard shell tasks
    • Access scheme: adopt GLUE mode, migrate task code
  • Refresh data scripts (for example, maintaining the on-shelf and off-shelf status of products based on the off-shelf time)
    • A single execution is faster, and the degree of attention to the execution process is not very high, just return the result after execution.
    • Access plan:
      • The response interface is written based on the project, and the original script logic is called by the interface, and the logic code does not need to be changed.
      • The Bean mode is adopted on the xxl platform, and the api is called directly.
      • The interface will return the execution result to xxl as the return value, and xxl will record the log.
  • health examination
    • Ditto.
  • Data preprocessing (such as big data algorithms, etc.)
    • The single execution time is relatively slow, up to tens of minutes, and the execution process is highly concerned.
    • It is necessary to deploy the code on the xxl server, and at the same time write a shell execution script. xxl uses the GLUE mode shell to call and collect the execution process logs.
  • DAG task
    • xxl has not yet provided a good DAG task processing solution. If DAG tasks are involved, it is necessary to create an additional task to achieve serialization, or use parent-child tasks.

 

Build:

configuration file  

File path: /xxl-job/xxl-job-admin/src/main/resources/application.properties

### 调度中心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

Note that the spring.mail.password here is not the QQ mailbox password, but the authorization code of the QQ mailbox     https://service.mail.qq.com/cgi-bin/help?subtype=1&id=28&no=1001256

Compile and run

Simply run the main method of the project directly to start.

Deployment on the server needs to be packaged into a jar package, and packaged in IDEA using the Maven plug-in.  Springboot method , and then upload to the server

start command

nohup  java -jar  xxl-job-admin-2.3.0-SNAPSHOT.jar &
nohup: ignoring input and appending output to ‘nohup.out’

Access:

  • BEAN access

    1. create task

    2. edit task

      Edit the task description, alarm email, set the operating mode to BEAN, and fill in the task parameter Handler

    3. Set cron timing time


  • GLUE access

    1. Add a sh file to the project to call the script

    2. Clone the code to the xxl server

    3. new task

      The page is basically the same as above, pay attention to the GLUE mode

    4. Edit IDE

      There is an IDE editor in the operation, and a new page will pop up when you click it. When writing a shell, pay attention to the cd path first, because the sh in the code finds the script in the root directory.

  • task management

    Task management can trigger an operation individually, as well as stop, start, edit, query logs, etc.

Executing once can specify the executor, if not executed, it will be selected by default. Click Save to trigger a task.

  • log management

You can also see the statistics summary on the home page:

  • Alarm email

There are two types of alarm emails. If the scheduler calls an error, the email format is as follows:

If an error is reported during task execution, the format is as follows:

Specific errors need to be checked in the log background.

Guess you like

Origin blog.csdn.net/sm9sun/article/details/113727916