1 Star 0 Fork 73

lvhaiyan888 / disjob

forked from dromara / disjob 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Blog License JDK Build status Maven Central

简体中文 | English

Disjob

Introduction

一个分布式的任务调度框架,除了具备常规的分布式任务调度功能外,还提供:暂停/取消运行中的任务、恢复执行被暂停的任务、任务拆分、失败重试、广播任务、任务依赖、工作流任务(DAG)、管理器与执行器分离部署、Web管理后台等能力。

轻量级,简单易用,特别适合长任务的执行。有较好的伸缩性、扩展性、稳定性,历经生产检验。

Architecture

  • 架构图

Architecture

  • 工程结构
disjob                                                    # 主项目
├── disjob-admin                                          # 基于Ruoyi框架二次开发的Disjob管理后台
├── disjob-bom                                            # Maven project bom module
├── disjob-common                                         # 工具包
├── disjob-core                                           # 任务调度相关的核心类(如数据模型、枚举类、抽象层接口等)
├── disjob-dispatch                                       # 任务分发模块
│   ├── disjob-dispatch-api                               # 任务分发的抽象接口层
│   ├── disjob-dispatch-http                              # 任务分发的Http实现
│   └── disjob-dispatch-redis                             # 任务分发的Redis实现
├── disjob-id                                             # 分布式ID生成
├── disjob-registry                                       # Server(Supervisor & Worker)注册模块
│   ├── disjob-registry-api                               # Server注册的抽象接口层
│   ├── disjob-registry-consul                            # Server注册的Consul实现
│   ├── disjob-registry-etcd                              # Server注册的Etcd实现
│   ├── disjob-registry-nacos                             # Server注册的Nacos实现
│   ├── disjob-registry-redis                             # Server注册的Redis实现
│   └── disjob-registry-zookeeper                         # Server注册的Zookeeper实现
├── disjob-reports                                        # 聚合各个模块的测试覆盖率报告
├── disjob-samples                                        # Samples项目
│   ├── disjob-samples-common                             # 存放使用范例中用到的公共代码,包括使用到的一些公共配置文件等
│   ├── disjob-samples-merged-springboot                  # Supervisor与Worker合并部署的范例(Spring boot应用)
│   ├── disjob-samples-supervisor-springboot              # Supervisor单独部署的范例(Spring boot应用)
│   ├── disjob-samples-worker-frameless                   # Worker单独部署的范例(非Spring-boot应用,直接main方法启动)
│   └── disjob-samples-worker-springboot                  # Worker单独部署的范例(Spring boot应用)
├── disjob-supervisor                                     # Supervisor代码(Spring-boot应用)
├── disjob-test                                           # 用于辅助测试
└── disjob-worker                                         # Worker代码

Features

  • 分为管理器(Supervisor)和执行器(Worker)两种角色,Supervisor与Worker可分离部署
  • Supervisor与Worker通过注册中心相互发现,支持的注册中心有:Redis、Consul、Nacos、Zookeeper、Etcd
  • Supervisor负责生成任务,把任务分发给Worker执行,支持的任务分发方式有:Redis、Http
  • 需要指定Job的分组(job-group),Job的任务只会分发给指定组的Worker执行
  • 提供拆分任务的能力,重写拆分方法JobHandler#split即可拆分为多个任务,实现分布式任务及并行执行
  • 支持暂停和取消运行中的任务,已暂停的任务可恢复继续执行,执行失败的任务支持重试
  • 支持任务保存(checkpoint)其执行状态,让手动或异常暂停的任务能从上一次的执行状态中恢复继续执行
  • 任务在执行时若抛出PauseTaskException,会暂停对应实例下的全部任务(包括分布在不同worker机器中的任务)
  • 支持广播任务,广播任务会分发给job-group下的所有worker执行
  • 支持Job间的依赖,多个Job配置好依赖关系后便会按既定的依赖顺序依次执行
  • 支持DAG工作流,可把jobHandler配置为复杂的DAG表达式,如:A->B,C,(D->E)->D,F->G
  • 提供Web管理后台,通过界面进行作业配置,任务监控等

Download From Maven Central

<dependency>
  <groupId>cn.ponfee</groupId>
  <artifactId>disjob-{xxx}</artifactId>
  <version>2.0.2</version>
</dependency>

Build From Source

./mvnw clean install -DskipTests -Dcheckstyle.skip=true -Dmaven.javadoc.skip=true -U

Quick Start

  1. 管理后台演示地址:http://ponfee.cn:8000/ ,用户名/密码:disjob/disjob123

  2. IDE分别导入项目(分为三个独立的项目,共用一个git仓库)

  1. 运行仓库提供的两个SQL脚本:mysql-disjob.sqlmysql-disjob_admin.sql创建两个数据库表(也可直接运行内置Mysql,启动时会自动初始化两个SQL脚本)
  1. pom文件中选择注册中心及任务分发的具体实现(默认redis注册中心、http任务分发)
  1. 修改配置文件
  1. 启动samples项目下的各应用,包括
@EnableSupervisor
@EnableWorker
public class MergedApplication extends AbstractSamplesApplication {
  public static void main(String[] args) {
    SpringApplication.run(MergedApplication.class, args);
  }
}
  1. 修改管理后台disjob-admin的配置
  1. 启动disjob-admin
  • 启动java类
  • 启动成功后浏览器访问 http://127.0.0.1:80/ 进入管理后台(用户名密码:admin/admin123)
  • 登录后在左侧菜单栏找到调度管理菜单,即可使用后台管理功能
    • 调度配置:查看、新增、修改、删除、触发、禁用等
    • 调度实例:具体时间点的运行实例,一个实例拆分成多个task。(第一个分页表格为查询root实例并支持下钻,鼠标向下滚动页面后看到的第二个分页表格为查询所有实例)

💡提示:若使用内置的mysql、redis,以上所有配置都无需修改即可启动各应用

Example

举个简单的例子:统计在(0,1万亿]区间内质数的个数。如果单机单线程CPU去统计的话不知道要到何年马月,这里我们就可以使用Disjob框架提供的分布式并行执行的能力来解决该类问题。

  1. 拆分任务

先根据当前的机器资源情况来决定拆分任务的数量,比如我们有5台机器及每台2 core CPU(质数统计是CPU密集型),决定拆分为10个任务。

  1. 分发任务

总任务被拆分成10个子任务后,框架会使用指定的路由算法把子任务分发给这些机器(Worker)。

  1. 接收任务

机器(Worker)接收到子任务后,会提交到框架的自定义线程池中执行。

  1. 分布式并行执行

在执行时我们可以使用分批次方式(通过代码循环)来统计,这里我们指定task-1在第一次循环统计(0, 1亿],第二次循环统计(10亿, 11亿],以此类推最后一次循环统计(9990亿, 9991亿]。同理其它的task也是按同样的方式分布式并行统计。

P.s. 黎曼猜想中可知质数分布是大体均匀的,判断一个数是否质数有很多方法,如埃氏筛法、欧拉筛法、Miller Rabin素性检验,我们可以使用Guava库提供的素性检验。

  1. Checkpoint

如果在统计过程中机器宕机后怎么办?难道再从头开始统计吗?No No No!我们可以在每循环10次(或每隔执行超过1分钟)时使用Checkpoint保存当前task-1的执行快照。宕机异常后的重新启动任务时会读取这份快照数据,从上一次的状态中接着继续统计。以下是task-1任务保存的快照数据样例

{
  "next": 4000000001, // 下一次循环时要统计的区间为(40亿, 41亿]
  "count": 19819734,  // 已经统计到了 19819734 个质数
  "finished": false   // 当前任务是否已经统计完成:true-是;false-否;
}
  1. 暂停与恢复

假如我们的这几台机器资源需要临时做其它的事情,想把当前的统计任务暂停一段时间。No problem!框架是支持暂停执行中的任务,只需要在管理后台的调度实例页面,找到该任务点击暂停按钮即可。在暂停时任务会接收到一个中断信号,收到中断信号时同样可以在代码中使用Checkpoint保存当前的执行快照。

当其它事情处理完后,我们可以在管理后台的调度实例页面,找到被暂停的这个任务,点击恢复按钮,此时任务会从上一次保存的状态中恢复继续执行。

  1. 自定义异常

当某个子任务在执行过程中抛出框架自定义的PauseTaskException则会暂停全部的10个子任务(包括分发在不同机器中的任务),同理如果抛出CancelTaskException则会取消全部的10个子任务。如果抛出其它的异常时,只会取消当前子任务,其它的9个子任务不受影响。

  1. 任务编排

现在这个质数统计的总任务已经执行完了,共10个子任务,每个子任务都统计出了它的那部分结果。Disjob能自动帮我汇总结果吗?Yes!框架提供了非常强大且方便的表达式来编排任务,如:A->B,C,(D->E)->D,F->G,现在我们可以创建一个汇总任务,然后再把这两个任务编排在一起。以下是本例中质数统计的job数据,其中job_handler指定了编排的这两个任务处理器(代码在项目源码中)

INSERT INTO `sched_job` (
  `job_id`,
  `job_group`,
  `job_name`,
  `job_handler`,
  `job_state`,
  `job_type`,
  `job_param`,
  `trigger_type`,
  `trigger_value`,
  `next_trigger_time`
) VALUES (
  1003164910267351009,
  'default',
  'prime-count-dag',
  'cn.ponfee.disjob.test.handler.PrimeCountJobHandler -> cn.ponfee.disjob.test.handler.PrimeAccumulateJobHandler',
  1,
  2,
  '{\"m\":1,\"n\":10000000000,\"blockSize\":100000000,\"parallel\":10}',
  2,
  '2023-09-02 18:00:00',
  unix_timestamp()*1000
);

本例中的质数统计流程图如下

范例流程图

Contributing

如有发现bug、更优的实现方案、新特性等,可提交PR或新建Issues

Todo List

  • 扩展注册中心:Zookeeper、Etcd、Nacos
  • 工作流任务(Workflow DAG)
  • 管理后台、账户体系及权限控制
  • 搭建一个关于项目使用说明的文档站点
  • 在线查看任务实时运行日志
  • 告警订阅:邮件、短信、电话、飞书、钉钉、微信
  • 可视化监控BI(Dashboard)
  • 增加多种Checkpoint的支持:File System、Hadoop、RocksDB
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright (c) 2017-2023 Ponfee Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Disjob是一个分布式的任务调度框架 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/lvhaiyan/disjob.git
git@gitee.com:lvhaiyan/disjob.git
lvhaiyan
disjob
disjob
master

搜索帮助