ElasticJob job框架升级: dangdang.ElasticJob2.1.5升级org.apache.shardingsphere ElasticJob 3.0.2
先说下为什么升级,
当我本地部署zk 启动job服务后,想这本地安装一个本地的job控制台, 无奈找了半天找到一个apache的3.0.2的 结果启动后,和本地版本不一致无法使用, 一怒之下升级了自己项目的ElasticJob 3.0.2
大致介绍下
1禁用旧的pom,加上新的
<!-- <dependency>-->
<!-- <groupId>com.dangdang</groupId>-->
<!-- <artifactId>elastic-job-common-core</artifactId>-->
<!-- <version>2.1.5</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.dangdang</groupId>-->
<!-- <artifactId>elastic-job-lite-spring</artifactId>-->
<!-- <version>2.1.5</version>-->
<!-- </dependency>-->
<!-- ElasticJob核心依赖 -->
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-core</artifactId>
<version>3.0.2</version>
</dependency>
<!-- 添加新版本依赖 -->
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
如果是在问的ai他会让你 还加一个zookeeper的引用,这里其实不用。
springbootstarter应该已经包含了
2,升级job配置中心,用于启动项目是扫描被注解的类,作为任务注册到任务中心
旧代码:
旧代码,注册任务中心
///*
// * Copyright © 2015-2026 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// * @since 0.0.1
// */
//package com.zmm.ninja.config;
//
//
//import com.dangdang.ddframe.job.api.ElasticJob;
//import com.dangdang.ddframe.job.config.JobCoreConfiguration;
//import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
//import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
//import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
//import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.stereotype.Component;
//
//import java.util.Map;
//import java.util.Optional;
//
///**
// * @author zhengmingming
// * @since 1.0.0
// */
//@Component
//@Slf4j
//public class RegisterEjob implements ApplicationContextAware, InitializingBean {
//
// @Autowired
// private ZookeeperRegistryCenter zookeeperRegistryCenter;
//
// private ApplicationContext applicationContext;
//
// @Override
// public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// this.applicationContext = applicationContext;
// }
//
// @Override
// public void afterPropertiesSet() throws Exception {
// Map<String, Object> registerJobs = applicationContext.getBeansWithAnnotation(ElasticJobZ.class);
// for (Map.Entry<String, Object> entry : registerJobs.entrySet()) {
// try {
// Object object = entry.getValue();
// if (!(object instanceof ElasticJob)) {
// throw new ClassCastException("[" + object.getClass().getName() + "] The class type is not com.dangdang.ddframe.job.api.ElasticJobZ");
// }
// ElasticJobZ elasticJob = AnnotationUtils.findAnnotation(object.getClass(), ElasticJobZ.class);
// // 判断 disable 属性是否为 true
// if (elasticJob != null && elasticJob.disable()) {
// log.info("Job {} is disabled and will not be scheduled.", elasticJob.jobName());
// continue;
// }
// SpringJobScheduler springJobScheduler = new SpringJobScheduler((ElasticJob) object, zookeeperRegistryCenter, getJobConfiguration(elasticJob, object));
// springJobScheduler.init();
// } catch (Exception e) {
// log.error("注册任务异常 ", e);
// }
// }
// }
//
// /**
// * 配置job任务
// *
// * @param elasticJob
// * @param object
// * @return
// */
// private LiteJobConfiguration getJobConfiguration(ElasticJobZ elasticJob, Object object) {
//
// Optional.ofNullable(elasticJob.jobName()).orElseThrow(() -> new IllegalArgumentException("The jobName cannot be null !"));
// Optional.ofNullable(elasticJob.cron()).orElseThrow(() -> new IllegalArgumentException("The cron cannot be null !"));
// Optional.ofNullable(elasticJob.desc()).orElseThrow(() -> new IllegalArgumentException("The desc cannot be null !"));
//
// SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(
// JobCoreConfiguration
// .newBuilder(elasticJob.jobName(), elasticJob.cron(), elasticJob.shardingTotalCount())
// .shardingItemParameters(StringUtils.isEmpty(elasticJob.shardingItemParameters()) ? null : elasticJob.shardingItemParameters())
// .description(elasticJob.desc())
// .failover(elasticJob.failover())
// .jobParameter(StringUtils.isEmpty(elasticJob.jobParameter()) ? null : elasticJob.jobParameter())
// .build(),
// object.getClass().getName());
// LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration
// .newBuilder(simpleJobConfiguration).overwrite(elasticJob.overwrite())
// .monitorExecution(true)
// .build();
// return liteJobConfiguration;
// }
//}
升级i后到代码,注册中心配置:
/*
* Copyright © 2015-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* @since 0.0.1
*/
package com.zmm.ninja.config;
import org.apache.shardingsphere.elasticjob.api.ElasticJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Optional;
/**
* @author zhengmingming
* @since 1.0.0
*/
@Component
@Slf4j
public class ElasticJobAnnotationProcessor implements ApplicationContextAware, InitializingBean {
@Autowired
private ZookeeperRegistryCenter zookeeperRegistryCenter;
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
Map<String, Object> registerJobs = applicationContext.getBeansWithAnnotation(ElasticScheduledJob.class);
for (Map.Entry<String, Object> entry : registerJobs.entrySet()) {
try {
Object object = entry.getValue();
if (!(object instanceof ElasticJob)) {
throw new ClassCastException("[" + object.getClass().getName() + "] The class type is not org.apache.shardingsphere.elasticjob.api.ElasticJob");
}
ElasticScheduledJob elasticJob = AnnotationUtils.findAnnotation(object.getClass(), ElasticScheduledJob.class);
// if (elasticJob != null && elasticJob.disabled()) {
// log.info("Job {} is disabled and will not be scheduled.", elasticJob.jobName());
// continue;
// }
new ScheduleJobBootstrap(zookeeperRegistryCenter, (ElasticJob) object, getJobConfiguration(elasticJob)).schedule();
} catch (Exception e) {
log.error("注册任务异常 ", e);
}
}
}
/**
* 配置job任务
*
* @param elasticJob 自定义的作业注解
* @return 作业配置
*/
private JobConfiguration getJobConfiguration(ElasticScheduledJob elasticJob) {
Optional.ofNullable(elasticJob.jobName()).orElseThrow(() -> new IllegalArgumentException("The jobName cannot be null !"));
Optional.ofNullable(elasticJob.cron()).orElseThrow(() -> new IllegalArgumentException("The cron cannot be null !"));
Optional.ofNullable(elasticJob.description()).orElseThrow(() -> new IllegalArgumentException("The desc cannot be null !"));
return JobConfiguration.newBuilder(elasticJob.jobName(), elasticJob.shardingTotalCount())
.cron(elasticJob.cron())
.shardingItemParameters(StringUtils.isEmpty(elasticJob.shardingItemParameters()) ? "" : elasticJob.shardingItemParameters())
.description(elasticJob.description())
.failover(elasticJob.failover())
.jobParameter(StringUtils.isEmpty(elasticJob.jobParameter()) ? "" : elasticJob.jobParameter())
.overwrite(elasticJob.overwrite())
.monitorExecution(true)
.disabled(elasticJob.disabled())
.build();
}
}
3升级后 zookpeer会在启动时注册,删除之前的zk注册类
删除之前的zk注册类
//
//package com.zmm.ninja.config;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
//import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
///**
// * @author zhengmingming
// * @since 1.0.0
// */
//@Configuration
//@Slf4j
//public class ZookeeperConfig {
//
// @Value("${server_lists}")
// private String serverLists;
//
// @Value("${namespace}")
// private String namespace;
//
// @Value("${base_sleep_time_milliseconds}")
// private int baseSleepTimeMilliseconds;
//
// @Value("${max_sleep_time_milliseconds}")
// private int maxSleepTimeMilliseconds;
//
// @Value("${max_retries}")
// private int maxRetries;
// /**
// * 初始化注册中心
// *
// * */
// @Bean(initMethod = "init")
// public ZookeeperRegistryCenter zookeeperRegistryCenter() {
// log.info("初始化zoopeeker");
// ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverLists, namespace);
// zookeeperConfiguration.setBaseSleepTimeMilliseconds(baseSleepTimeMilliseconds);
// zookeeperConfiguration.setMaxSleepTimeMilliseconds(maxSleepTimeMilliseconds);
// zookeeperConfiguration.setMaxRetries(maxRetries);
// return new ZookeeperRegistryCenter(zookeeperConfiguration);
// }
//}
zk配置中心配置类会自动配置
需要调整zk地址配置
elasticjob.reg-center.serverLists=127.0.0.1:2181
elasticjob.reg-center.namespace=ninjaJob
自定义注解:
package com.zmm.ninja.config;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Component
public @interface ElasticScheduledJob {
/**
* 作业名称
*/
String jobName();
/**
* cron表达式
*/
String cron();
/**
* 分片总数
*/
int shardingTotalCount() default 1;
/**
* 分片参数
* 格式: 0=北京,1=上海,2=广州
*/
String shardingItemParameters() default "";
/**
* 作业参数
*/
String jobParameter() default "";
/**
* 是否开启失效转移
*/
boolean failover() default false;
/**
* 是否开启错过任务重新执行
*/
boolean misfire() default true;
/**
* 作业描述信息
*/
String description() default "";
/**
* 是否覆盖已存在的作业配置
*/
boolean overwrite() default false;
/**
* 是否流式处理 (仅DataflowJob有效)
*/
boolean streamingProcess() default false;
/**
* 脚本命令 (仅ScriptJob有效)
*/
String scriptCommandLine() default "";
/**
* 是否禁用
*/
boolean disabled() default false;
}
新建任务

控制台:
下载解压
得到文件:

修改配置文件
apache-shardingsphere-elasticjob-3.0.1-lite-ui-bin/conf/application.properties
里面有端口号 和 登录的账号密码

进入bin目录 启动
sudo ./start.sh

进入日志查看启动情况, 或者直接访问

进入后根据自己的配置 先注册,在进行作业的控制

浙公网安备 33010602011771号