您好,登錄后才能下訂單哦!
今天小編給大家分享一下Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境如何搭建及配置的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
任務(wù)調(diào)度是為了自動(dòng)完成特定任務(wù),在約定的特定時(shí)刻去執(zhí)行人物的過(guò)程
1.使用Spring定時(shí)器,在集群的情況下,可能會(huì)導(dǎo)致任務(wù)重復(fù)執(zhí)行的情況,當(dāng)我們部署了多臺(tái)服務(wù),同時(shí)每臺(tái)服務(wù)又有定時(shí)任務(wù)時(shí),若不進(jìn)行合理的控制在同一時(shí)間,只有一個(gè)定時(shí)任務(wù)啟動(dòng)執(zhí)行了,這時(shí),定時(shí)執(zhí)行的結(jié)果就可能存在混亂和錯(cuò)誤了,考慮使用分布式鎖,保證任務(wù)不會(huì)重復(fù)執(zhí)行
2.大大提高了可用性,當(dāng)做了集群之后,某個(gè)項(xiàng)目掛了,任務(wù)應(yīng)該要由另外一個(gè)項(xiàng)目繼續(xù)進(jìn)行
3.單機(jī)處理始終有極限,假設(shè)該主服務(wù)器有ABCD四個(gè)任務(wù),可以將任務(wù)分配給自己底下的從服務(wù)器,將它們調(diào)動(dòng)起來(lái)一起來(lái)完成任務(wù)
zookeeper是一個(gè)注冊(cè)中心,在Elastic-job過(guò)程中進(jìn)行一個(gè)選舉的功能,以及對(duì)節(jié)點(diǎn)的監(jiān)聽(tīng),誰(shuí)被選舉成leader誰(shuí)才可以執(zhí)行任務(wù),一旦leader掛了,將會(huì)進(jìn)行重新的選舉,依賴(lài)zookeeper里面的信息
第一步:Zookeeper安裝并運(yùn)行
1)解壓zookeeper-3.4.11.tar.進(jìn)入conf目錄,復(fù)制zoo_sample.cfg文件,命名為zoo.cfg
2)進(jìn)入bin目錄,運(yùn)行zkServer.cmd就可以了
3)解壓ZooInspector運(yùn)行文件
第二步:創(chuàng)建maven項(xiàng)目添加如下依賴(lài)
<dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-core</artifactId> <version>2.1.5</version> </dependency>
第三步:創(chuàng)建任務(wù)類(lèi)
public class MyElasticJob implements SimpleJob { public void execute(ShardingContext shardingContext){ System.out.println("執(zhí)行任務(wù)"+new Date()); } }
第四步:創(chuàng)建配置類(lèi)
public class JobDemo { public static void main(String[] args) { //JobScheduler(注冊(cè)中心對(duì)象,任務(wù)配置對(duì)象) new JobScheduler(createRegistryCenter(),createJobConfiguration()).init(); } //定時(shí)任務(wù)配置 private static LiteJobConfiguration createJobConfiguration() { //定義作業(yè)核心配置newBuilder("任務(wù)名稱(chēng)","corn表達(dá)式","分片數(shù)量") JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("myElasticJob","0/5 * * * * ?",1).build(); // 定義SIMPLE類(lèi)型配置 cn.wolfcode.MyElasticJob System.out.println("MyElasticJob.class.getCanonicalName---->"+ MyElasticJob.class.getCanonicalName()); SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,MyElasticJob.class.getCanonicalName()); //定義Lite作業(yè)根配置 LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build(); return simpleJobRootConfig; } //注冊(cè)中心配置 private static CoordinatorRegistryCenter createRegistryCenter() { ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("43.143.161.59:2181", "elastic-job-demo"); //設(shè)置節(jié)點(diǎn)超時(shí)時(shí)間 zookeeperConfiguration.setSessionTimeoutMilliseconds(100); //zookeeperConfiguration("zookeeper地址","項(xiàng)目名") CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration); regCenter.init(); return regCenter; } }
運(yùn)行結(jié)果:
啟動(dòng)多臺(tái)機(jī)器的時(shí)候,只有一臺(tái)機(jī)器運(yùn)行
當(dāng)leader終止后,再次進(jìn)行選取執(zhí)行任務(wù)
雖然自己想每十秒執(zhí)行一次,但運(yùn)行結(jié)果
原因是zookeeper里面不允許覆蓋里面配置的,要是想覆蓋,就要
否則zookeeper還是讀取里面的配置
第一步:添加Maven依賴(lài)
<?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>
第二步:創(chuàng)建啟動(dòng)類(lèi)
@SpringBootApplication public class ElasticJobServer { public static void main(String[] args) { SpringApplication.run(ElasticJobServer.class,args); } }
第三步:創(chuàng)建任務(wù)類(lèi)
@Component public class MyElasticJob implements SimpleJob { @Override public void execute(ShardingContext shardingContext) { System.out.println("定時(shí)調(diào)度:"+new Date()); } }
第四步:創(chuàng)建任務(wù)配置類(lèi)
@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); //設(shè)置節(jié)點(diǎn)超時(shí)時(shí)間 zookeeperConfiguration.setSessionTimeoutMilliseconds(100); //zookeeperConfiguration("zookeeper地址","項(xiàng)目名") CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration); regCenter.init(); return regCenter; } //功能的方法 public static LiteJobConfiguration createJobConfiguration(Class clazz,String corn,int shardingCount) { //定義作業(yè)核心配置newBuilder("任務(wù)名稱(chēng)","corn表達(dá)式","分片數(shù)量") JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(clazz.getSimpleName(),corn,shardingCount).build(); // 定義SIMPLE類(lèi)型配置 cn.wolfcode.MyElasticJob System.out.println("MyElasticJob.class.getCanonicalName---->"+ MyElasticJob.class.getCanonicalName()); SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,clazz.getCanonicalName()); //定義Lite作業(yè)根配置 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微服務(wù)分布式調(diào)度Elastic-job環(huán)境如何搭建及配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。