微服务分布式调度Elastic-job

  • 什么是任务调度?
  • 为什么需要分布式调度?
  • Elastic-Job
    • 环境搭建
    • 更改Zookeeper配置
    • SpringBoot集成ElasticJob

什么是任务调度?

任务调度是为了自动完成特定任务,在约定的特定时刻去执行人物的过程


为什么需要分布式调度?

1.使用Spring定时器,在集群的情况下,可能会导致任务重复执行的情况,当我们部署了多台服务,同时每台服务又有定时任务时,若不进行合理的控制在同一时间,只有一个定时任务启动执行了,这时,定时执行的结果就可能存在混乱和错误了,考虑使用分布式锁,保证任务不会重复执行
2.大大提高了可用性,当做了集群之后,某个项目挂了,任务应该要由另外一个项目继续进行
3.单机处理始终有极限,假设该主服务器有ABCD四个任务,可以将任务分配给自己底下的从服务器,将它们调动起来一起来完成任务


Elastic-Job


zookeeper是一个注册中心,在Elastic-job过程中进行一个选举的功能,以及对节点的监听,谁被选举成leader谁才可以执行任务,一旦leader挂了,将会进行重新的选举,依赖zookeeper里面的信息

环境搭建

第一步:Zookeeper安装并运行

1)解压zookeeper-3.4.11.tar.进入conf目录,复制zoo_sample.cfg文件,命名为zoo.cfg
2)进入bin目录,运行zkServer.cmd就可以了
3)解压ZooInspector运行文件

第二步:创建maven项目添加如下依赖

<dependency><groupId>com.dangdang</groupId><artifactId>elastic-job-lite-core</artifactId><version>2.1.5</version></dependency>

第三步:创建任务类

public class MyElasticJob implements SimpleJob {public void execute(ShardingContext shardingContext){System.out.println("执行任务"+new Date());}}

第四步:创建配置类

public class JobDemo {public static void main(String[] args) {//JobScheduler(注册中心对象,任务配置对象)new JobScheduler(createRegistryCenter(),createJobConfiguration()).init();}//定时任务配置private static LiteJobConfiguration createJobConfiguration() {//定义作业核心配置newBuilder("任务名称","corn表达式","分片数量")JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("myElasticJob","0/5 * * * * " />,1).build();// 定义SIMPLE类型配置 cn.wolfcode.MyElasticJobSystem.out.println("MyElasticJob.class.getCanonicalName-->"+ MyElasticJob.class.getCanonicalName());SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,MyElasticJob.class.getCanonicalName());//定义Lite作业根配置LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();return simpleJobRootConfig;}//注册中心配置private static CoordinatorRegistryCenter createRegistryCenter() {ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("43.143.161.59:2181", "elastic-job-demo");//设置节点超时时间zookeeperConfiguration.setSessionTimeoutMilliseconds(100);//zookeeperConfiguration("zookeeper地址","项目名")CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);regCenter.init();return regCenter;}}

运行结果:

启动多台机器的时候,只有一台机器运行


当leader终止后,再次进行选取执行任务


更改Zookeeper配置


虽然自己想每十秒执行一次,但运行结果

原因是zookeeper里面不允许覆盖里面配置的,要是想覆盖,就要

否则zookeeper还是读取里面的配置


SpringBoot集成ElasticJob

第一步:添加Maven依赖

<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>

第二步:创建启动类

@SpringBootApplicationpublic class ElasticJobServer {public static void main(String[] args) {SpringApplication.run(ElasticJobServer.class,args);}}

第三步:创建任务类

@Componentpublic class MyElasticJob implements SimpleJob {@Overridepublic void execute(ShardingContext shardingContext) {System.out.println("定时调度:"+new Date());}}

第四步:创建任务配置类

@Configurationpublic class JobConfig {@Beanpublic static CoordinatorRegistryCenter registryCenter(@Value("${zookeeper.url}") String url,@Value("${zookeeper.groupName}") String groupName) {ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(url, groupName);//设置节点超时时间zookeeperConfiguration.setSessionTimeoutMilliseconds(100);//zookeeperConfiguration("zookeeper地址","项目名")CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);regCenter.init();return regCenter;}//功能的方法public static LiteJobConfiguration createJobConfiguration(Class clazz,String corn,int shardingCount) {//定义作业核心配置newBuilder("任务名称","corn表达式","分片数量")JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(clazz.getSimpleName(),corn,shardingCount).build();// 定义SIMPLE类型配置 cn.wolfcode.MyElasticJobSystem.out.println("MyElasticJob.class.getCanonicalName-->"+ MyElasticJob.class.getCanonicalName());SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,clazz.getCanonicalName());//定义Lite作业根配置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:2181groupName: elastic-job-boot