美文网首页
elastic-job搭建

elastic-job搭建

作者: engineer_tang | 来源:发表于2022-10-01 13:44 被阅读0次

1. 安装zookeeper

下载zookeeper

## 1.1 下载zookeeper
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz

# 解压文件
tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz

1.2 配置zookeetper

mv apache-zookeeper-3.5.5-bin zookeeper
mkdir /opt/zookeeper/{dataDir,dataLogDir}
cd /opt/zookeeper/conf/
cp zoo_sample.cfg zoo.cfg 
image.png

配置完成保存并退出

1.3 启动zookeeper

#启动命令:
./bin/zkServer.sh start
#在前端启动,可看启动日志
./bin/zkServer.sh start-foreground
#停止命令:
./bin/zkServer.sh stop  

#重启命令:
./bin/zkServer.sh restart

#状态查看命令:
./bin/zkServer.sh status

2. 运行elastic-job的UI包

2.1 下载包

# 下载UI包
wget https://dlcdn.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

运行后,访问地址:http://192.168.31.128:8088/#/login ,默认账号密码都是root

image.png

2.2 配置注册中心

image.png
image.png

2.3 修改任务触发规则

image.png

3. 项目中接入elasticjob

3.1 在项目中的pom引入依赖:

        <dependency>
            <groupId>org.apache.shardingsphere.elasticjob</groupId>
            <artifactId>elasticjob-lite-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <!-- 自行修改相应版本-->
            <version>3.0.2</version>
        </dependency>

3.2 配置文件增加elasticjob配置

elasticjob:
  regCenter:
    serverLists: 192.168.31.128:2181
    namespace: spring-cloud-alibaba-job
    baseSleepTimeMilliseconds: 2000
    maxSleepTimeMilliseconds: 4000
    maxRetries: 3
  jobs:
    elasticMessageJob:
      elasticJobClass: com.joe.strawberry.job.component.ElasticMessageJob
      cron: 0/1 * * * * ?
      shardingTotalCount: 1
      overwrite: true
      failover: true

  tracing:
    type: RDB

3.3 增加任务

package com.joe.strawberry.job.component;


import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ElasticMessageJob implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("#############");
        log.info("测试的定时任务执行了");
    }
}

相关文章

网友评论

      本文标题:elastic-job搭建

      本文链接:https://www.haomeiwen.com/subject/himjartx.html