码迷,mamicode.com
首页 > 编程语言 > 详细

elasticjob学习二:封装elasticjob-spring-boot-starter

时间:2020-03-12 19:06:42      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:turn   processor   ica   http   地址   ref   ext   job   mem   

之前已经简单的学习了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

标签:turn   processor   ica   http   地址   ref   ext   job   mem   

原文地址:https://www.cnblogs.com/zhenghengbin/p/12474490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!