Elastic job是当当网架构师张亮,曹昊和江树建基于Zookepper、Quartz开发并开源的一个Java分布式定时任务,解决了Quartz不支持分布式的弊端。Elastic job主要的功能有支持弹性扩容,通过Zookepper集中管理和监控job,支持失效转移等,这些都是Quartz等其他定时任务无法比拟的。

目前Elastic job的最新版本已经由原来的elastic-job-core分离除了两个项目,分别为Elastic-Job-Lite和Elastic-Job-Cloud。Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成,Elastic-Job-Lite定位为轻量级无中心化解决方案,使用jar包的形式提供分布式任务的协调服务。 Elastic-Job-Cloud使用Mesos + Docker(TBD)的解决方案,额外提供资源治理、应用分发以及进程隔离等服务,Elastic-Job-Lite和Elastic-Job-Cloud提供同一套API开发作业,开发者仅需一次开发,即可根据需要以Lite或Cloud的方式部署

开头copy 网上一段术语,讲解的很清晰。总结就是:elastic job  是用来实现 分布式的定时任务,比如说定时任务项目,部署在2台,但是只需要执行一次,而又想保持2台服务器代码都一致,这时候elasitc job 可以很完美的解决问题,当然使用的是zookeeper的相关特性。

1.实战

项目目录如下

1.pom.xml

新建maven项目,引入springboot的相关jar包

再引入elastic job 的相关核心包     elastic-job-core,elastic-job-spring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xj</groupId>
  <artifactId>el</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>el</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     
       <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     
     
    <!-- 引入elastic-job-core核心模块 -->  
<dependency>
   <groupId>com.dangdang</groupId>
   <artifactId>elastic-job-core</artifactId>
   <version>1.1.1</version>
</dependency>
         
<!-- 使用springframework自定义命名空间时引入 -->  
<dependency>
   <groupId>com.dangdang</groupId>
   <artifactId>elastic-job-spring</artifactId>
   <version>1.1.1</version>
</dependency>
  </dependencies>
   
   
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
   
   
   
</project>

2.reg.properties

存放zookeeper 配置中心的相关配置信息

1
2
3
4
5
serverLists=10.3.142.107:2181,10.3.142.107:2182,106.3.14.107:2183
namespace=elastic-job-example
baseSleepTimeMilliseconds=1000
maxSleepTimeMilliseconds=3000
maxRetries=3

3.withNamespace.xml

存放作业的相关详情,这里测试存放了 simple 与dataFlow 2中情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd 
                        http://www.dangdang.com/schema/ddframe/reg 
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd 
                        http://www.dangdang.com/schema/ddframe/job 
                        http://www.dangdang.com/schema/ddframe/job/job.xsd 
                        ">
     
    <context:property-placeholder location="classpath:conf/*.properties" /> 
      
    <!--配置作业注册中心 -->  
    <reg:zookeeper id="regCenter" server-lists="${serverLists}" namespace="${namespace}"  
                   base-sleep-time-milliseconds="${baseSleepTimeMilliseconds}" max-sleep-time-milliseconds="${baseSleepTimeMilliseconds}" max-retries="${maxRetries}" />  
   
    <!-- 配置作业-->  
    <job:simple id="mySimpleJob" class="com.xj.el.MySimpleJob" registry-center-ref="regCenter"  
                sharding-total-count="2" cron="0/3 * * * * ?" overwrite="true" />  
   
   
    
    <job:dataflow id="throughputDataFlow"  class="com.xj.el.MyThroughputDataFlowElasticJob" registry-center-ref="regCenter"
     cron="0/10 * * * * ?"  sharding-total-count="3"   sharding-item-parameters="0=A,1=B,2=C"
     process-count-interval-seconds= "10"  concurrent-data-process-thread-count="10"/>
   
</beans>

elastic-job提供了三种类型的作业:Simple类型作业、Dataflow类型作业、Script类型作业。这里主要讲解前两者。Script类型作业意为脚本类型作业,支持shell,python,perl等所有类型脚本,使用不多,可以参见github文档。

SimpleJob需要实现SimpleJob接口,意为简单实现,未经过任何封装,与quartz原生接口相似,比如示例代码中所使用的job。

Dataflow类型用于处理数据流,需实现DataflowJob接口。该接口提供2个方法可供覆盖,分别用于抓取(fetchData)和处理(processData)数据。
可通过DataflowJobConfiguration配置是否流式处理。
流式处理数据只有fetchData方法的返回值为null或集合长度为空时,作业才停止抓取,否则作业将一直运行下去; 非流式处理数据则只会在每次作业执行过程中执行一次fetchData方法和processData方法,随即完成本次作业。
实际开发中,Dataflow类型的job还是很有好用的。

4.MySimpleJob.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.xj.el;
import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.plugin.job.type.simple.AbstractSimpleElasticJob;
public class MySimpleJob extends AbstractSimpleElasticJob{
    @Override
    public void process(JobExecutionMultipleShardingContext shardingContext) {
          System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, 当前分片项: %s",  
         Thread.currentThread().getId(), shardingContext.getShardingTotalCount(), shardingContext.getShardingItems()));  
    }  
}

5.MyThroughputDataFlowElasticJob.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.xj.el;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.internal.job.AbstractJobExecutionShardingContext;
import com.dangdang.ddframe.job.plugin.job.type.dataflow.AbstractIndividualThroughputDataFlowElasticJob;
public class MyThroughputDataFlowElasticJob extends AbstractIndividualThroughputDataFlowElasticJob<TestBean>{
         
    public List<TestBean> fetchData(JobExecutionMultipleShardingContext shardingContext) {
        List<TestBean> testBeanList = new ArrayList();
         //获取testBean的相关数据
        return testBeanList;
    }
     
    public boolean processData(JobExecutionMultipleShardingContext shardingContext, TestBean data) {
        //处理上文testBean的相关数据
        return false;
    }
     
}

6.springboot 测试主类 SpringBootSampleApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.xj.el;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Hello world!
 *
 */
@RestController
@EnableAutoConfiguration
@ImportResource(locations = {"classpath:withNamespace.xml"})
public class SpringBootSampleApplication 
{
     public static void main(String[] args) {
            SpringApplication.run(SpringBootSampleApplication.class, args);
        }
      
      
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
}

本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1972498,如需转载请自行联系原作者

Elastic Job 入门相关推荐

  1. Elastic Job 入门教程(三)— 作业监听

    接上一篇:Elastic Job 入门教程(二)- Spring Boot框架下是实现Elastic Job 脚本作业(Script Job),本章我们讨论作业Job的监听. 定义监听器 @Compo ...

  2. Elastic search入门到集群实战操作详解(原生API操作、springboot整合操作)-step1

    Elastic search入门到集群实战操作详解(原生API操作.springboot整合操作)-step2 https://blog.csdn.net/qq_45441466/article/de ...

  3. 第一章 Elastic Stack入门

    一.预备知识Restful 1.起源 在没有前后端分离概念之前,一个网站的完成总是"all in one",在这个阶段,页面.数据.渲染全部在服务端完成,这样做的最大的弊端是后期维 ...

  4. java使用elastic search入门

    转自:https://www.ibm.com/developerworks/cn/java/j-use-elasticsearch-java-apps/ 如果您使用过 Apache Lucene 或 ...

  5. Elastic Search入门:架构说明及Docker方式体验

    Elastic Search简称ES,是一款在搜索和日志检索领域极其成功的开源产品,当然ES背后的商业化公司也很成功.很多大的知名品牌,像是SAP.Booking.Ebay都在使用ES, 今天就来实际 ...

  6. Elastic Job 入门详解

    Elastic job是当当网架构师张亮,曹昊和江树建基于Zookepper.Quartz开发并开源的一个Java分布式定时任务,解决了Quartz不支持分布式的弊端.Elastic job主要的功能 ...

  7. Elastic Job入门示例

    Elastic Job官网:http://elasticjob.io/index_zh.html 示例:Spring Boot + Elastic Job 实现一个simple任务类型 1.maven ...

  8. aws elastic beanstalk入门之简介

    文档:What is AWS Elastic Beanstalk? - AWS Elastic Beanstalk 1.简介 Elastic Beanstalk 可以在 AWS 云中快速部署和管理应用 ...

  9. Elastic Job入门(1) - 简介

    介绍 构建一般的业务系统来说,使用Quartz或者Spring Task即可基本满足我们的单体服用应用需要.然而随着线上业务量的不断发展,这两种定时任务已经日渐无法满足我们的需求.一般,使用这两种定时 ...

  10. Elastic search 入门

    摘要:Elasticsearch是一个实时分布式搜索和分析引擎.它让你以前所未有的速度处理大数据成为可能.它用于全文搜索.结构化搜索.分析以及将这三者混合使用:维基百科使用Elasticsearch提 ...

最新文章

  1. javascript 盒子模型
  2. 百度顶会论文复现(3):视频分类综述
  3. git stash 缓存 简介
  4. 配置修改Nginx支持 PATHINFO
  5. Android中OnItemClick的四个参数意义
  6. 数据结构 | 链表队列(基本操作及图示)
  7. 基于JAVA+Servlet+JSP+MYSQL的物流管理系统
  8. 计算机基础知识刷题app,计算机考试刷题app
  9. 在python中安装包出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))
  10. Image Segmentation
  11. 单片机笔记(江科大自化协)
  12. [Leetcode][分治法]相关题目汇总/分析/总结
  13. c语言猴子吃桃问题(简洁做法)
  14. java毕业设计员工绩效考核系统分析与设计Mybatis+系统+数据库+调试部署
  15. 排名前6位的最流行的大数据框架,你在用哪一款?
  16. 年会弹幕文字_微信弹幕_微信墙/弹幕抽奖/晚会年会必备互动
  17. 技嘉服务器主板装系统,技嘉AB350M-DS3H主板u盘重装系统win7教程
  18. django多对多展示
  19. ASP.NET中的配置文件
  20. 卵形曲线坐标计算方法

热门文章

  1. js动态修改onclick的响应函数后,IE无效的解决方案
  2. SubSnoic 框架入门到提高(4)---全程记录
  3. 使用 Lightbox 2 和 JavaScript 构建出色的图片库
  4. 计量经济学计算机实验报告,综合实训报告范文
  5. static,inline,volatile的作用
  6. h264编解码器知识点
  7. cgroup学习(三)——伪文件
  8. Linux-2.6.21的负载均衡
  9. 数组中只出现一次的数
  10. 【分层图最短路】通信线路