Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
什么是任務(wù)調(diào)度
任務(wù)調(diào)度是為了自動(dòng)完成特定任務(wù),在約定的特定時(shí)刻去執(zhí)行人物的過程
為什么需要分布式調(diào)度
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)起來一起來完成任務(wù)
Elastic-Job
zookeeper是一個(gè)注冊(cè)中心,在Elastic-job過程中進(jìn)行一個(gè)選舉的功能,以及對(duì)節(jié)點(diǎn)的監(jiān)聽,誰被選舉成leader誰才可以執(zhí)行任務(wù),一旦leader掛了,將會(huì)進(jìn)行重新的選舉,依賴zookeeper里面的信息
環(huán)境搭建
第一步: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)目添加如下依賴
<dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-core</artifactId> <version>2.1.5</version> </dependency>
第三步:創(chuàng)建任務(wù)類
public class MyElasticJob implements SimpleJob { public void execute(ShardingContext shardingContext){ System.out.println("執(zhí)行任務(wù)"+new Date()); } }
第四步:創(chuàng)建配置類
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ù)名稱","corn表達(dá)式","分片數(shù)量") JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("myElasticJob","0/5 * * * * ?",1).build(); // 定義SIMPLE類型配置 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ù)
更改Zookeeper配置
雖然自己想每十秒執(zhí)行一次,但運(yùn)行結(jié)果
原因是zookeeper里面不允許覆蓋里面配置的,要是想覆蓋,就要
否則zookeeper還是讀取里面的配置
SpringBoot集成ElasticJob
第一步:添加Maven依賴
<?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)類
@SpringBootApplication public class ElasticJobServer { public static void main(String[] args) { SpringApplication.run(ElasticJobServer.class,args); } }
第三步:創(chuàng)建任務(wù)類
@Component public class MyElasticJob implements SimpleJob { @Override public void execute(ShardingContext shardingContext) { System.out.println("定時(shí)調(diào)度:"+new Date()); } }
第四步:創(chuàng)建任務(wù)配置類
@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ù)名稱","corn表達(dá)式","分片數(shù)量") JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(clazz.getSimpleName(),corn,shardingCount).build(); // 定義SIMPLE類型配置 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
到此這篇關(guān)于Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置的文章就介紹到這了,更多相關(guān)Java Elastic-job內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java多線程編程之InheritableThreadLocal
這篇文章主要為大家詳細(xì)介紹了java多線程編程之InheritableThreadLocal,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Windows系統(tǒng)下Java連接SQL Server的方法簡(jiǎn)介
這篇文章主要介紹了Windows系統(tǒng)下Java連接SQL Server的方法,分別是JDBC和JTDS的相關(guān)使用,需要的朋友可以參考下2015-09-09Springboot中的@ConditionalOnBean注解詳細(xì)解讀
這篇文章主要介紹了Springboot中的@ConditionalOnBean注解詳細(xì)解讀,@ConditionalOnMissingBean注解兩個(gè)類,一個(gè)Computer類,一個(gè)配置類,想要完成;如果容器中沒有Computer類,就注入備用電腦Computer類,如果有Computer就不注入,需要的朋友可以參考下2023-11-11SpringBoot實(shí)現(xiàn)郵件發(fā)送功能的姿勢(shì)分享
我們?cè)谌粘i_發(fā)中,經(jīng)常會(huì)碰到email郵件發(fā)送的場(chǎng)景,如發(fā)送驗(yàn)證碼,向客戶發(fā)送郵件等等,這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)郵件發(fā)送的相關(guān)資料,需要的朋友可以參考下2021-08-08SpringBoot配置文件、多環(huán)境配置、讀取配置的4種實(shí)現(xiàn)方式
SpringBoot支持多種配置文件位置和格式,其中application.properties和application.yml是默認(rèn)加載的文件,配置文件可以根據(jù)環(huán)境通過spring.profiles.active屬性進(jìn)行區(qū)分,命令行參數(shù)具有最高優(yōu)先級(jí),可覆蓋其他所有配置2024-09-09Ehcache簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Ehcache簡(jiǎn)介,使用Spring的AOP進(jìn)行整合,可以靈活的對(duì)方法的返回結(jié)果對(duì)象進(jìn)行緩存2017-07-07