SpringBoot 快速集成 Elastic Job


 

一、引入依赖

<dependency>
    <groupId>com.github.kuhn-hegroupId>
    <artifactId>elastic-job-lite-spring-boot-starterartifactId>
    <version>2.1.53version>
dependency>

 

二、配置Elastic Job

#elastic-job
elaticjob.zookeeper.server-lists=127.0.0.1:2181
elaticjob.zookeeper.namespace=bounter-job

 

三、开发Job类

package cn.bounter.job.elasticjob;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
import org.springframework.stereotype.Component;

//Job configuration annotation
@ElasticSimpleJob(cron="*/3 * * * * ?", shardingTotalCount=2)
@Component
public class AppJob implements SimpleJob {
    @Override
    public void execute(ShardingContext shardingContext) {
        switch (shardingContext.getShardingItem()) {
            case 0:
                System.out.println("hello from shard 0");
                break;
            case 1:
                System.out.println("hello from shard 1");
                break;
        }
    }
}

 

四、启动SpringBoot应用,观察控制台输出

 

这就是所有步骤了,是不是很简单哉!那就赶快自己试试吧!
 
需要源代码的见Github传送门:
https://github.com/13babybear/bounter-job

转载于:https://www.cnblogs.com/gdufs/p/10925420.html

你可能感兴趣的:(SpringBoot 快速集成 Elastic Job)