elasticjob学习二:封装elasticjob-spring-boot-starter
Posted Kevin-养码青年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于elasticjob学习二:封装elasticjob-spring-boot-starter的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot-ElasticJob封装快速上手使用(分布式定时器)
elasticjob学习一:simplejob初识和springboot整合