Java分佈式定時任務--Elastic-job實踐

1,什麼是Elastic-job
Elastic-job 是一個分佈式調度解決方案。
Elastic-Job由2個獨立的子項目組成:Elastic-Job-Lite和Elastic-Job-Cloud。
Elastic-Job-Lite是一個無中心的解決方案,使用輕量級jar來協調分佈式作業。

Elastic-Job-Lite和Elastic-Job-Cloud提供統一的API。開發人員只需要一次代碼,然後決定根據需要部署Lite或Cloud。
2,特徵
分佈式計劃作業座標;
支持彈性縮放和縮小;
故障轉移;
Web控制檯;
Spring的整合和命名空間的支持;
分佈式環境不穩定時自我診斷和恢復;
支持並行調度;

2,使用
第一步:pox.xml文件中引入

      <dependency>
        <groupId>com.dangdang</groupId>
        <artifactId>elastic-job-lite-core</artifactId>
        <version>2.1.5</version>
      </dependency>
      <dependency>
        <groupId>com.dangdang</groupId>
        <artifactId>elastic-job-lite-spring</artifactId>
        <version>2.1.5</version>
      </dependency>

第二步:Java代碼的編寫

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

/**
  @Auth :  zhangna
*/
public class MyElasticJob implements SimpleJob {
     @Override
     public void execute(ShardingContext shardingContext) {
     
           you can do something........
           
     }
}

第三步:作業的配置

<?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: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.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
                    ">
<!--配置註冊中心 -->
<reg:zookeeper id="regCenter" server-lists="127.0.0.1:2181" namespace="dd-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />

<!-- 配置任務-->
<job:simple id="myElasticJob" class="com.zhangna.job.myElasticJob" registry-center-ref="regCenter" cron="0 0/1 * * * ?" sharding-total-count="1" overwrite="true" />
</beans> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章