elasticjob 教程

ElasticJob环境要求

  • Java 8+
  • Maven 3.5.0+
  • ZooKeeper 3.6.0+

ElasticJob-Lite

  1. 提前安装好 ZooKeeper
  2. 新建一个 spring boot 项目,版本选择 2.7.7
  3. 引入 elasticjob-lite-spring-boot-starter依赖
	<dependency>
        <groupId>org.apache.shardingsphere.elasticjob</groupId>
        <artifactId>elasticjob-lite-spring-boot-starter</artifactId>
        <version>3.0.2</version>
	</dependency>
  1. 编写一个 MyJob 类并继承 SimpleJob
@Component
public class MyJob implements SimpleJob {
    @Override
    public void execute(ShardingContext shardingContext) {
        System.out.println("SimpleJob:MyJob result is: xxx");
    }
}
  1. 按要求修改配置文件 application.yaml serverLists 表示 ZooKeeper 的地址
elasticjob:
  regCenter:
    serverLists: localhost:2181
    namespace: elasticjob-lite-springboot

  jobs:
    simpleJob:
      elasticJobClass: cn.sxy21.elasticjob.job.MyJob
      timeZone: GMT+08:00

jobs 的配置项是否必填参考文档

使用 RESTful API 管理作业

ElasticJob-Lite 提供了 Java API,可以通过直接对注册中心进行操作的方式控制作业在分布式环境下的生命周期

  1. 新建一个 config 类
@Component
public class JobConfig {

    @Autowired
    private ZookeeperRegistryCenter zrc;

    @Bean
    public JobOperateAPI jobOperateAPI(){
        return new JobOperateAPIImpl(zrc);
    }

    @Bean
    public JobConfigurationAPI jobConfigurationAPI() {
        return new JobConfigurationAPIImpl(zrc);
    }
}
  1. 新建一个 Controller 类
@RestController
@RequestMapping("/jobs")
public class JobApiController {


    @Autowired
    private JobOperateAPI jobOperateAPI;

    @Autowired
    private JobConfigurationAPI jobConfigurationAPI;

    @GetMapping("/config")
    public String get(@RequestParam String name){
        JobConfigurationPOJO jobConfig =  jobConfigurationAPI.getJobConfiguration(name);
        return jobConfig.toString();
    }

    @GetMapping("/disable")
    public String stop(@RequestParam String name){
        jobOperateAPI.disable(name,null);
        return name;
    }

    @GetMapping("/enable")
    public String start(@RequestParam String name){
        jobOperateAPI.enable(name,null);
        return name;
    }
}

运维平台和 RESTFul API 部署

elasticjob-ui 是 ElasticJob 的管理控制台,包含了动态配置、作业管控等功能。需要使用 jdk 1.8 编译和运行

  1. 编译

方法一:直接下载编译好的二进制包
下载 apache-shardingsphere-${version}-shardingsphere-ui-bin.tar.gz 解压后执行 bin/start.sh

方法二:使用 jdk1.8 编译

git clone https://github.com/apache/shardingsphere-elasticjob-ui.git

cd shardingsphere-elasticjob-ui/

# 在配置文件 shardingsphere-elasticjob-lite-ui/shardingsphere-elasticjob-lite-ui-backend/src/main/resources/application.properties
# 取消注释
dynamic.datasource.allowed-driver-classes={'org.h2.Driver','org.postgresql.Driver'}

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_341.jdk/Contents/Home

mvn clean package -Prelease

解压 elasticjob-lite-ui-bin.tar.gz 运行 bin/start.sh

  1. 打开浏览器访问 http://localhost:8088/ 即可访问控制台,可通过启动脚本输入 -p 自定义端口号。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值