ElasticJob入門

 Elastic job是當(dāng)當(dāng)網(wǎng)架構(gòu)師張亮,曹昊和江樹建基于Zookepper、Quartz開發(fā)并開源的一個(gè)Java分布式定時(shí)任務(wù),解決了Quartz不支持分布式的弊端。Elastic job主要的功能有支持彈性擴(kuò)容,通過Zookepper集中管理和監(jiān)控job,支持失效轉(zhuǎn)移等,這些都是Quartz等其他定時(shí)任務(wù)無法比擬的。

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的民樂網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

    目前Elastic job的最新版本已經(jīng)由原來的elastic-job-core分離除了兩個(gè)項(xiàng)目,分別為Elastic-Job-Lite和Elastic-Job-Cloud。Elastic-Job是一個(gè)分布式調(diào)度解決方案,由兩個(gè)相互獨(dú)立的子項(xiàng)目Elastic-Job-Lite和Elastic-Job-Cloud組成,Elastic-Job-Lite定位為輕量級(jí)無中心化解決方案,使用jar包的形式提供分布式任務(wù)的協(xié)調(diào)服務(wù)。 Elastic-Job-Cloud使用Mesos + Docker(TBD)的解決方案,額外提供資源治理、應(yīng)用分發(fā)以及進(jìn)程隔離等服務(wù),Elastic-Job-Lite和Elastic-Job-Cloud提供同一套API開發(fā)作業(yè),開發(fā)者僅需一次開發(fā),即可根據(jù)需要以Lite或Cloud的方式部署 

開頭copy 網(wǎng)上一段術(shù)語,講解的很清晰??偨Y(jié)就是:elastic job  是用來實(shí)現(xiàn) 分布式的定時(shí)任務(wù),比如說定時(shí)任務(wù)項(xiàng)目,部署在2臺(tái),但是只需要執(zhí)行一次,而又想保持2臺(tái)服務(wù)器代碼都一致,這時(shí)候elasitc job 可以很完美的解決問題,當(dāng)然使用的是zookeeper的相關(guān)特性。

1.實(shí)戰(zhàn)

項(xiàng)目目錄如下

Elastic Job 入門

1.pom.xml

新建maven項(xiàng)目,引入springboot的相關(guān)jar包

再引入elastic job 的相關(guān)核心包     elastic-job-core,elastic-job-spring

<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自定義命名空間時(shí)引入 -->  
<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 配置中心的相關(guān)配置信息

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

存放作業(yè)的相關(guān)詳情,這里測(cè)試存放了 simple 與dataFlow 2中情況。

<?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" /> 
     
    <!--配置作業(yè)注冊(cè)中心 -->  
    <reg:zookeeper id="regCenter" server-lists="${serverLists}" namespace="${namespace}"  
                   base-sleep-time-milliseconds="${baseSleepTimeMilliseconds}" max-sleep-time-milliseconds="${baseSleepTimeMilliseconds}" max-retries="${maxRetries}" />  
  
    <!-- 配置作業(yè)-->  
    <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提供了三種類型的作業(yè):Simple類型作業(yè)、Dataflow類型作業(yè)、Script類型作業(yè)。這里主要講解前兩者。Script類型作業(yè)意為腳本類型作業(yè),支持shell,python,perl等所有類型腳本,使用不多,可以參見github文檔。

SimpleJob需要實(shí)現(xiàn)SimpleJob接口,意為簡(jiǎn)單實(shí)現(xiàn),未經(jīng)過任何封裝,與quartz原生接口相似,比如示例代碼中所使用的job。

Dataflow類型用于處理數(shù)據(jù)流,需實(shí)現(xiàn)DataflowJob接口。該接口提供2個(gè)方法可供覆蓋,分別用于抓取(fetchData)和處理(processData)數(shù)據(jù)。
可通過DataflowJobConfiguration配置是否流式處理。
流式處理數(shù)據(jù)只有fetchData方法的返回值為null或集合長(zhǎng)度為空時(shí),作業(yè)才停止抓取,否則作業(yè)將一直運(yùn)行下去; 非流式處理數(shù)據(jù)則只會(huì)在每次作業(yè)執(zhí)行過程中執(zhí)行一次fetchData方法和processData方法,隨即完成本次作業(yè)。
實(shí)際開發(fā)中,Dataflow類型的job還是很有好用的。

4.MySimpleJob.java

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, 任務(wù)總片數(shù): %s, 當(dāng)前分片項(xiàng): %s",  
		 Thread.currentThread().getId(), shardingContext.getShardingTotalCount(), shardingContext.getShardingItems()));  
	}	

}

5.MyThroughputDataFlowElasticJob.java

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的相關(guān)數(shù)據(jù)
		return testBeanList;
	}

	
	public boolean processData(JobExecutionMultipleShardingContext shardingContext, TestBean data) {
		//處理上文testBean的相關(guān)數(shù)據(jù)
		return false;
	}
	
}

6.springboot 測(cè)試主類 SpringBootSampleApplication.java

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!";
    }
}



本文標(biāo)題:ElasticJob入門
當(dāng)前URL:http://www.muchs.cn/article20/pdhgco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、商城網(wǎng)站Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管