2 Star 2 Fork 5

吃竹子的程序熊 / elastic-job-spring-boot-starter

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

整合 SPRING BOOT ELASTIC-JOB,编写自己的elastic-job-spring-boot-starter

依赖版本:

spring boot 全家桶版本:2.0.1.RELEASE

elastic-job版本:2.1.5

使用说明

启用Elastic-job

在spring boot 项目中可以在项目启动类上添加@EnableElasticJob注解来开启Elastic-job功能。

@EnableElasticJob有几个参数:

  • enabled 参数表示是否开启Elastic-job功能,默认为true
  • basePackages 参数可以接收一个字符串数组,里面是想要扫描的ElasticJob作业实例所在的包名。
  • basePackageClasses 参数接收一个Class数组,数组中的class对应的包名将会被添加到basePackages中。
  • smartLoad参数本质上是用来禁用注解扫描功能,采用路径匹配的模式,默认为false, 暂时未实现对应的功能,对于任务配置依然需要对应的注解。
  • loadAnnotation表示扫描的注解类,默认为@ElasticJobMeteData,无需修改。

配置Elstic job作业

  • 在包cn.jpanda.elastic.job.autoconfigure.annotations.jobs下提供三个目前Elasticjob 支持的作业类型对应的注解。
  • @SimpleElasticJob
  • @ScriptElasticJob
  • @DataFlowElasticJob 分别对应着简单作业,脚本作业和流式作业。 注解中含有的参数对应着ElasticJob作业的核心配置项。
  • jobName 表示作业名称,如果为空,则使用作业实现类的全限定名称作为任务名称。
  • cron 表示cron表达式,用于控制任务触发时间
  • shardingTotalCount 表示作业分片总数
  • shardingItemParameters 表示分片参数
  • jobParameter 表示作业自定义参数
  • failover 表示是否开启任务执行失效转移,默认为false
  • misfire 表示是否开启错过任务重新执行
  • description 表示作业描述信息
  • jobShardingStrategyClass 表示作业分片策略实现类的全限定名称
  • reconcileIntervalMinutes 表示修复作业服务器不一致状态服务调度间隔时间
  • maxTimeDiffSeconds 表示最大允许的本机与注册中心的时间误差秒数
  • disabled
  • overwrite

除此之外,对于脚本型作业和流式作业还有特有的配置项:

@ScriptElasticJob

  • scriptCommandLine biaoshi 脚本型作业执行命令行

@DataFlowElasticJob

  • streamingProcess 表示是否流式处理数据

对于一个简单的作业来说,只需要使用上述的@***ElasticJob注解来进行配置就可以了。

事件追踪

如果需要开启作业的事件追踪功能,可以使用@JobEvent注解,@JobEvent注解有两个参数。

  • enabled 表示是否开启事件追踪功能,默认为true,
  • eventHandlerClass 表示处理事件追踪的处理类,默认为JobEventRdbConfiguration类。 所以如果开启了事件追踪功能,往往也就需要配置一个JobEventRdbConfiguration实例。

定制作业线程处理器和异常处理器

如果需要定制作业的线程处理器和异常处理器可以使用@JobHandler注解来设定对应的值。 @JobHandler注解有两个参数:

  • executorServiceHandlers表示线程处理器,默认为DefaultExecutorServiceHandler。
  • exceptionHandlers 表示异常处理器,默认为DefaultJobExceptionHandler。

开启作业监听器

如果需要在业务中开启事件监听器可以使用@JobListener注解,@JobListener注解有三个参数:

  • listenClass表示监听器集合,需实现ElasticJobListener接口集合。
  • startedTimeoutMilliseconds 表示开始超时时间
  • completedTimeoutMilliseconds 表示结束超时时间

对于普通的ElasticJobListener实现来说可以不用理会startedTimeoutMilliseconds和completedTimeoutMilliseconds参数。

对于AbstractDistributeOnceElasticJobListener来说,则需要配置这两个参数,

框架会根据监听器实现的接口来判断监听器的类型。

开启作业的监控配置

如果需要开启作业的监控配置,可以使用注解@JobMonitor来实现:

  • monitorExecution 表示监控作业运行时状态
  • monitorPort 表示作业监听端口

指定作业对应的注册中心

如果需要指定作业使用的注册中心,可以使用注解@RegistryCenter来实现。

  • registerCenter 表示作业使用的注册中心,默认为ZookeeperRegistryCenter。

扩展

目前Elastic Job只有script,simple和dataflow三种类型的作业。 如果需要扩展新类型的任务,或者定制任务注解的解析,可以新增一个注解类,并使用@ElasticJobMeteData注解来标注这个新注解。 同时,新增一个类实现JobParseAnnotationHandlerStrategy接口的方法,或者继承AbstractJobParseAnnotationHandlerStrategy,并根据自己的需要重写或实现相应的方法。并在实现类上添加注解@JobParser. 同时将@JobParser的value设为新增的类型。

涉及文档

1.通过官方文档和JobSchedule类,归纳所需要的公共配置.

从JobScheduler类入手,辅以官方文档,归纳定义任务时所需要的所有配置。

elasticjob文档

首先从JobScheduler类入手,找到最复杂的构造方法,该构造方法涉及到的参数为:

  • CoordinatorRegistryCenter(用于协调分布式服务的注册中心)
  • LiteJobConfiguration(Lite作业配置)
  • JobEventBus(运行痕迹时间总线)
  • ElasticJobListener(弹性化分布式作业监听器)

CoordinatorRegisterCenter

CoordinatorRegistryCenter目前实现类是ZookeeperRegistryCenter,其中包含的配置为ZookeeperConfiguration,对于注册中心来讲通常是全局唯一的,所以我们可能需要一个Zookeeper注册中心配置类(ZookeeperProperties),这个配置类应该包含了ZookeeperConfiguration中的所有可定制的参数。 image

LiteJobConfiguration

LiteJobConfiguration用来配置具体的Lite作业,这里面主要涉及了三个类。

  • JobCoreConfiguration
  • JobTypeConfiguration
  • LiteJobConfiguration

image

JobCoreConfiguration是一个作业的核心配置,他被JobTypeConfiguration引用,来填充作业的基础的、核心的配置内容。

JobTypeConfiguration在持有了JobCoreConfiguration的同时,还包含了针对不同类型的作业的配置,可用于进一步划分不同的作业。

最后,LiteJobConfiguration集成了JobTypeConfiguration的内容,并提供了额外的配置,包括监控,分片策略等内容

JobCoreConfiguration

因为在Elastic-job中,JobCoreConfiguration作为最底层的构造单元之一,将作用在每一个作业上,在不需要特殊定制JobCoreConfiguration的场景中,定义一个全局的JobCoreConfiguration可以大大减少工作量,所以JobCoreConfiguration可以考虑成为一个全局的默认配置项出现。

同时,在具体的作业中,不同的作业,可能会对JobCoreConfiguration进行一些定制化的操作,这也要要求JobCoreConfiguration能够单独配置。

因此,对于JobCoreConfiguration在提供全局配置的同时,还要提供定制性的配置方案,因此可以考虑默认全局配置,然后定制配置覆盖默认配置的方式来实现。

  • image

JobTypeConfiguration

之后,对于稍上一层的JobTypeConfiguration来说,他的作用除了持有JobCoreconfiguration之外,就是对作业类型的分装和作业实现类的封装。 在JobTypeConfiguration中将任务划分为三种:

  • SimpleJob(简单任务)
  • DataflowJob(流式任务)
  • ScriptJob(脚本任务) image

三种不同的任务类型,也对应着其各自不同的、特有的任务定义。

  • image

鉴于此,对于JobTypeConfiguration来讲,需要针对这三种任务来进行其单独的配置,因为这一配置需要和具体的作业相关联,所以考虑考虑将其作为注解的形式来实现。

LiteJobConfiguration

最后,在JobCoreConfiguration和JobTypeConfiguration配置完成之后,就又回到了LiteJobConfiguration中,在LiteJobConfiguration中,除了上述的两类配置之外,还提供了关于监控、分片策略的配置。 image

到这为止,关于作业配置的方面,总结一下我们需要:

  • JobCoreConfiguration全局配置方案
  • JobCoreConfiguration局部定制配置方案
  • SimpleJob,DataFlowJob,ScriptJob独立的配置方案。
  • 监控/分片策略配置方案.

关于具体的配置属性,可以在com.dangdang:elastic-job-lite-spring:2.1.5中的META-INF/namespace中的job.xsd和reg.xsd文件中查看。

JobEventBus

在分析了LiteJobConfiguration之后,继续拆分JobSheduler的构造参数——JobEventBus。

JobEventBus提供了Elastic-job的事件追踪功能,他主要引用了一个叫做JobEventConfiguration的作业事件配置接口,目前该接口的实现可以算是只有JobEventRdbConfiguration类,在JobEventRdbConfiguration类中引用了DataSource数据源,仅此而已。

鉴于此,我们可以考虑采用项目默认的数据源,但是也要考虑采用独有的数据源。 因此,也要尽可能的为DataSource提供定制的支持。

ElasticJobListener

最后,只剩下ElasticJobListener参数,ElasticJobListener是弹性化分布式作业监听器接口定义,目前在Elastic-job中提供了两种监听器的配置:

  • 分布式listener配置(全局)
  • 单节点listener配置(唯一) 对于普通的分布式作业监听器来说,只要实现了ElasticJobListener即可,但是对于在分布式作业中只执行一次的监听器,也就是单节点监听配置,则要求集成AbstractDistributeOnceElasticJobListener类.

这两种监听器在配置上来看,主要是构造参数的不同,因此可以考虑定义为两个不同的注解.

2.拆分配置,分为注解和配置文件

截止到这,对于Elastic-job的配置部分已经整理的差不多了,接下来就是对上述涉及到的配置进行汇总分类。

全局配置

首先是全局配置,对于Zookeeper注册中心的配置毋庸置疑是需要提供全局配置的。

之后是JobCoreConfiguration,在考虑到不需要为每个作业都单独定制的场景时,其也需要提供全局配置的方案,但是现实很骨感,对于JobCoreConfiguration来说,能复用的也就只有JobProperties参数,其他的均和具体的作业有强关联。

而LiteJobConfiguration中的监控和分片策略大概率下复用的可能性比较高,所以需要提供默认的实现。

最后是JobEventBus和ElasticJobListener,事件追踪和任务监听,可以独立存在,也可以不存在,其必要性相对比较弱,可以考虑不提供全局默认配置。 不过,需要考虑为JobEventBus提供DataSource的全局配置以及定义方案。

因此,以下部分需要提供全局配置方案:

  • ZookeeperProperties(zookeeper配置中心)
  • DataSourceProperties(JobEventBus的Datasource)
  • MonitorProperites(LiteJobConfiguration的监控配置)
  • ShardingStrategyProperties(LiteJobConfiguration的分片策略配置)

单独配置

关于单独配置,首先JobTypeConfiguration对应的三类任务,必然是需要提供单独配置的, 同时事件追中,以及监听也都需要提供单独的配置,而JobCoreConfiguration同样要为定制性的场景考虑单独配置。

针对独有的配置SimpleJob,DataflowJob,ScriptJob三个配置,他和具体的job一一对应,可以考虑作为注解来存在。

  • @SimpleJob(简单任务配置)
  • @DataflowJob(流式任务配置)
  • @ScriptJob(脚本任务配置)

对于JobCoreConfiguration来说,可以提供单独的注解,不配置则采用默认注解,在配置的场景下则采用对应的配置。

  • @JobCore - 作业核心配置

最后再针对任务监控,分片策略,事件追踪分别提供定制性的注解

  • @JobMonitor(任务监控)
  • @JobShardingStrategy(分片策略)
  • @JobEvent(事件追踪)

和spring cloud整合

在spring cloud Finchley*版本中引用了curator4.0+版本的jar包,但是在elastic-job使用的是2.10.0版本的jar包,两者不兼容,所以整合时需要重新定义一下curator系列的版本jar包。

    <com.dodomall.elastic-job-spring-boot-starter>1.0-SNAPSHOT</com.dodomall.elastic-job-spring-boot-starter>
    <org.apache.curator.curator.client>2.10.0</org.apache.curator.curator.client>
    <org.apache.curator.curator.framework>2.10.0</org.apache.curator.curator.framework>
    <org.apache.curator.curator.recipes>2.10.0</org.apache.curator.curator.recipes>
<dependencyManagement>
    <dependencies>
        <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>${org.apache.curator.curator.client}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${org.apache.curator.curator.framework}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>${org.apache.curator.curator.recipes}</version>
            </dependency>
        </dependencies>
</dependencyManagement>
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: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and 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 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 2018 吃竹子的程序熊 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.

简介

整合SpringBoot2.0和ElasticJob 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/topanda/elastic-job-spring-boot-starter.git
git@gitee.com:topanda/elastic-job-spring-boot-starter.git
topanda
elastic-job-spring-boot-starter
elastic-job-spring-boot-starter
master

搜索帮助