第一篇需要实现一个最简单的需求:某个任务定时执行,多台机子只让其中一台机子执行任务

一、安装 分布式应用程序协调服务 zookeeper,安装步骤在链接里面

 Linux(Centos7)下安装 zookeeper docker版 集群 

二、在springboot项目中引入 elastic-job 依赖,我这里用的 springboot 2.0.5 版本

整合代码参考官方的springboot demo

https://github.com/elasticjob/elastic-job-example

对应的  elastic-job 版本

<elastic-job.version>2.1.5</elastic-job.version>

        <!-- apache elastic-job start -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>${elastic-job.version}</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>${elastic-job.version}</version>
</dependency>
<!-- apache elastic-job end -->

依赖XML代码

application.yaml 加入配置

regCenter:
serverList: localhost:2181
namespace: my-project simpleJob:
cron: 0/15 * * * * ?
shardingTotalCount: 1
shardingItemParameters: 0=a dataflowJob:
cron: 0/5 * * * * ?
shardingTotalCount: 3
shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou

application.yaml 配置代码

三、作业代码(simpleJob类型的任务),把官方的demo拿到自己的项目中来就行,注意添加一个@Component注解在类上

/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/ package com.dianji.task_server.job.exec; import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date; @Slf4j
@Component
public class SpringSimpleJob implements SimpleJob {
@Value("${flag}")
private String serverFlag; @Override
public void execute(final ShardingContext shardingContext) {
int shardingItem = shardingContext.getShardingItem();
if (shardingItem == 0) {
log.info("这是来自『{}』的任务,SIMPLE「SpringSimpleJob_execute」", serverFlag + ":" +
shardingContext.getShardingParameter());
String logStr = "Item: {0} | Parameter: {1} | : {2} | Time: {3} | Thread: {4} | {5}";
logStr = MessageFormat.format(logStr, shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
new SimpleDateFormat("HH:mm:ss").format(new Date()),
Thread.currentThread().getId(), "SIMPLE「SpringSimpleJob_execute」");
log.info(logStr);
} else if (shardingItem == 1) {
log.info("这是来自『{}』的任务", serverFlag + ":" + shardingContext.getShardingParameter());
} else if (shardingItem == 2) {
log.info("这是来自『{}』的任务", shardingContext.getShardingParameter());
}
}
}

作业任务代码

四、配置作业(直接将官方的配置代码拿进自己的项目中)

/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/ package com.dianji.task_server.job.config; import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.dianji.task_server.job.exec.SpringSimpleJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; @Configuration
public class SimpleJobConfig { @Resource
private ZookeeperRegistryCenter regCenter; @Resource
private JobEventConfiguration jobEventConfiguration; @Bean
public SimpleJob simpleJob() {
return new SpringSimpleJob();
} @Bean(initMethod = "init")
public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters) {
return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
} private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}

作业配置代码

五、最简单的整合步骤就完成了,启动应用(分别启动三个进程),看见任务开始执行了

最简单的任务需求得到了满足,定时执行,多台机子只让其中一台机子执行任务

关于分片的数量设置,实际操作了一把

假如:有3个进程实例
并且elastic-job 设置  

分片 1 时:3个进程只有1一个进程执行1次任务,共执行1次任务

分片 2 时:3个进程中的2个进程一共执行2次任务,共执行2次任务

分片 3 时:3个进程每个执行1次任务,共执行3次任务

分片 4 时:3个进程同时一共执行4次任务,共执行4次任务

分片 5 时:3个进程同时一共执行5次任务,共执行5次任务

 
分片为X时,此任务同时在Y台个进程里面执行X次。
 
这样就可以根据不同的使用场景配置不同的分片数量了
 

elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行的更多相关文章

  1. elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(二)动态添加任务需求

    之前一篇用过了如何在使用创建最简单的任务:比如每天定时清空系统的缓存 这篇文章主要讲解:如何运用elastic-job-lite做灵活的细粒度任务,比如: 如何定时取消某个订单在下订单后30分钟未支付 ...

  2. 分布式定时任务框架——python定时任务框架APScheduler扩展

    http://bbs.7boo.org/forum.php?mod=viewthread&tid=14546 如果将定时任务部署在一台服务器上,那么这个定时任务就是整个系统的单点,这台服务器出 ...

  3. Elastic-Job - 分布式定时任务框架

    Elastic-Job - 分布式定时任务框架 摘要 Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范 ...

  4. 分布式定时任务框架比较,spring batch, tbschedule jobserver

    分布式定时任务框架比较,spring batch, tbschedule jobserver | 移动开发参考书 分布式定时任务框架比较,spring batch, tbschedule jobser ...

  5. 基于spring+quartz的分布式定时任务框架

    问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...

  6. Elastic-Job——分布式定时任务框架

    摘要: Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范部分.该项目基于成熟的开源产品Quartz和Z ...

  7. Quartz小记(一):Elastic-Job - 分布式定时任务框架

    Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范部分.该项目基于成熟的开源产品Quartz和Zooke ...

  8. Scheduled定时任务器在Springboot中的使用

    Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...

  9. Spring Boot 2.x基础教程:使用Elastic Job实现定时任务

    上一篇,我们介绍了如何使用Spring Boot自带的@Scheduled注解实现定时任务.文末也提及了这种方式的局限性.当在集群环境下的时候,如果任务的执行或操作依赖一些共享资源的话,就会存在竞争关 ...

随机推荐

  1. dubbo监控中心搭建

    从网上下载了一个dubbo监控中心,地址忘了,文件名是dubbo-monitor-simple-2.5.3-assembly.tar.gz. 修改监控中心配置文件如下: dubbo.container ...

  2. _Python定义方法

    def 定义一个方法 在项目编程中,我们往往要做很多重复的事,比如一个排序的功能(当然Python中内置排序的方法),在编程中,我们肯定是会多次用到这个功能的,如果我们每次都在要用这个功能时,都去写一 ...

  3. forget word out4

    1★ be 使~ 成为:   2★ bene bene   3★ bi 2,两个,双重   4★ by 在~ 旁边,副的  

  4. 小程序公用js提取到app.js中调用的实例

    index.wxml: <view "> <text>{{page}}</text> </view> <view "> ...

  5. Openwrt Udev Configure(3)

    1      Scope of Document This document describes how to write udev script, when enum usb device mayb ...

  6. HeadFirstJava

    java执行过程的来龙去脉 源代码——编译器——输出——java虚拟机 扩展名为.java ——扩展名为.class 不要直接用类名点变量来改变属性值,一般都用get.set方法.封装的基本原则:将你 ...

  7. asp.net文件压缩,下载,物理路径,相对路径,删除文件

    知识动手实践一次,就可以变成自己的了.不然一直是老师的,书本的. 这几天做了一个小小的项目,需要用到文件下载功能,期初想到只是单个的文件,后面想到如果很多文件怎么办?于是又想到文件压缩.几经波折实践, ...

  8. bzoj1082

    题解: 暴搜+二分+剪枝 二分答案,暴力判断是否有解 然后加上剪枝 代码: #include<bits/stdc++.h> using namespace std; ; int rest, ...

  9. docker 部署 flask(二)编写及生成镜像。

    简介: 上一篇文章,我们简单的测试了一下服务器环境和docker基础镜像.并没有涉及我们自己编写的flask python程序. 现在,我们就要把我们自己的flask程序,放进docker镜像. 但是 ...

  10. 关于C++静态成员函数访问非静态成员变量的问题

    静态成员函数不能访问非静态成员,这是因为静态函数属于类而不是属于整个对象,静态函数中的 member可能都没有分配内存.静态成员函数没有隐含的this自变量.所以,它就无法访问自己类的非静态成员 代码 ...