全部產品
Search
文件中心

SchedulerX:在本地接入公網環境

更新時間:Jul 06, 2024

當SchedulerX在公網地區時,只要可以訪問公網的機器或容器(例如,非阿里雲的機器或本地開發環境),都可以接入SchedulerX。本文介紹如何在本地接入SchedulerX進行測試和開發。

前提條件

操作步驟

  1. 在應用pom.xml檔案中添加SchedulerxWorker依賴。不同應用初始化SchedulerxWorker時有所不同:

    普通Java或Spring應用

    <dependency>
      <groupId>com.aliyun.schedulerx</groupId>
      <artifactId>schedulerx2-worker</artifactId>
      <version>${schedulerx2.version}</version>
      <!--如果用的是logback,需要把log4j和log4j2排除掉 -->
      <exclusions>
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>              

    Spring Boot應用

    <dependency>
      <groupId>com.aliyun.schedulerx</groupId>
      <artifactId>schedulerx2-spring-boot-starter</artifactId>
      <version>${schedulerx2.version}</version>
      <!--如果使用logback,需要將log4j和log4j2排除 -->
      <exclusions>
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>                   
  2. 擷取接入應用的配置資訊,如部署應用的公網地區(Region)和對應的Endpoint(acm.aliyun.com)等。

    1. 登入分布式任務調度平台,在頂部功能表列,將地區切換為公網

    2. 在左側導覽列,單擊應用管理,在應用管理頁面,單擊操作列的接入配置擷取對應接入方式的配置資訊。

      image.png

      image.png

      在①處選擇不同的接入方式擷取接入配置。

  3. 初始化SchedulerxWorker。不同應用在初始化SchedulerxWorker有所不同:

    普通Java應用

    在main函數中初始化SchedulerxWorker。

    public void initSchedulerxWorker() throws Exception {
        SchedulerxWorker schedulerxWorker = new SchedulerxWorker();
        schedulerxWorker.setEndpoint("xxxx");
        schedulerxWorker.setNamespace("xxxx");
        schedulerxWorker.setGroupId("xxxx");
        //1.2.1及以上版本需要設定應用key
        schedulerxWorker.setAppKey("xxxx");
        schedulerxWorker.init();
    }

    接入配置面板,單擊一鍵複製將配置拷貝至對應設定檔中,或將schedulerxWorker.setEndpointschedulerxWorker.setNamespace schedulerxWorker.setGroupIdschedulerxWorker.setAppKey的值設定為步驟2中擷取的參數值。

    說明
    • 一個應用如果包含多個業務,或者需將定時任務歸類,可以建立多個分組。例如,應用animals建立兩個分組animals.dogsanimals.cats。此時,無需申請兩批執行個體分別接入這兩個分組,只需在應用用戶端中將這兩個分組配置到groupId=後面即可,例如groupId=animals.dogs,animals.cats

    • 在初始化SchedulerxWorker用戶端時,如需添加其他配置需求,請參見SchedulerxWorker配置參數說明

    Spring應用

    在xml設定檔中注入SchedulerxWorker Bean。

    <bean id="schedulerxWorker" class="com.alibaba.schedulerx.worker.SchedulerxWorker">
        <property name="endpoint">
          <value>${endpoint}</value>
        </property>
        <property name="namespace">
          <value>${namespace}</value>
        </property>
        <property name="groupId">
          <value>${groupId}</value>
        </property>
        <!--1.2.1及以上版本設定appKey -->
        <property name="appKey">
          <value>${appKey}</value>
        </property>
      
    </bean>

    接入配置面板,單擊一鍵複製將配置拷貝至對應設定檔中,或將${endpoint}${namespace}${groupId}${appKey}替換為步驟2中擷取的參數值。

    Spring Boot應用

    application.properties檔案中添加如下配置:

    spring.schedulerx2.endpoint=${endpoint}
    spring.schedulerx2.namespace=${namespace}
    spring.schedulerx2.groupId=${groupId}
    # 1.2.1及以上版本設定appKey
    spring.schedulerx2.appKey=${appKey} 

    接入配置面板,單擊一鍵複製將配置拷貝至對應設定檔中,或將${endpoint}${namespace}${groupId}${appKey} 替換為步驟2中擷取的參數值。

    說明

    如果一個應用程式套件含多個業務,或者需要將定時任務進行歸類,可以建立多個分組。例如,應用animals建立了兩個分組animals.dogs和animals.cats,此時無需申請兩批執行個體分別接入這兩個分組,在應用用戶端中將這兩個分組配置到groupId=後面即可,例如groupId=animals.dogs,animals.cats

  4. 在應用中建立類JavaProcessor,實現任務調度。

    下方範例程式碼介紹如何?一個簡單的定時列印hello schedulerx2.0的JavaProcessor類。

    package com.aliyun.schedulerx.test.job;
    
    import com.alibaba.schedulerx.worker.domain.JobContext;
    import com.alibaba.schedulerx.worker.processor.JavaProcessor;
    import com.alibaba.schedulerx.worker.processor.ProcessResult;
    
    @Component
    public class MyHelloJob extends JavaProcessor {
    
        @Override
        public ProcessResult process(JobContext context) throws Exception {
            System.out.println("hello schedulerx2.0");
            return new ProcessResult(true);
        }
    }          
  5. 運行本地應用。

結果驗證

  1. 用戶端接入完成,將該應用發布到阿里雲。

  2. 登入分布式任務調度平台,在頂部功能表列選擇地區,然後在左側導覽列,單擊應用管理,在應用管理頁面查看執行個體總數

    • 如果執行個體總數為0,說明應用接入失敗。請檢查、修改本地應用。

    • 如果執行個體總數不為0,顯示接入的執行個體個數,說明應用接入成功。在操作列單擊查看執行個體,即可在串連執行個體對話方塊中查看執行個體列表。