之前已经简单的学习了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. 推荐几个树莓派 raspbian 系统 可用的 arm docker源

    树莓派刚到手各种尝鲜,试过不少系统,最后还是回归了raspbian os 系统 root@raspberrypi:/home/pi# cat /proc/version Linux version 4 ...

  2. elasticsearch5.4安装

    1.从官网下载ES 安装包: elasticsearch-.tar.gz 2.解压到要安装的目录 注意:一定要切换用户,不能用root用户解压,不能用root用户启动 tar -zxvf elasti ...

  3. 蓝桥杯-PREV45-图形排版

    这是2017年蓝桥杯C组C++的压轴题,拿到之后没什么想法.但是蓝桥杯有部分分.所以直接敲了个大暴力提交上去过了一半的数据.后来想到了DP,但是没能实现出来,感觉还是有问题的.后来看了解题视频发现是预 ...

  4. [LC] 240. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. 89)PHP,跳转练习(1)

    首先是需要两个简单的文件: 目录关系是: 我的beifen.php代码展示,这个先执行: <?php header('Location:b.php'); b.php代码展示: <?php ...

  6. spring给予XML配置的声明式事务

    步骤: 1.添加aop.tx命名空间声明: 2.配置事务管理器: 3.配置增强: 4.配置aop 具体xml设置如下: <?xml version="1.0" encodin ...

  7. 关于使用gitlab协同开发提交代码步骤

    记录使用gitlab协同开发时从自己的分支向master分支提交代码的步骤: 环境:安装了git和TortoiseGit(git的可视化工具) 1.首先切换到自己的分支(如果不在自己的分支) 2.gi ...

  8. 吴裕雄--天生自然 R语言开发学习:基本统计分析

    #---------------------------------------------------------------------# # R in Action (2nd ed): Chap ...

  9. MySQL数据类型(DATA Type)与数据恢复与备份方法

    一.数据类型(DATA Type)概述 MySQL支持多种类型的SQL数据类型:数字类型,日期和时间类型,字符串(字符和字节)类型以及空间类型 数据类型描述使用以下约定: M表示整数类型的最大显示宽度 ...

  10. 自研接口测试平台(Django2+Bootstrap3+Unittest)

    自研接口测试平台(Django2+Bootstrap3+Unittest) HttpRuleTest是一款面向 HTTP 协议的通用测试框架,只需web端维护接口测试数据,即可实现自动化测试.监控.数 ...