一、引入依赖
<dependency> <groupId>com.github.kuhn-he</groupId> <artifactId>elastic-job-lite-spring-boot-starter</artifactId> <version>2.1.53</version> </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传送门: