Java微服務分佈式調度Elastic-job環境搭建及配置

什麼是任務調度

任務調度是為瞭自動完成特定任務,在約定的特定時刻去執行人物的過程

為什麼需要分佈式調度

1.使用Spring定時器,在集群的情況下,可能會導致任務重復執行的情況,當我們部署瞭多臺服務,同時每臺服務又有定時任務時,若不進行合理的控制在同一時間,隻有一個定時任務啟動執行瞭,這時,定時執行的結果就可能存在混亂和錯誤瞭,考慮使用分佈式鎖,保證任務不會重復執行

2.大大提高瞭可用性,當做瞭集群之後,某個項目掛瞭,任務應該要由另外一個項目繼續進行

3.單機處理始終有極限,假設該主服務器有ABCD四個任務,可以將任務分配給自己底下的從服務器,將它們調動起來一起來完成任務

Elastic-Job

zookeeper是一個註冊中心,在Elastic-job過程中進行一個選舉的功能,以及對節點的監聽,誰被選舉成leader誰才可以執行任務,一旦leader掛瞭,將會進行重新的選舉,依賴zookeeper裡面的信息

環境搭建

第一步:Zookeeper安裝並運行

1)解壓zookeeper-3.4.11.tar.進入conf目錄,復制zoo_sample.cfg文件,命名為zoo.cfg

2)進入bin目錄,運行zkServer.cmd就可以瞭

3)解壓ZooInspector運行文件

第二步:創建maven項目添加如下依賴

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

第三步:創建任務類

public class MyElasticJob implements SimpleJob {
    public void execute(ShardingContext shardingContext){
        System.out.println("執行任務"+new Date());
    }
}

第四步:創建配置類

public class JobDemo {
    public static void main(String[] args) {
        //JobScheduler(註冊中心對象,任務配置對象)
        new JobScheduler(createRegistryCenter(),createJobConfiguration()).init();
    }
    //定時任務配置
    private static LiteJobConfiguration createJobConfiguration() {
        //定義作業核心配置newBuilder("任務名稱","corn表達式","分片數量")
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("myElasticJob","0/5 * * * * ?",1).build();
        // 定義SIMPLE類型配置 cn.wolfcode.MyElasticJob
        System.out.println("MyElasticJob.class.getCanonicalName---->"+ MyElasticJob.class.getCanonicalName());
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,MyElasticJob.class.getCanonicalName());
        //定義Lite作業根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return simpleJobRootConfig;
    }
    //註冊中心配置
    private static CoordinatorRegistryCenter createRegistryCenter() {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("43.143.161.59:2181", "elastic-job-demo");
        //設置節點超時時間
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        //zookeeperConfiguration("zookeeper地址","項目名")
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        regCenter.init();
        return regCenter;
    }
}

運行結果:

啟動多臺機器的時候,隻有一臺機器運行

當leader終止後,再次進行選取執行任務

更改Zookeeper配置

雖然自己想每十秒執行一次,但運行結果

原因是zookeeper裡面不允許覆蓋裡面配置的,要是想覆蓋,就要

否則zookeeper還是讀取裡面的配置

SpringBoot集成ElasticJob

第一步:添加Maven依賴

<?xml version="1.0" encoding="UTF-8"?>
<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>cn.wolfcode</groupId>
  <artifactId>elstaic-job-boot</artifactId>
  <version>1.0.0</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.dangdang</groupId>
      <artifactId>elastic-job-lite-spring</artifactId>
      <version>2.1.5</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
  </dependencies>
</project>

第二步:創建啟動類

@SpringBootApplication
public class ElasticJobServer {
    public static void main(String[] args) {
        SpringApplication.run(ElasticJobServer.class,args);
    }
}

第三步:創建任務類

@Component
public class MyElasticJob implements SimpleJob {
    @Override
    public void execute(ShardingContext shardingContext) {
        System.out.println("定時調度:"+new Date());
    }
}

第四步:創建任務配置類

@Configuration
public class JobConfig {
    @Bean
    public static CoordinatorRegistryCenter registryCenter(@Value("${zookeeper.url}") String url,@Value("${zookeeper.groupName}") String groupName) {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(url, groupName);
        //設置節點超時時間
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        //zookeeperConfiguration("zookeeper地址","項目名")
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        regCenter.init();
        return regCenter;
    }
    //功能的方法
    public static LiteJobConfiguration createJobConfiguration(Class clazz,String corn,int shardingCount) {
        //定義作業核心配置newBuilder("任務名稱","corn表達式","分片數量")
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(clazz.getSimpleName(),corn,shardingCount).build();
        // 定義SIMPLE類型配置 cn.wolfcode.MyElasticJob
        System.out.println("MyElasticJob.class.getCanonicalName---->"+ MyElasticJob.class.getCanonicalName());
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,clazz.getCanonicalName());
        //定義Lite作業根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return simpleJobRootConfig;
    }
    @Bean(initMethod = "init")
    public SpringJobScheduler testScheduler(MyElasticJob job,CoordinatorRegistryCenter registryCenter){
        LiteJobConfiguration jobConfiguration = createJobConfiguration(job.getClass(),"0/5 * * * * ?",1);
        return new SpringJobScheduler(job,registryCenter,jobConfiguration);
    }
}

第五步:配置文件

zookeeper:
  url: 43.143.161.59:2181
  groupName: elastic-job-boot

到此這篇關於Java微服務分佈式調度Elastic-job環境搭建及配置的文章就介紹到這瞭,更多相關Java Elastic-job內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: