【Elastic-job】实践汇总以及记录遇到的若干问题

2022-04-25  本文已影响0人  伊丽莎白2015

1. 版本问题

网上关于Elastic-job的文章有很多,如果初次上手Elastic-job,会发现有些文章依赖的groupId是com.dangdang,还有些甚至是程序员自己写的基于dangdang的jar包自己实现了一套spring boot。

【新闻】停滞数年后,ElasticJob 携首个 Apache 版本 3.0.0-alpha 回归!
https://baijiahao.baidu.com/s?id=1674705760276990789&wfr=spider&for=pc

基于上述文章,可以看到Elastic-job不再是dangdang网的项目了,而是apache shardingsphere的子项目,而3.0.0后的版本,是集成了Spring boot starter的,文档写的也更规范。

本文的实践就是基于shardingsphere-elasticjob 3.0的。

2. 资源链接

官网:https://shardingsphere.apache.org/elasticjob/

Github:https://github.com/apache/shardingsphere-elasticjob

3. 实践版本:

4. 其它实践

4.1 job失效转移:

我们都知道3.0.0版本后的Elastic-job支持在application.yaml中定义job,但如果我们使用以下代码在Controller中动态定义Job,HTTP API为/addJob:

JobConfiguration jobConfig = JobConfiguration.newBuilder("myJob", 3)
.cron("0/5 * * * * ?")
.shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou")
.build();

假设这个Controller的项目,布署在3个节点上,instance01:8080, instance02:8080, instance03:8080。
我们在调用http://instance01:8080/addJob后,myJob确实会在instance01节点开始run,但如果此时instance01故障了,myJob默认情况下,并没有自动转移到instance02以及03上。
【实践5】想要job故障转移,必须在节点中先schedule,(zookeeper并不会帮忙做这块),所以需要在instance02、instance03上都调用过/addJob,那么job就会失效转移了。

4.2 jobListener

创建一个jobListener参考:https://shardingsphere.apache.org/elasticjob/current/cn/user-manual/elasticjob-lite/usage/job-listener/listener-interface/
文档上有说:

添加SPI实现
将JobListener实现添加至infra-common下resources/META-INF/services/org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener

【实践6】自己在实现的时候,忽略了上述配置,需要在resources下新建目录METE-INF/servises
新建文件:org.apache.shardingsphere.elasticjob.infra.listener
内容(自定义的Listener的类全名):xxx.xxx.MyJobListener

4.3 关于job-error-hander-type=EMAIL的配置:
email configuration
以上图是PDF中截取,我也是参考这个配置来运行我的alert,但发现会提示到不到email.host,【实践7】props应该是具体某个job的配置,不应该和jobs对齐,而应该和上述的jobErrorHandlerType对齐,下面是我的配置:
elasticJob:
  regCenter:
    ...
  jobs:
    my-simple-job:
        ...
        jobErrorHandlerType: EMAIL
        props:
          email:
            host: host
            ...
4.4 官方Q&A:

官网上列了14个问题以及解决方案,有可能在实践过程中真的会遇到:
https://shardingsphere.apache.org/elasticjob/current/cn/faq/

上一篇 下一篇

猜你喜欢

热点阅读