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 ...
随机推荐
- 【Unity编辑器】UnityEditor多重弹出窗体与编辑器窗口层级管理
一.简介 最近马三为公司开发了一款触发器编辑器,对于这个编辑器策划所要求的质量很高,是模仿暴雪的那个触发器编辑器来做的,而且之后这款编辑器要作为公司内部的一个通用工具链使用.其实,在这款触发器编辑器之 ...
- ASP.NET-SHA256加密方法
/// <summary> /// SHA256加密 /// </summary> /// <param name="strData">< ...
- Netty学习路线
预研时间170517-170519 投入时间:约10h 理解度:入门①前置基础:了解基本网络协议和通信方式[图解HTTP]http://download.csdn.net/detail/niehanm ...
- API设计中响应数据格式用json的优点
通常我们再设计api返回时,都使用json格式返回,相比xml,他又有什么优点呢? 更直观易懂 占用空间更小 能与JavaScript跟好的兼容.js通过eval()进行Json读取. 支持多种语言. ...
- Git分支实战入门详细图解
现在我们模拟一个简单的分支和合并案例,其中工作流可供真实项目借鉴. (1)在master开展工作 (2)为新的需求创建分支 (3)在新的分支上展开工作 这时,你接到一个电话,说项目有一个严重的问题需要 ...
- CDH5.16.1的maven依赖版本查询地址
1查询官网地址,提供了详细的各个版本的jar依赖版本信息 https://www.cloudera.com/documentation/enterprise/release-notes/topics/ ...
- mysql并发控制之MVCC
1.MVCC(Multiversion concurrency control) :多版本并发控制,当我们并发访问数据库(读或写)时,对事物内正在处理的数据做多版本控制,用以防止写操作的阻塞影响读操作 ...
- Q查询条件
e. Q查询 ``` def search(self, query_list): query = self.request.GET.get('query', '') # 获取query的值 # Q(Q ...
- 前后端分离--ajaxUpload异步上传文件成功,前端获取数据却失败的解决方案
转载:https://blog.csdn.net/baidu_32809053/article/details/78709951
- Shell-仅保留最近3天的备份文件: find . -name "*.sql" -mtime +3 -ls -exec rm {} \;
Code: find . -name "*.sql" -mtime +3 -ls -exec rm {} \;