1. 程式人生 > >快速入門Elastic-Job

快速入門Elastic-Job

0.前言

  本案例基於springboot 1.5.9

1.maven依賴

 1 <dependency>
 2    <groupId>com.dangdang</groupId>
 3    <artifactId>elastic-job-lite-core</artifactId>
 4    <version>2.1.5</version>
 5 </dependency>
 7 <dependency>
 8    <groupId>org.apache.curator</groupId>
 9
<artifactId>curator-client</artifactId> 10 <version>2.11.1</version> 11 </dependency>

2.配置類

 
  
  
 1 import com.dangdang.ddframe.job.config.JobCoreConfiguration;
 2 import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
 3 import com.dangdang.ddframe.job.lite.api.JobScheduler;
4 import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; 5 import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter; 6 import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; 7 import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 9 import org.springframework.boot.CommandLineRunner;
8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 11 @Configuration 12 public class ElasticJobConfig { 13 private static CoordinatorRegistryCenter createRegistryCenter() { 14 //多點 15 //CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("ip1:port1,ip2:port2,ip3:port3", "elastic-job-demo")); 16 //單點 17 CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("127.0.0.1", "elastic-job-demo"));//(String serverLists, String namespace) 18 regCenter.init(); 19 return regCenter; 20 } 21 private static LiteJobConfiguration createJobConfiguration() { 22 //定義作業核心配置,這裡設定總片數為 3(若要修改總片數,需要刪除 zookeeper 之前的節點,或者改變上面的 namespace 否則不會立即生效) 23 JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("myJobName", "0/5 * * * * ?", 5).shardingItemParameters("0=a,1=b,2=c").build(); 24 //定義作業 SIMPLE 型別配置 25 SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, MyElasticJob.class.getCanonicalName()); 26 //定義作業 Lite 根配置 27 LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build(); 28 return simpleJobRootConfig; 29 } 30 @Bean 31 public CommandLineRunner commandLineRunner() {//CommandLineRunner 裡面的方法在應用啟動後執行 32 return (String... args) -> { 33 new JobScheduler(createRegistryCenter(), createJobConfiguration()).init(); 34 }; 35 } 36 }
 
 

 

 
 

3.任務類

 1 import com.dangdang.ddframe.job.api.ShardingContext;
 2 import com.dangdang.ddframe.job.api.simple.SimpleJob;
 3 
 4 public class MyElasticJob implements SimpleJob {
 5 
 6     @Override
 7     public void execute(ShardingContext shardingContext) {
 8         System.out.println("總分片數: " + shardingContext.getShardingTotalCount());
 9         switch (shardingContext.getShardingItem()) {
10             case 0: {
11                 System.err.println("當前分片" + shardingContext.getShardingItem());
12                 break;
13             }
14             case 1: {
15                 System.err.println("當前分片" + shardingContext.getShardingItem());
16                 break;
17             }
18             case 2: {
19                 System.err.println("當前分片" + shardingContext.getShardingItem());
20                 break;
21             }
22             default: {
23                 System.out.println("MyElasticJob - default");
24             }
25         }
26     }
27 }

4.測試

  先啟動zookeeper,然後啟動應用即可

5.補充

  可通過對資料的 id 對最大分片數進行取餘,分配到各個分片上,從而以多執行緒的方式更快速地執行任務

6.參考: 傳送門