SpringBoot使用Elastic-Job
本文介绍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的更多相关文章
- SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践
一. 前言 日志对于一个程序的重要程度不用过多的言语修饰,本篇将以实战的方式讲述开源微服务全栈项目 有来商城 是如何整合当下主流日志解决方案 ELK +Filebeat . 话不多说,先看实现的效果图 ...
- Spring Security OAuth2 微服务认证中心自定义授权模式扩展以及常见登录认证场景下的应用实战
一. 前言 [APP 移动端]Spring Security OAuth2 手机短信验证码模式 [微信小程序]Spring Security OAuth2 微信授权模式 [管理系统]Spring Se ...
- SpringBoot 快速集成 Elastic Job
一.引入依赖 <dependency> <groupId>com.github.kuhn-he</groupId> <artifactId>elasti ...
- SpringBoot使用ELK日志收集ELASTIC (ELK) STACK
1:资源 # 文档向导 # logstash https://www.elastic.co/guide/en/logstash/current/index.html #kibana https://w ...
- Elastic Job入门(3) - 集成Springboot
引入pom文件 <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job ...
- ELK学习笔记(四)SpringBoot+Logback+Redis+ELK实例
废话不多说,直接上干货,首先看下整体应用的大致结构.(整个过程我用到了两台虚拟机 应用和Shipper 部署在192.168.25.128 上 Redis和ELK 部署在192.168.25.129 ...
- springboot elasticsearch 集成注意事项
文章来源: http://www.cnblogs.com/guozp/p/8686904.html 一 elasticsearch基础 这里假设各位已经简单了解过elasticsearch,并不对es ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- ELK入门使用-与springboot集成
前言 ELK官方的中文文档写的已经挺好了,为啥还要记录本文?因为我发现,我如果不写下来,过几天就忘记了,而再次捡起来必然还要经历资料查找筛选测试的过程.虽然这个过程很有意义,但并不总是有那么多时间去做 ...
- springboot~maven制作底层公用库
把一些公用方法,类型抽象到一个项目里,让其它项目依赖它,这种设计是一种解耦的体现,其实像springboot就是我们的一种依赖,他里面有很多子模块,用到哪个就添加哪个依赖即可,像redis,mongo ...
随机推荐
- XML配置spring session jdbc实现session共享
概述 session的基础知识就不再多说. 通常,我们会把一个项目部署到多个tomcat上,通过nginx进行负载均衡,提高系统的并发性.此时,就会存在一个问题.假如用户第一次访问tomcat1,并登 ...
- mysql—增删改查语句总结
关于MySQL数据库——增删改查语句集锦 一.基本的sql语句 CRUD操作: create 创建(添加) read 读取 update 修改 delete 删除 .添加数据 ,'n001','201 ...
- selenium定位方式-获取标签元素:find_element_by_xxx
定位方式取舍# 唯一定位方式.多属性定位.层级+角标定位(离目标元素越近,相对定位越好) # 推荐用css selector(很少用递进层次的定位)# 什么时候用xpath呢? 当你定位元素时,必须要 ...
- 2018-2019-2 网络对抗技术 20165337 Exp1 PC平台逆向破解(BOF实验)
实验内容 直接修改程序,跳转到getShell函数. 使用BOF攻击,覆盖返回地址,触发getShell函数. 注入一个自己的shellcode并运行. 任务一:直接修改程序,跳转到getShell函 ...
- kalilinux渗透测试笔记
声明:本文理论大部分是苑房弘kalilinux渗透测试的内容 第五章:基本工具 克隆网页,把gitbook的书记下载到本地 httrack "http://www.mybatis.org/m ...
- osgearth介绍
osgEarth为开发osg应用提供了一个地理空间SDK和地形引擎. osgEarth的目标: l 提供基于osg开发3D地理空间应用的支持; l 直接从数据源可视化地形模型和影像变得更加简单: l ...
- codeforces 813E 主席树
题意: 一个数列多组询问,每次询问[l,r]中最多能选多少个数字,其中每个数字的出现次数不超过k次 题解: 我们保存对于每个位置上,出现超过k次的位置,那么对于每次询问,我们就变成了查询区间[l,r] ...
- IIS 运行ASP.Net的基本配置(编辑中。。。)
今天在新建的IIS上运行Asp.net 程序,发现IIS根本没有走asp的路由系统,直接返回了404,后来发现是IIS没有正确安装,需要安装以下的组件: 未安装前,IIS里的样子: 安装后,IIS的样 ...
- 21 re正则模块 垃圾回收机制
垃圾回收机制 不能被程序访问到的数据,就称之为垃圾 引用计数 引用计数:用来记录值的内存地址被记录的次数的:当一个值的引用计数为0时,该值就会被系统的垃圾回收机制回收 每一次对值地址的引用都可以使该值 ...
- JAVA Scanner的简单运用
package Code428; import java.util.Scanner; /*Scanner 可以实现键盘输入数据 引用的步骤1.import 包路径.类名称只有java.lang包下的内 ...