之前已经简单的学习了es-job.但是如果实际应用都如同第一篇进行编写,会有很多重复代码,不方便。这篇主要是进行封装。我还会用一个demo使用下封装好的组件。

elasticjob-spring-boot-starter 封装

  • 1.pom文件
    <properties>
<elastic-job.version>2.1.4</elastic-job.version>
</properties>
<dependencies>
<!-- spring boot dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- elastic-job dependency -->
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
</dependencies>
  • 2.配置JobParserAutoConfiguration类
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "elastic.job.zk",name = {"namespace","serverLists"},matchIfMissing = false)
@EnableConfigurationProperties(JobZookeeperProperties.class)
public class JobParserAutoConfiguration { @Bean(initMethod = "init")
public ZookeeperRegistryCenter zookeeperRegistryCenter(JobZookeeperProperties jobZookeeperProperties) {
ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(jobZookeeperProperties.getServerLists(),
jobZookeeperProperties.getNamespace());
zkConfig.setBaseSleepTimeMilliseconds(zkConfig.getBaseSleepTimeMilliseconds());
zkConfig.setMaxSleepTimeMilliseconds(zkConfig.getMaxSleepTimeMilliseconds());
zkConfig.setConnectionTimeoutMilliseconds(zkConfig.getConnectionTimeoutMilliseconds());
zkConfig.setSessionTimeoutMilliseconds(zkConfig.getSessionTimeoutMilliseconds());
zkConfig.setMaxRetries(zkConfig.getMaxRetries());
zkConfig.setDigest(zkConfig.getDigest());
log.info("初始化job注册中心配置成功, zkaddress : {}, namespace : {}", jobZookeeperProperties.getServerLists(), jobZookeeperProperties.getNamespace());
return new ZookeeperRegistryCenter(zkConfig);
} @Bean
public ElasticJobConfParser elasticJobConfParser(JobZookeeperProperties jobZookeeperProperties, ZookeeperRegistryCenter zookeeperRegistryCenter) {
return new ElasticJobConfParser(jobZookeeperProperties, zookeeperRegistryCenter);
} }
  • 3.META-INF文件下设置自动引入JobParserAutoConfiguration
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.kevin.task.autoconfigure.JobParserAutoConfiguration
  • 4.定义2个自定义注解,EnableElasticJob 和ElasticJobConfig

  • 5.解析自定义注解ElasticJobConfig

完整代码请看:https://github.com/FunCodingOfWe/elasticjob-spring-boot-starter

elasticjob-spring-boot-starter 的使用

  • 1.下载该项目,编译install
mvn install

  • 2.新项目中引入依赖
        <dependency>
<groupId>com.kevin</groupId>
<artifactId>elasticjob-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
  • 3.开启elasticjob @EnableElasticJob

@EnableElasticJob
@SpringBootApplication
public class EsJobApplication { public static void main(String[] args) {
SpringApplication.run(EsJobApplication.class, args);
} }
  • 4.配置对应的job
@Component
@ElasticJobConfig(
name = "com.kevin.task.task.MySimpleJob",
cron = "0/5 * * * * ?",
description = "测试简单任务",
overwrite = true,
eventTraceRdbDataSource = "dataSource",
shardingTotalCount = 2
)
public class MySimpleJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
System.out.println("执行mysimpleJob====");
}
}
  • 5.配置zk地址和namespace

elastic:
job:
zk:
namespace: elastic-job
serverLists: 127.0.0.1:2181

示例代码:https://github.com/FunCodingOfWe/es-job

总结

通过对es-job的封装,更加简化了我们的工作,从而更加容易的使用es-job

elasticjob学习二:封装elasticjob-spring-boot-starter的更多相关文章

  1. 学习Spring Boot:(二十三)Spring Boot 中使用 Docker

    前言 简单的学习下怎么在 Spring Boot 中使用 Docker 进行构建,发布一个镜像,现在我们通过远程的 docker api 构建镜像,运行容器,发布镜像等操作. 这里只介绍两种方式: 远 ...

  2. 年轻人的第一个自定义 Spring Boot Starter!

    陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...

  3. 手把手教你手写一个最简单的 Spring Boot Starter

    欢迎关注微信公众号:「Java之言」技术文章持续更新,请持续关注...... 第一时间学习最新技术文章 领取最新技术学习资料视频 最新互联网资讯和面试经验 何为 Starter ? 想必大家都使用过 ...

  4. 深入学习微框架:Spring Boot(转)

    转:http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ 相关参考: https://spring.io/guides/gs/s ...

  5. 自己写spring boot starter

    自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...

  6. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

  7. 从零开始开发一个Spring Boot Starter

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

  8. 最详细的自定义Spring Boot Starter开发教程

    1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...

  9. Spring Boot Starter 介绍

    http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

  10. spring -boot s-tarter 详解

    Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...

随机推荐

  1. 104)PHP,目录树状输出

    使用特定数量的缩进达到树状目的! 核心问题,计算需要缩进的数量! 缩进级别,与递归调用深度保持一致.每当执行一级递归操作,所找到的文件的缩进级别+; 语法实现: 增加一个参数,表示当前函数调用的深度级 ...

  2. KUKA机器人常见十大问题及解决方法

    1 开机坐标系无效 世界坐标系是以枪头为基点,在这种坐标系中,机器人所有的动作都是按照以枪头为顶点来完成移动,XYZ方向切割枪方向不改变,如果机器人在世界坐标系中移动,枪头也随着改变方向,那就是我们在 ...

  3. 转载【docker】CMD ENTRYPOINT 的使用方法

    原文:https://blog.csdn.net/u010900754/article/details/78526443

  4. HihoCode-1323-回文字符串

    参考博客: https://blog.csdn.net/mitsuha_/article/details/76690634 https://blog.csdn.net/u014142379/artic ...

  5. HDU-2544-最短路(各种最短路径算法)

    迪杰斯特拉算法--O(n^2) #include"iostream" #include"cstring" #include"cstdio" ...

  6. Monkey命令参数介绍

    1) 参数: -p   参数-p用于约束限制,用此参数指定一个或多个包(Package,即App).指定   包之后,Monkey将只允许系统启动指定的APP.如果不指定包,Monkey将允许系统启动 ...

  7. SpringMVC在使用过程中的错误

    HTTP Status 500 - Request processing failed; nested exception is org.springframework.validation.Bind ...

  8. Java volatile修饰字段

     一.关键字volatile修饰字段: 使用特殊域变量(volatile)实现线程同步 volatile:不稳定的:反复无常的:易挥发的: 1.volatile关键字为域变量的访问提供了一种免锁机制, ...

  9. 查询优化基础知识 - chendh blog

    概述 处理一个给定的查询,尤其是复杂查询,通常会有许多种策略,查询优化就是从这许多策略中找出最有效的查询执行计划的处理过程. 查询执行计划的步骤 产生逻辑上与给定表达式等价的表达式: 估计每个执行计划 ...

  10. 15.uboot study 串口初始化

    3. 串口初始化 4. 代码实现 关于串口 对于嵌入式设备的开发,刚开始好多设备都无法使用,由于无法获得程序的运行状态,调试程序需要花费好多时间和精力,因此串口对于嵌入式程序的调试的作用显而易见,当串 ...