本文介绍SpringBoot整合Elastic-Job分布式调度任务(简单任务)。

1.有关Elastic-Job

Elastic-Job是当当网开源的分布式任务调度解决方案,是业内使用较多的分布式调度解决方案。

这里主要介绍Elastic-Job-Lite,Elastic-Job-Lite定位为轻量级无中心化解决方案,使用jar包的形式提供最轻量级的分布式任务的协调服务,外部依赖仅Zookeeper。

架构图如下:

Elastic-Job官网地址:http://elasticjob.io/index_zh.html

Elastic-Job-Lite官方文档地址:http://elasticjob.io/docs/elastic-job-lite/00-overview/intro/

2.使用Elastic-Job

2.1 加入依赖

新建项目,在项目中加入Elastic-Job依赖,完整pom如代码清单所示。

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalaoyang</groupId>
<artifactId>springboot2_elasticjob</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2_elasticjob</name>
<description>springboot2_elasticjob</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.2 配置文件

配置文件中需要配置一下zookeeper地址和namespace名称,注意:这个不是必须要配置的,在文件中直接写死也是可以的,配置文件如下所示。

spring.application.name=springboot2_elasticjob

regCenter.serverList=localhost:2181
regCenter.namespace=springboot2_elasticjob

2.3 配置zookeeper

接下来需要配置一下zookeeper,创建一个JobRegistryCenterConfig,内容如下:

@Configuration
@ConditionalOnExpression("'${regCenter.serverList}'.length() > 0")
public class JobRegistryCenterConfig { @Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList,
@Value("${regCenter.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
} }

2.4 定义Elastic-Job任务

配置一个简单的任务,这里以在日志中打印一些参数为例,如下所示。

public class MySimpleJob implements SimpleJob {
Logger logger = LoggerFactory.getLogger(MySimpleJob.class); @Override
public void execute(ShardingContext shardingContext) {
logger.info(String.format("Thread ID: %s, 作业分片总数: %s, " +
"当前分片项: %s.当前参数: %s," +
"作业名称: %s.作业自定义参数: %s"
,
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()
)); }
}

2.5 配置任务

配置任务的时候,这里定义了四个参数,分别是:

  • cron:cron表达式,用于控制作业触发时间。
  • shardingTotalCount:作业分片总数
  • shardingItemParameters:分片序列号和参数用等号分隔,多个键值对用逗号分隔

    分片序列号从0开始,不可大于或等于作业分片总数

    如:

    0=a,1=b,2=c
  • jobParameters:作业自定义参数

    作业自定义参数,可通过传递该参数为作业调度的业务方法传参,用于实现带参数的作业

    例:每次获取的数据量、作业实例从数据库读取的主键等。

至于其他参数请参考文档,http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/

本文配置如下:

@Configuration
public class MyJobConfig { private final String cron = "0/5 * * * * ?";
private final int shardingTotalCount = 3;
private final String shardingItemParameters = "0=A,1=B,2=C";
private final String jobParameters = "parameter"; @Autowired
private ZookeeperRegistryCenter regCenter; @Bean
public SimpleJob stockJob() {
return new MySimpleJob();
} @Bean(initMethod = "init")
public JobScheduler simpleJobScheduler(final SimpleJob simpleJob) {
return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(),
cron, shardingTotalCount, shardingItemParameters, jobParameters));
} private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters,
final String jobParameters) {
// 定义作业核心配置
JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).
shardingItemParameters(shardingItemParameters).jobParameter(jobParameters).build();
// 定义SIMPLE类型配置
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName());
// 定义Lite作业根配置
LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
return simpleJobRootConfig; }
}

3.测试

启动项目,就可以看到控制台的输出了,如下所示:

4.源码

源码地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_elasticjob

SpringBoot使用Elastic-Job的更多相关文章

  1. SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践

    一. 前言 日志对于一个程序的重要程度不用过多的言语修饰,本篇将以实战的方式讲述开源微服务全栈项目 有来商城 是如何整合当下主流日志解决方案 ELK +Filebeat . 话不多说,先看实现的效果图 ...

  2. Spring Security OAuth2 微服务认证中心自定义授权模式扩展以及常见登录认证场景下的应用实战

    一. 前言 [APP 移动端]Spring Security OAuth2 手机短信验证码模式 [微信小程序]Spring Security OAuth2 微信授权模式 [管理系统]Spring Se ...

  3. SpringBoot 快速集成 Elastic Job

    一.引入依赖 <dependency> <groupId>com.github.kuhn-he</groupId> <artifactId>elasti ...

  4. SpringBoot使用ELK日志收集ELASTIC (ELK) STACK

    1:资源 # 文档向导 # logstash https://www.elastic.co/guide/en/logstash/current/index.html #kibana https://w ...

  5. Elastic Job入门(3) - 集成Springboot

    引入pom文件 <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job ...

  6. ELK学习笔记(四)SpringBoot+Logback+Redis+ELK实例

    废话不多说,直接上干货,首先看下整体应用的大致结构.(整个过程我用到了两台虚拟机  应用和Shipper 部署在192.168.25.128 上 Redis和ELK 部署在192.168.25.129 ...

  7. springboot elasticsearch 集成注意事项

    文章来源: http://www.cnblogs.com/guozp/p/8686904.html 一 elasticsearch基础 这里假设各位已经简单了解过elasticsearch,并不对es ...

  8. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  9. ELK入门使用-与springboot集成

    前言 ELK官方的中文文档写的已经挺好了,为啥还要记录本文?因为我发现,我如果不写下来,过几天就忘记了,而再次捡起来必然还要经历资料查找筛选测试的过程.虽然这个过程很有意义,但并不总是有那么多时间去做 ...

  10. springboot~maven制作底层公用库

    把一些公用方法,类型抽象到一个项目里,让其它项目依赖它,这种设计是一种解耦的体现,其实像springboot就是我们的一种依赖,他里面有很多子模块,用到哪个就添加哪个依赖即可,像redis,mongo ...

随机推荐

  1. 基于IPV6的数据包分析(GNS3)

    1.拓扑图 2.配置ipv6地址.使路由器之间可互ping,用ospf配置.(R5为例) 查看路由表 试R5 ping 到R4 R4 ping到 R1 3.开始抓包分析 128返回请求(Echo Re ...

  2. Basic 001 Bob

    Instructions Bob is a lackadaisical teenager. In conversation, his responses are very limited.Bob an ...

  3. Android相关面试题---初识

    一 .Activity的生命周期 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称为返回栈(Back Stack).每当我们启动一个新的活动,它会 ...

  4. 通过FileReader和FileWriter实现复制文件的方法。

    public class CopyDemo { public static void main(String []args) { copyd(); } public static  void copy ...

  5. MDX Query - mdx的基本语法和概念

    文档: https://wenku.baidu.com/view/ef14b0e1900ef12d2af90242a8956bec0975a5e7.html?rec_flag=default http ...

  6. 文件比较与同步工具——FreeFileSync

    1. 基本介绍 FreeFileSync是一个用于文件同步的免费开源程序.FreeFileSync通过比较其内容,日期或文件大小上的一个或多个文件夹,然后根据用户定义的设置同步内容.除了支持本地文件系 ...

  7. 【转】一文掌握 Linux 性能分析之 CPU 篇

    [转]一文掌握 Linux 性能分析之 CPU 篇 平常工作会涉及到一些 Linux 性能分析的问题,因此决定总结一下常用的一些性能分析手段,仅供参考. 说到性能分析,基本上就是 CPU.内存.磁盘 ...

  8. nb-iot简介【转】

    转自:http://www.elecfans.com/tags/nb-iot/ 标签 > nb-iot nb-iot 关注 118人关注 提供NB-IoT技术特点,NB-IoT模块/芯片,NB- ...

  9. Haproxy_haproxy.cfg

    global # 全局参数的设置 log 127.0.0.1 local2 # log语法:log <address_1>[max_level_1] # 全局的日志配置,使用log关键字, ...

  10. go语言关于值类型和引用类型

    前言:关于值类型和引用类型确实是个比较绕的问题,之前在学校的时候学习C语言的时候,就感觉没有看太懂,后面看java,关于引用也是模模糊糊,这个东西也确实比较抽象,估计好多写了好几年代码的人有也有些迷惑 ...