文档

如何在SchedulerX 2.0平台上托管ElasticJob任务

更新时间:

分布式任务调度SchedulerX 2.0兼容开源ElasticJob任务接口,您无需修改代码,即可将ElasticJob任务在SchedulerX 2.0平台上进行托管。本文介绍如何在SchedulerX 2.0平台上托管ElasticJob任务。

背景信息

ElasticJob基于Quartz开发并且依赖ZooKeeper作为注册中心,是一款轻量级、无中心化的分布式任务调度框架,目前已经通过Apache开源。更多信息,请参见ElasticJob

SchedulerX 2.0优势

丰富的可视化能力

  • 查看用户大盘1

  • 查看任务历史记录2

  • 查看用户运行日志3

  • 查看任务运行堆栈4

  • 查看任务操作记录5

高级特性

  • 任务编排

    支持工作流(DAG)进行任务编排,可通过拖拽对前端进行操作。详细的任务状态图方便您了解下游任务执行失败的原因。6

  • 限流

    常见场景为夜间离线报表业务,例如,很多报表任务是凌晨1点或者2点开始,需控制应用最大并发的任务数量(否则业务无法支撑),达到并发上限的任务会在队列中等待。若同时要求早上9点前必须完成KPI报表,可以设置KPI任务高优先级,高优先级任务会抢占低优先级任务,从而实现优先调度。

    SchedulerX 2. 0支持可抢占的任务优先级队列,只需要在控制台进行配置即可。

    7

  • 资源隔离

    支持命名空间和应用级别资源隔离,支持多租户权限管理。

商业化报警运维

  • 报警

    支持通过邮件、钉钉、短信或电话进行任务执行失败、超时和无可用机器报警通知。

  • 运维操作

    支持原地程序运行、重刷数据、标记成功、查看堆栈、停止任务和指定机器等。

免运维、低成本

ElasticJob依赖ZooKeeper作为任务存储和任务调度协调,至少需要3个节点的ZooKeeper。如果有节点发生故障,需要重新配置ZooKeeper的服务端和客户端的配置,可能需要重启所有的应用。同时,受ZooKeeper功能限制,如果任务量级比较大,一个ZooKeeper集群将无法支撑住,且ZooKeeper无法水平扩展支持更大的TPS,就需要维护多个ZooKeeper集群,这样会增加机器成本。

通过SchedulerX 2.0托管ElasticJob任务,您无需自己维护ZooKeeper集群,也无需关注任务量级的增长,从而节省了机器和人力的维护成本。

高可用

SchedulerX 2.0采用高可用架构,任务多备份机制,经历阿里集团多年双十一、容灾演练,可以做到整个集群任意2个节点发生故障或者任意一个机房断电,任务调度都不会受到影响。

与开源ElasticJob的区别

对比项

开源ElasticJob

SchedulerX为底座的ElasticJob任务

Simple任务

对

对

Script任务

对

对

Dataflow任务

对

对

单机

对

对

分片广播

对

对

定时

cron

cron、fixed_rate、fixed_delay、one_time

工作流

错

对

可视化

历史记录、日志服务、运行堆栈、操作记录、用户大盘等

监控报警

邮件

邮件、钉钉群、短信、电话

运维操作

运行一次、原地重跑、重刷数据、标记成功、停止运行

Spring Boot接入方法

以ElasticJob-3.0.1版本为例。更多信息,请参见Demo工程

  1. pom.xml文件增加schedulerx2-plugin-elasticjob插件,删除elasticjob的依赖。

     <!-- 排掉elasticjob的依赖 -->
    <!--
     <dependency>
         <groupId>org.apache.shardingsphere.elasticjob</groupId>
         <artifactId>elasticjob-lite-spring-boot-starter</artifactId>
         <version>3.0.1</version>
    </dependency>
    -->
    <dependency>
        <groupId>com.aliyun.schedulerx</groupId>
        <artifactId>schedulerx2-plugin-elasticjob</artifactId>
        <version>3.0.1.3</version>
    </dependency>
  2. SchedulerX控制台创建您的命名空间和应用。具体操作,请参见创建命名空间应用管理

  3. application.yml移除elasticjob的配置,增加schedulerx的配置。

    #elasticjob:
    #  regCenter:
    #    serverLists: localhost:2181
    #    namespace: elasticjob-springboot-demo
    schedulerx: #需增加一个"schedulerx:",则以下任务会自动同步到schedulerx控制台
      jobs:
        simpleJob:
          elasticJobClass: com.alibaba.elasticjob.test.processor.SpringBootSimpleJob
          cron: 0/10 * * * * ?
          shardingTotalCount: 1
          overwrite: true
        shardingJob:
          elasticJobClass: com.alibaba.elasticjob.test.processor.SpringBootShardingJob
          cron: 0 * * * * ?
          shardingTotalCount: 3
          shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
          overwrite: true
    #增加以下schedulerx的配置。更多信息,请参见Spring Boot应用接入SchedulerX。 
    spring:
       schedulerx2:
          endpoint: acm.aliyun.com
          namespace: 433d8b23-xxxx-xxxx-xxxx-90d4d1b9a4af
          groupId: xueren_primary
          appKey: xxxxxxxxxxx
          regionId: public
          aliyunAccessKey: xxxxxxxxxxxx
          aliyunSecretKey: xxxxxxxxxxxx
  4. 启动程序增加自动扫描com.alibaba.schedulerx.plugin.*

    image..png

  5. 配置日志服务,搜集客户端的日志。使用log4j2配置log4j2.xml。具体操作,请参见阿里巴巴任务调度SchedulerX支持日志服务

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="off">
    	<Appenders>
    		<Console name="Console" target="SYSTEM_OUT">
    			<PatternLayout
    				pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n" />
    		</Console>
    		<SchedulerxLog4j2Appender name="schedulerxLog"
                timeFormat="yyyy-MM-dd'T'HH:mmZ"
                timeZone="UTC"
                ignoreExceptions="true">
            <PatternLayout pattern="%d %-5level [%thread] %logger{0}: %msg"/>
        </SchedulerxLog4j2Appender>
    	</Appenders>
    	<Loggers>
    		<Root level="info">
    			<AppenderRef ref="Console" />
    		</Root>
    		<Logger name="schedulerx" level="info" additivity="false">
    			<AppenderRef ref="schedulerxLog" />
    		</Logger>
    	</Loggers>
    </Configuration>

    Java执行类如下所示。

    image..png

  6. 启动程序后,SchedulerX控制台会自动同步任务。

    • 调度频率为秒级别,时间类型会更改为second_delay。

    • 分片个数大于1,执行方式为分片运行,否则为单机运行。

    image..png

    通过控制台,可以直接看到任务的运行日志

    63

  • 本页导读 (1)
文档反馈