惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
博客园 - Franky
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)

博客园 - 漂泊雪狼

关于SeaTunnel 达梦数据迁移无法自动建表的问题 Kettel链接达梦数据 记一次Spring boot集成mybatis错误修复过程 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Spring Boot 框架下使用MyBatis访问数据库之基于XML配置的方式 CentOS7下载配置PostgreSQL的pgAgent运行代理作业 ElasticSearch 中文分词搜索环境搭建 学习笔记——Ubuntu下使用Docker包部署禅道任务管理系统 windows下的Redis主从集群搭建 asp.net mvc5中使用Swagger 自动生成WebApi文档笔记 Centos 7.5下搭建SVN源代码服务器 使用Fiddler 4 调用WebService 获取geometry边界范围的示例代码 使用pgrouting进行最短路径搜索 Nginx设置防止IP及非配置域名访问 java 调用c# web api 代码 一台机器部署多个tomcat服务 nginx反向代理多个服务 笔记 利用Kettle 从Excel中抽取数据写入SQLite sql server 统计信息 sql server 索引碎片相关问题
Elastic Job 入门笔记
漂泊雪狼 · 2021-03-30 · via 博客园 - 漂泊雪狼

Elastic Job 3.0版本还在完善中,坑比较多,此文使用2.1.5作为测试

1、安装Zookeeper

关键命令

cd /opt
# 解压压缩包,解压完成后当前目录生成一个apache-zookeeper-3.6.1-bin的文件夹
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz
# 修改文件夹名称为zookeeper-3.6.1(好记)
mv apache-zookeeper-3.6.1-bin  zookeeper-3.6.1


cd /opt/zookeeper-3.6.1/conf
# 复制一份配置文件,名为zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 修改配置文件 
vim zoo.cfg
# 所有参数默认即可,在最后加上
admin.serverPort=2190


vim /etc/profile
# 替换为自己的安装目录
export ZOOKEEPER_HOME=/opt/zookeeper-3.6.1 
# 我安装的东西较多,参考截图红框设置
export PATH=$PATH:$ZOOKEEPER_HOME/bin 


# 启动(未设置环境变量可以cd到安装目录/bin下面)
zkServer.sh start
# 查看是否启动成功,也可以在安装目录/logs/下查看启动日志
ps -ef|grep zookeeper

参考

2、Elastic-job环境搭建

下载源码,分支为2.1.5 https://codechina.csdn.net/mirrors/dangdangdotcom/elastic-job?utm_source=csdn_github_accelerator

执行

mvn clean package -Dmaven.test.skip=true

  

进行源码编译打包,几分钟后即可全部编译完成

进入elastic-job-lite-console-2.1.5\bin windows下执行 start.bat

启动成功,默认端口8899

浏览器登录后添加注册中心配置


2、新建一个Spring Boot项目,pom文件加上如下配置

   <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.1.18.RELEASE</spring-boot.version>
        <elasticjob.version>2.1.5</elasticjob.version>
    </properties>


        <!-- 引入elastic-job-lite核心模块 -->
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-lite-core</artifactId>
            <version>${elasticjob.version}</version>
        </dependency>

        <!-- 使用springframework自定义命名空间时引入 -->
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-lite-spring</artifactId>
            <version>${elasticjob.version}</version>
        </dependency>

  3、新建一个简单Job类

public class TestJob implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
        int item = shardingContext.getShardingItem();

        System.out.println(String.format("-----ThreadId:%s,当前分片项:%s",Thread.currentThread().getId(),item));
    }
}

4、自定义bean的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:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd
                        ">
    <!--配置作业注册中心 -->
    <reg:zookeeper id="jobRegesterCenter" server-lists="192.168.0.9:2181" namespace="lfp-elastic-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />

    <!-- 配置作业-->
    <job:simple id="testJob" class="cn.wison.elasticjobdemo.jobs.TestJob" registry-center-ref="jobRegesterCenter" cron="* * * * * ?"
                disabled="false" overwrite="true" sharding-total-count="4" sharding-item-parameters="0=A,1=B,2=C" description="测试" />
</beans>

5、修改启动Main函数

@SpringBootApplication
@ImportResource(locations={"classpath:jobs.xml"})
public class ElasticJobDemoApplication {

 6、启动程序执行可以看到分片执行效果