ShardingSphere-ElasticJob踩的坑 截至3.0.1版本

ShardingSphere-ElasticJob踩的坑 截至3.0.1版本ElasticJobEasticJob原先是属于当当的,在2017年的时候已经停止了维护,最近在2021年,又重新开启了维护,这篇文章主要是写重启维护后,所带来的一些更改,以及所采的一些坑。1.官网地址https://shardingsphere.apache.org/elasticjob/current/cn/overview/https://github.com/apache/shardingsphere-elasticjob2.新版elastic-job平台的部署方法安装目录是/usr/

ElasticJob

EasticJob原先是属于当当的,在2017 年的时候已经停止了维护,最近在2021年,又重新开启了维护,这篇文章主要是写重启维护后,所带来的一些更改,以及所采的一些坑。(2022年,新爆了一些漏洞,elastic-job平台已经更新到3.0.1版本了)

1.官网地址
https://shardingsphere.apache.org/elasticjob/current/cn/overview/
https://github.com/apache/shardingsphere-elasticjob

2.新版elastic-job平台的部署方法

安装目录是/usr/local/soft/elasticjob(请下载3.0.1版本)
wget https://downloads.apache.org/shardingsphere/elasticjob-ui-3.0.1/apache-shardingsphere-elasticjob-3.0.1-lite-ui-bin.tar.gz;
tar -zxvf apache-shardingsphere-elasticjob-3.0.1-lite-ui-bin.tar.gz;
mv  apache-shardingsphere-elasticjob-3.0.1-lite-ui-bin elasticjob;
cd  /usr/local/soft/elasticjob/elasticjob/conf;
修改application.properties文件的端口和账号上传mysql-connector-java-8.0.28.jar 到/usr/local/soft/elasticjob/elasticjob/lib;
没这一步在3.0.0版本的ui 将无法写入mysql的数据源cd  /usr/local/soft/elasticjob/elasticjob/bin;
./start.sh;
http://192.168.16.147:5073/#/login  测试登录

在这里插入图片描述
3.新版Elastic-job 需要 maven 版本3.5.0 以上 ;java8 以上;zookeeper 版本3.6.0 以上(java 代码里接入的zk和平台里的zk都要3.6.0 版本以上)

4. 代码的集成
项目结构图
在这里插入图片描述
pom文件

<properties>
    <java.version>1.8</java.version>
    <curator.version>5.1.0</curator.version>
    <elasticjob.version>3.0.1</elasticjob.version>
</properties>
<dependencies>
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    <!--elasticjob-邮件通知-->
    <dependency>
        <groupId>org.apache.shardingsphere.elasticjob</groupId>
        <artifactId>elasticjob-error-handler-email</artifactId>
        <version>${ 
   elasticjob.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.shardingsphere.elasticjob</groupId>
        <artifactId>elasticjob-lite-spring-namespace</artifactId>
        <version>${ 
   elasticjob.version}</version>
    </dependency>
    <!--事件追踪-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.2.0</version>
    </dependency>
    <!--h2 数据库-->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.196</version>
    </dependency>


    <dependency>
        <groupId>org.apache.shardingsphere.elasticjob</groupId>
        <artifactId>elasticjob-lite-core</artifactId>
        <version>${ 
   elasticjob.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>curator-client</artifactId>
                <groupId>org.apache.curator</groupId>
            </exclusion>
            <exclusion>
                <artifactId>curator-framework</artifactId>
                <groupId>org.apache.curator</groupId>
            </exclusion>
            <exclusion>
                <artifactId>curator-recipes</artifactId>
                <groupId>org.apache.curator</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <artifactId>curator-client</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>zookeeper</artifactId>
                <groupId>org.apache.zookeeper</groupId>
            </exclusion>
        </exclusions>
        <groupId>org.apache.curator</groupId>
        <version>${ 
   curator.version}</version>
    </dependency>

    <dependency>
        <artifactId>zookeeper</artifactId>
        <groupId>org.apache.zookeeper</groupId>
        <version>3.6.0</version>
    </dependency>
    <dependency>
        <artifactId>curator-recipes</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>curator-framework</artifactId>
                <groupId>org.apache.curator</groupId>
            </exclusion>
        </exclusions>
        <groupId>org.apache.curator</groupId>
        <version>${ 
   curator.version}</version>
    </dependency>
    <dependency>
        <artifactId>curator-framework</artifactId>
        <groupId>org.apache.curator</groupId>
        <version>${ 
   curator.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>log4j-core</artifactId>
                <groupId>org.apache.logging.log4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>log4j-jul</artifactId>
                <groupId>org.apache.logging.log4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>log4j-slf4j-impl</artifactId>
                <groupId>org.apache.logging.log4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <artifactId>log4j-core</artifactId>
        <groupId>org.apache.logging.log4j</groupId>
    </dependency>
    <dependency>
        <artifactId>log4j-jul</artifactId>
        <groupId>org.apache.logging.log4j</groupId>
    </dependency>
    <dependency>
        <artifactId>log4j-api</artifactId>
        <groupId>org.apache.logging.log4j</groupId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>
</dependencies>

注意一点:elasticjob 的版本需要是3.0.0 版本,3.0.1 版本我这边是启动的起来,但是Job平台那显示异常,报错如下

Unable to find property 'timeZone' on class: org.apache.shardingsphere.elasticjob.infra.pojo.JobConfi

Job.xml 文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:elasticjob="http://shardingsphere.apache.org/schema/elasticjob"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://shardingsphere.apache.org/schema/elasticjob
                        http://shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
                        ">
  <import resource="base-job.xml"></import>


    <!-- 配置作业 Bean -->
    <bean id="simpleJob6" class="com.top.elasticjob.job.TestJob">
    </bean>


    <elasticjob:job id="ceshi" job-ref="simpleJob6" registry-center-ref="regCenter" tracing-ref="elasticJobTrace" sharding-total-count="2" cron="0 0/5 * * * ? " sharding-item-parameters="0=Beijing,1=Shanghai,2=Guangzhou" monitor-execution="false" failover="true" description="测试Job是否可用" disabled="false" overwrite="true" job-sharding-strategy-type="AVG_ALLOCATION" job-error-handler-type="EMAIL" job-listener-types="myJobListener">
        <props>
            <prop key="email.host">smtp.qq.com</prop>
            <prop key="email.port">25</prop>
            <prop key="email.username">22222222@qq.com</prop>
            <prop key="email.password">ufuvhzyogrcxchbf</prop>
            <prop key="email.useSsl">false</prop>
            <prop key="email.subject">错误</prop>
            <prop key="email.from">22222222@qq.com</prop>
            <prop key="email.to">11111111@qq.com</prop>
<!--            <prop key="email.cc">${ 
   cc}</prop>-->
<!--            <prop key="email.bcc">${ 
   bcc}</prop>-->
            <prop key="email.debug">false</prop>
        </props>
    </elasticjob:job>



</beans>

base-job.xml的配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:elasticjob="http://shardingsphere.apache.org/schema/elasticjob"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://shardingsphere.apache.org/schema/elasticjob
                        http://shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
                        ">
    <!--配置作业注册中心 -->
    <elasticjob:zookeeper id="regCenter" server-lists="192.168.16.147:2181" namespace="top"
                          base-sleep-time-milliseconds="3000"
                          max-sleep-time-milliseconds="3000" max-retries="3"  digest="top123"/>

    <bean id="tracingDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:tcp://192.168.16.147:19200/~/youdb" />
        <property name="username" value="h2" />
        <property name="password" value="h2" />
    </bean>

 <bean id="tracingDataSourceMysql" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://192.168.16.147:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <!--配置事件追踪-->
    <elasticjob:rdb-tracing id="elasticJobTrace" data-source-ref="tracingDataSourceMysql" />

</beans>

作业监听器的代码

package com.top.elasticjob.joblistener;

import org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener;
import org.apache.shardingsphere.elasticjob.infra.listener.ShardingContexts;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/** * CH常规监听器 */
public class MyJobListener implements ElasticJobListener { 
   


    @Override
    public void beforeJobExecuted(ShardingContexts shardingContexts) { 
   
             System.out.println("任务执行前");
    }

    @Override
    public void afterJobExecuted(ShardingContexts shardingContexts) { 
   
        System.out.println("任务执行后");
    }

    @Override
    public String getType() { 
   
        //这个名称需要和配置文件的对应
        return "myJobListener";
    }
}

简单Job的代码

package com.top.elasticjob.job;

import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;

public class TestJob implements SimpleJob { 
   

    @Override
    public void execute(ShardingContext shardingContext) { 
   
        int a0=0;
        System.out.println(2222222);
    }
}

说明:
事件追踪:

我这边以mysql 为数据库来做Job的事件追踪的话,数据库的内容录入不进去,java的Job启动和Elastic-job 平台启动都正常,但是数据录入不进去,所以这边采用的是H2 数据库,配置里面 的tracing-ref=“elasticJobTrace” 就是事件追踪的配置核心

<!--事件追踪-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.2.0</version>
</dependency>
<!--h2 数据库-->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.196</version>
</dependency>
<!-mysql 数据库 和h2 数据库选一个就行->
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>

错误处理策略:

 job-error-handler-type="EMAIL"
  <props>
            <prop key="email.host">smtp.qq.com</prop>
            <prop key="email.port">25</prop>
            <prop key="email.username">22222222@qq.com</prop>
            <prop key="email.password">ufuvhzyogrcxchbf</prop>
            <prop key="email.useSsl">false</prop>
            <prop key="email.subject">错误</prop>
            <prop key="email.from">22222222@qq.com</prop>
            <prop key="email.to">11111111@qq.com</prop>
<!--            <prop key="email.cc">${ 
   cc}</prop>-->
<!--            <prop key="email.bcc">${ 
   bcc}</prop>-->
            <prop key="email.debug">false</prop>
        </props>

这些配置都是配置Email 策略的,当Job 运行出错时,任务不中断,发送邮件通知
email.password 的密码 是下图开启SMTP 时所给的授权码
在这里插入图片描述
作业监听器

job-listener-types="myJobListener"

这个属性就是配置作业监听器的地方,值需要和写作业监听器类里面的getType 的返回值一样;加载作业监听器是通过SPI的方式加载的,需要在resources 下配置
在这里插入图片描述
文件的内容就是所写的作业监听器MyJobListener类的地址
新版的Elastic-job相比于旧版的Job来说,功能都差不多,但是所支持的zk版本更高。本例中需要自行安装zookeeper注册中心和h2数据库或者mysql 数据库,ElasticJob的其余内容和配置 相对简单,查看官网即可,需要源码的可以关注微信公众号(小白逆袭之路),回复 elastic-job。

今天的文章ShardingSphere-ElasticJob踩的坑 截至3.0.1版本分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30414.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注