栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot使用Elastic-Job

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot使用Elastic-Job

本文介绍SpringBoot整合Elastic-Job分布式调度任务(简单任务)。

1.有关Elastic-Job

Elastic-Job是当当网开源的分布式任务调度解决方案,是业内使用较多的分布式调度解决方案。

这里主要介绍Elastic-Job-Lite,Elastic-Job-Lite定位为轻量级无中心化解决方案,使用jar包的形式提供最轻量级的分布式任务的协调服务,外部依赖仅Zookeeper。

架构图如下:

Elastic-Job官网地址:http://elasticjob.io/index_zh.html
Elastic-Job-Lite官方文档地址:http://elasticjob.io/docs/elastic-job-lite/00-overview/intro/

2.使用Elastic-Job 2.1 加入依赖

新建项目,在项目中加入Elastic-Job依赖,完整pom如代码清单所示。



    4.0.0
    
 org.springframework.boot
 spring-boot-starter-parent
 2.0.3.RELEASE
  
    
    com.dalaoyang
    springboot2_elasticjob
    0.0.1-SNAPSHOT
    springboot2_elasticjob
    springboot2_elasticjob

    
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-web
 
 
     org.springframework.boot
     spring-boot-starter-test
     test
 
 
     com.dangdang
     elastic-job-lite-core
     2.1.5
 
 
     com.dangdang
     elastic-job-lite-spring
     2.1.5
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
 
    



2.2 配置文件

配置文件中需要配置一下zookeeper地址和namespace名称,注意:这个不是必须要配置的,在文件中直接写死也是可以的,配置文件如下所示。

spring.application.name=springboot2_elasticjob

regCenter.serverList=localhost:2181
regCenter.namespace=springboot2_elasticjob
2.3 配置zookeeper

接下来需要配置一下zookeeper,创建一个JobRegistryCenterConfig,内容如下:

@Configuration
@ConditionalOnexpression("'${regCenter.serverList}'.length() > 0")
public class JobRegistryCenterConfig {

    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList,
   @Value("${regCenter.namespace}") final String namespace) {
 return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
    }

}
2.4 定义Elastic-Job任务

配置一个简单的任务,这里以在日志中打印一些参数为例,如下所示。

public class MySimpleJob implements SimpleJob {
    Logger logger = LoggerFactory.getLogger(MySimpleJob.class);

    @Override
    public void execute(ShardingContext shardingContext) {
 logger.info(String.format("Thread ID: %s, 作业分片总数: %s, " +
   "当前分片项: %s.当前参数: %s," +
   "作业名称: %s.作业自定义参数: %s"
  ,
  Thread.currentThread().getId(),
  shardingContext.getShardingTotalCount(),
  shardingContext.getShardingItem(),
  shardingContext.getShardingParameter(),
  shardingContext.getJobName(),
  shardingContext.getJobParameter()
 ));

    }
}
2.5 配置任务

配置任务的时候,这里定义了四个参数,分别是:

  • cron:cron表达式,用于控制作业触发时间。
  • shardingTotalCount:作业分片总数
  • shardingItemParameters:分片序列号和参数用等号分隔,多个键值对用逗号分隔
    分片序列号从0开始,不可大于或等于作业分片总数
    如:
    0=a,1=b,2=c
  • jobParameters:作业自定义参数
    作业自定义参数,可通过传递该参数为作业调度的业务方法传参,用于实现带参数的作业
    例:每次获取的数据量、作业实例从数据库读取的主键等。

至于其他参数请参考文档,http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/

本文配置如下:

@Configuration
public class MyJobConfig {

    private final String cron = "0/5 * * * * ?";
    private final int shardingTotalCount = 3;
    private final String shardingItemParameters = "0=A,1=B,2=C";
    private final String jobParameters = "parameter";

    @Autowired
    private ZookeeperRegistryCenter regCenter;

    @Bean
    public SimpleJob stockJob() {
 return new MySimpleJob();
    }

    @Bean(initMethod = "init")
    public JobScheduler simpleJobScheduler(final SimpleJob simpleJob) {
 return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(),
  cron, shardingTotalCount, shardingItemParameters, jobParameters));
    }

    private LiteJobConfiguration getLiteJobConfiguration(final Class jobClass,
 final String cron,
 final int shardingTotalCount,
 final String shardingItemParameters,
 final String jobParameters) {
 // 定义作业核心配置
 JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).
  shardingItemParameters(shardingItemParameters).jobParameter(jobParameters).build();
 // 定义SIMPLE类型配置
 SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName());
 // 定义Lite作业根配置
 LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
 return simpleJobRootConfig;

    }
}
3.测试

启动项目,就可以看到控制台的输出了,如下所示:

4.源码

源码地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_elasticjob

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号