微服务分布式调度Elastic-job

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: 微服务分布式调度Elastic-job


什么是任务调度

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


为什么需要分布式调度?

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.MyElasticJob
        System.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依赖

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

第二步:创建启动类

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

第三步:创建任务类

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

第四步:创建任务配置类

@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);
        //设置节点超时时间
        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.MyElasticJob
        System.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:2181
  groupName: elastic-job-boot
相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
目录
相关文章
|
3月前
|
Java 调度 Maven
Elastic-job分布式调度系统
Elastic-job分布式调度系统
|
14天前
|
存储 消息中间件 Apache
比较微服务中的分布式事务模式
比较微服务中的分布式事务模式
31 2
|
3月前
|
算法 调度
电动汽车集群并网的分布式鲁棒优化调度matlab
电动汽车集群并网的分布式鲁棒优化调度matlab
|
3月前
|
消息中间件 分布式计算 中间件
秀出天际!阿里甩出的988页分布式微服务架构进阶神仙手册我粉了
秀出天际!阿里甩出的988页分布式微服务架构进阶神仙手册我粉了
|
11天前
|
Cloud Native 云计算 微服务
云原生时代:企业分布式应用架构的惊人蜕变,从SOA到微服务的大逃亡!
【8月更文挑战第8天】在云计算与容器技术推动下,企业分布式应用架构正经历从SOA到微服务再到云原生的深刻变革。SOA强调服务重用与组合,通过标准化接口实现服务解耦;微服务以细粒度划分服务,增强系统灵活性;云原生架构借助容器化与自动化技术简化部署与管理。每一步演进都为企业带来新的技术挑战与机遇。
42 6
|
28天前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14209 19
|
17天前
|
存储 监控 安全
|
1月前
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
119 5
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB,阿里云的开源分布式数据库,与微服务相结合,提供灵活扩展和高效管理解决方案。
【7月更文挑战第3天】PolarDB,阿里云的开源分布式数据库,与微服务相结合,提供灵活扩展和高效管理解决方案。通过数据分片和水平扩展支持微服务弹性,保证高可用性,且兼容MySQL协议,简化集成。示例展示了如何使用Spring Boot配置PolarDB,实现服务动态扩展。PolarDB缓解了微服务数据库挑战,加速了开发部署,为云原生应用奠定基础。
186 3
|
29天前
|
消息中间件 缓存 架构师
对抗软件复杂度问题之降低代码的复杂度,如何解决
对抗软件复杂度问题之降低代码的复杂度,如何解决

热门文章

最新文章