SpringBoot使用Elastic-Job

本文介紹SpringBoot整合Elastic-Job分布式調(diào)度任務(wù)(簡(jiǎn)單任務(wù))。

1.有關(guān)Elastic-Job

Elastic-Job是當(dāng)當(dāng)網(wǎng)開源的分布式任務(wù)調(diào)度解決方案,是業(yè)內(nèi)使用較多的分布式調(diào)度解決方案。

image

這里主要介紹Elastic-Job-Lite,Elastic-Job-Lite定位為輕量級(jí)無中心化解決方案,使用jar包的形式提供最輕量級(jí)的分布式任務(wù)的協(xié)調(diào)服務(wù),外部依賴僅Zookeeper。

架構(gòu)圖如下:

image

Elastic-Job官網(wǎng)地址:http://elasticjob.io/index_zh.html
Elastic-Job-Lite官方文檔地址:http://elasticjob.io/docs/elastic-job-lite/00-overview/intro/

2.使用Elastic-Job

2.1 加入依賴

新建項(xiàng)目,在項(xiàng)目中加入Elastic-Job依賴,完整pom如代碼清單所示。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dalaoyang</groupId>
    <artifactId>springboot2_elasticjob</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot2_elasticjob</name>
    <description>springboot2_elasticjob</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 配置文件

配置文件中需要配置一下zookeeper地址和namespace名稱,注意:這個(gè)不是必須要配置的,在文件中直接寫死也是可以的,配置文件如下所示。

spring.application.name=springboot2_elasticjob

regCenter.serverList=localhost:2181
regCenter.namespace=springboot2_elasticjob

2.3 配置zookeeper

接下來需要配置一下zookeeper,創(chuàng)建一個(gè)JobRegistryCenterConfig,內(nèi)容如下:

@Configuration
@ConditionalOnExpression("'${regCenter.serverList}'.length() > 0")
public class JobRegistryCenterConfig {

    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList,
                                             @Value("${regCenter.namespace}") final String namespace) {
        return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
    }

}

2.4 定義Elastic-Job任務(wù)

配置一個(gè)簡(jiǎn)單的任務(wù),這里以在日志中打印一些參數(shù)為例,如下所示。

public class MySimpleJob implements SimpleJob {
    Logger logger = LoggerFactory.getLogger(MySimpleJob.class);

    @Override
    public void execute(ShardingContext shardingContext) {
        logger.info(String.format("Thread ID: %s, 作業(yè)分片總數(shù): %s, " +
                        "當(dāng)前分片項(xiàng): %s.當(dāng)前參數(shù): %s," +
                        "作業(yè)名稱: %s.作業(yè)自定義參數(shù): %s"
                ,
                Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter()
        ));

    }
}

2.5 配置任務(wù)

配置任務(wù)的時(shí)候,這里定義了四個(gè)參數(shù),分別是:

  • cron:cron表達(dá)式,用于控制作業(yè)觸發(fā)時(shí)間。
  • shardingTotalCount:作業(yè)分片總數(shù)
  • shardingItemParameters:分片序列號(hào)和參數(shù)用等號(hào)分隔,多個(gè)鍵值對(duì)用逗號(hào)分隔
    分片序列號(hào)從0開始,不可大于或等于作業(yè)分片總數(shù)
    如:
    0=a,1=b,2=c
  • jobParameters:作業(yè)自定義參數(shù)
    作業(yè)自定義參數(shù),可通過傳遞該參數(shù)為作業(yè)調(diào)度的業(yè)務(wù)方法傳參,用于實(shí)現(xiàn)帶參數(shù)的作業(yè)
    例:每次獲取的數(shù)據(jù)量、作業(yè)實(shí)例從數(shù)據(jù)庫讀取的主鍵等。

至于其他參數(shù)請(qǐng)參考文檔,http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/

本文配置如下:

@Configuration
public class MyJobConfig {

    private final String cron = "0/5 * * * * ?";
    private final int shardingTotalCount = 3;
    private final String shardingItemParameters = "0=A,1=B,2=C";
    private final String jobParameters = "parameter";

    @Autowired
    private ZookeeperRegistryCenter regCenter;

    @Bean
    public SimpleJob stockJob() {
        return new MySimpleJob();
    }

    @Bean(initMethod = "init")
    public JobScheduler simpleJobScheduler(final SimpleJob simpleJob) {
        return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(),
                cron, shardingTotalCount, shardingItemParameters, jobParameters));
    }

    private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass,
                                                         final String cron,
                                                         final int shardingTotalCount,
                                                         final String shardingItemParameters,
                                                         final String jobParameters) {
        // 定義作業(yè)核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).
                shardingItemParameters(shardingItemParameters).jobParameter(jobParameters).build();
        // 定義SIMPLE類型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName());
        // 定義Lite作業(yè)根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
        return simpleJobRootConfig;

    }
}

3.測(cè)試

啟動(dòng)項(xiàng)目,就可以看到控制臺(tái)的輸出了,如下所示:

image

4.源碼

源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_elasticjob

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 作業(yè)的必要性以及存在的問題 1. 為什么需要作業(yè)? 作業(yè)即定時(shí)任務(wù)。一般來說,系統(tǒng)可使用消息傳遞代替部分使用作業(yè)的...
    Sununy閱讀 7,044評(píng)論 3 25
  • 文章摘要:在生產(chǎn)環(huán)境中部署Elastic-Job集群后,那么如何來運(yùn)維監(jiān)控線上跑著的定時(shí)任務(wù)呢?如果在生產(chǎn)環(huán)境的大...
    癲狂俠閱讀 9,648評(píng)論 1 27
  • 背景 因?yàn)橛衅髽I(yè)級(jí)批處理需求,具體到應(yīng)用場(chǎng)景就是在三方支付系統(tǒng)中,日切后要進(jìn)行清分結(jié)算的跑批處理。所以使用到成熟的...
    行徑行閱讀 5,900評(píng)論 2 38
  • 2018年,是很難用語言描述的一年,中美關(guān)系沖突,互聯(lián)網(wǎng)技術(shù)的挑戰(zhàn),數(shù)字化帶來的價(jià)值重構(gòu),全球市場(chǎng)的變局,民營企業(yè)...
    馮全平閱讀 341評(píng)論 0 1
  • “拼了!”沈冥怒喝了一聲,召喚出了雪熊妖靈。 但是他的身體剛剛有所變化,只聽嘭的一聲,陸飄一拳頭打在了沈冥的腹部,...
    im喵小姐閱讀 327評(píng)論 0 0

友情鏈接更多精彩內(nèi)容