zoukankan      html  css  js  c++  java
  • elasticjob学习二:封装elasticjob-spring-boot-starter

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

  • 相关阅读:
    算法第四章上机实践报告
    算法第三章作业
    算法第三章上机实践报告
    算法第二章总结
    关于stl::sort--算法第二章作业
    算法第二章上机实践报告
    算法第一章作业
    1
    2020-2021-1 20209302毕慧敏《Linux内核原理与分析》第十二周作业
    2020-2021-1 20209302毕慧敏《Linux内核原理与分析》第十一周作业
  • 原文地址:https://www.cnblogs.com/zhenghengbin/p/12474490.html
Copyright © 2011-2022 走看看