mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-11-28 01:00:05 +08:00
add 新增 ruoyi-powerjob-server 整合 powerjob 框架
update 重构 ruoyi-job 适配 powerjob remove 移除 xxljob 建议使用 powerjob
This commit is contained in:
@@ -28,10 +28,14 @@
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- xxl-job-core -->
|
||||
<!--PowerJob-->
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<groupId>tech.powerjob</groupId>
|
||||
<artifactId>powerjob-worker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>tech.powerjob</groupId>
|
||||
<artifactId>powerjob-official-processors</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package org.dromara.common.job.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dromara.common.core.utils.StreamUtils;
|
||||
import org.dromara.common.job.config.properties.PowerJobProperties;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import tech.powerjob.common.utils.CommonUtils;
|
||||
import tech.powerjob.common.utils.NetUtils;
|
||||
import tech.powerjob.worker.PowerJobSpringWorker;
|
||||
import tech.powerjob.worker.common.PowerJobWorkerConfig;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 启动定时任务
|
||||
*
|
||||
* @author yhan219
|
||||
* @since 2023/6/2
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(prefix = "powerjob.worker", name = "enabled", havingValue = "true")
|
||||
@EnableConfigurationProperties(PowerJobProperties.class)
|
||||
@EnableScheduling
|
||||
public class PowerJobConfig {
|
||||
|
||||
@Bean
|
||||
public PowerJobSpringWorker initPowerJob(PowerJobProperties properties, DiscoveryClient discoveryClient) {
|
||||
|
||||
PowerJobProperties.Worker worker = properties.getWorker();
|
||||
|
||||
/*
|
||||
* Address of PowerJob-server node(s). Do not mistake for ActorSystem port. Do not add
|
||||
* any prefix, i.e. http://.
|
||||
*/
|
||||
List<String> serverAddress;
|
||||
if (StringUtils.isNotBlank(worker.getServerName())) {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(worker.getServerName());
|
||||
if (CollUtil.isEmpty(instances)) {
|
||||
throw new RuntimeException("调度中心不存在!");
|
||||
}
|
||||
serverAddress = StreamUtils.toList(instances, instance ->
|
||||
String.format("%s:%s", instance.getHost(), instance.getPort()));
|
||||
} else {
|
||||
CommonUtils.requireNonNull(worker.getServerAddress(), "serverAddress can't be empty! " +
|
||||
"if you don't want to enable powerjob, please config program arguments: powerjob.worker.enabled=false");
|
||||
serverAddress = Arrays.asList(worker.getServerAddress().split(","));
|
||||
}
|
||||
/*
|
||||
* Create OhMyConfig object for setting properties.
|
||||
*/
|
||||
PowerJobWorkerConfig config = new PowerJobWorkerConfig();
|
||||
/*
|
||||
* Configuration of worker port. Random port is enabled when port is set with non-positive number.
|
||||
*/
|
||||
if (worker.getPort() != null) {
|
||||
config.setPort(worker.getPort());
|
||||
} else {
|
||||
int port = worker.getAkkaPort();
|
||||
if (port <= 0) {
|
||||
port = NetUtils.getRandomPort();
|
||||
}
|
||||
config.setPort(port);
|
||||
}
|
||||
/*
|
||||
* appName, name of the application. Applications should be registered in advance to prevent
|
||||
* error. This property should be the same with what you entered for appName when getting
|
||||
* registered.
|
||||
*/
|
||||
config.setAppName(worker.getAppName());
|
||||
config.setServerAddress(serverAddress);
|
||||
config.setProtocol(worker.getProtocol());
|
||||
/*
|
||||
* For non-Map/MapReduce tasks, {@code memory} is recommended for speeding up calculation.
|
||||
* Map/MapReduce tasks may produce batches of subtasks, which could lead to OutOfMemory
|
||||
* exception or error, {@code disk} should be applied.
|
||||
*/
|
||||
config.setStoreStrategy(worker.getStoreStrategy());
|
||||
/*
|
||||
* When enabledTestMode is set as true, PowerJob-worker no longer connects to PowerJob-server
|
||||
* or validate appName.
|
||||
*/
|
||||
config.setEnableTestMode(worker.isEnableTestMode());
|
||||
/*
|
||||
* Max length of appended workflow context . Appended workflow context value that is longer than the value will be ignored.
|
||||
*/
|
||||
config.setMaxAppendedWfContextLength(worker.getMaxAppendedWfContextLength());
|
||||
|
||||
config.setTag(worker.getTag());
|
||||
|
||||
config.setMaxHeavyweightTaskNum(worker.getMaxHeavyweightTaskNum());
|
||||
|
||||
config.setMaxLightweightTaskNum(worker.getMaxLightweightTaskNum());
|
||||
|
||||
config.setHealthReportInterval(worker.getHealthReportInterval());
|
||||
/*
|
||||
* Create PowerJobSpringWorker object and set properties.
|
||||
*/
|
||||
return new PowerJobSpringWorker(config);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package org.dromara.common.job.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import org.dromara.common.core.utils.StreamUtils;
|
||||
import org.dromara.common.job.config.properties.XxlJobProperties;
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* xxl-job config
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(XxlJobProperties.class)
|
||||
@AllArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true")
|
||||
public class XxlJobConfig {
|
||||
|
||||
private final XxlJobProperties xxlJobProperties;
|
||||
|
||||
private final DiscoveryClient discoveryClient;
|
||||
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
log.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
if (StringUtils.isNotBlank(xxlJobProperties.getAdminAppname())) {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(xxlJobProperties.getAdminAppname());
|
||||
if (CollUtil.isEmpty(instances)) {
|
||||
throw new RuntimeException("调度中心不存在!");
|
||||
}
|
||||
String serverList = StreamUtils.join(instances, instance ->
|
||||
String.format("http://%s:%s", instance.getHost(), instance.getPort()));
|
||||
xxlJobSpringExecutor.setAdminAddresses(serverList);
|
||||
} else {
|
||||
xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
|
||||
}
|
||||
xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
|
||||
XxlJobProperties.Executor executor = xxlJobProperties.getExecutor();
|
||||
xxlJobSpringExecutor.setAppname(executor.getAppname());
|
||||
xxlJobSpringExecutor.setAddress(executor.getAddress());
|
||||
xxlJobSpringExecutor.setIp(executor.getIp());
|
||||
xxlJobSpringExecutor.setPort(executor.getPort());
|
||||
xxlJobSpringExecutor.setLogPath(executor.getLogPath());
|
||||
xxlJobSpringExecutor.setLogRetentionDays(executor.getLogRetentionDays());
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package org.dromara.common.job.config.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import tech.powerjob.common.RemoteConstant;
|
||||
import tech.powerjob.common.enums.Protocol;
|
||||
import tech.powerjob.worker.common.constants.StoreStrategy;
|
||||
import tech.powerjob.worker.core.processor.ProcessResult;
|
||||
import tech.powerjob.worker.core.processor.WorkflowContext;
|
||||
|
||||
/**
|
||||
* PowerJob properties configuration class.
|
||||
*
|
||||
* @author songyinyin
|
||||
* @since 2020/7/26 16:37
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "powerjob")
|
||||
public class PowerJobProperties {
|
||||
|
||||
private final Worker worker = new Worker();
|
||||
|
||||
public Worker getWorker() {
|
||||
return worker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Powerjob worker configuration properties.
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public static class Worker {
|
||||
|
||||
/**
|
||||
* Whether to enable PowerJob Worker
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* Name of application, String type. Total length of this property should be no more than 255
|
||||
* characters. This is one of the required properties when registering a new application. This
|
||||
* property should be assigned with the same value as what you entered for the appName.
|
||||
*/
|
||||
private String appName;
|
||||
/**
|
||||
* Akka port of Powerjob-worker, optional value. Default value of this property is 27777.
|
||||
* If multiple PowerJob-worker nodes were deployed, different, unique ports should be assigned.
|
||||
* Deprecated, please use 'port'
|
||||
*/
|
||||
@Deprecated
|
||||
private int akkaPort = RemoteConstant.DEFAULT_WORKER_PORT;
|
||||
/**
|
||||
* port
|
||||
*/
|
||||
private Integer port;
|
||||
/**
|
||||
* Address(es) of Powerjob-server node(s). Ip:port or domain.
|
||||
* Example of single Powerjob-server node:
|
||||
* <p>
|
||||
* 127.0.0.1:7700
|
||||
* </p>
|
||||
* Example of Powerjob-server cluster:
|
||||
* <p>
|
||||
* 192.168.0.10:7700,192.168.0.11:7700,192.168.0.12:7700
|
||||
* </p>
|
||||
*/
|
||||
private String serverAddress;
|
||||
|
||||
private String serverName;
|
||||
/**
|
||||
* Protocol for communication between WORKER and server
|
||||
*/
|
||||
private Protocol protocol = Protocol.AKKA;
|
||||
/**
|
||||
* Local store strategy for H2 database. {@code disk} or {@code memory}.
|
||||
*/
|
||||
private StoreStrategy storeStrategy = StoreStrategy.DISK;
|
||||
/**
|
||||
* Max length of response result. Result that is longer than the value will be truncated.
|
||||
* {@link ProcessResult} max length for #msg
|
||||
*/
|
||||
private int maxResultLength = 8192;
|
||||
/**
|
||||
* If test mode is set as true, Powerjob-worker no longer connects to the server or validates appName.
|
||||
* Test mode is used for conditions that your have no powerjob-server in your develop env, so you can't start up the application
|
||||
*/
|
||||
private boolean enableTestMode = false;
|
||||
/**
|
||||
* Max length of appended workflow context value length. Appended workflow context value that is longer than the value will be ignored.
|
||||
* {@link WorkflowContext} max length for #appendedContextData
|
||||
*/
|
||||
private int maxAppendedWfContextLength = 8192;
|
||||
|
||||
private String tag;
|
||||
/**
|
||||
* Max numbers of LightTaskTacker
|
||||
*/
|
||||
private Integer maxLightweightTaskNum = 1024;
|
||||
/**
|
||||
* Max numbers of HeavyTaskTacker
|
||||
*/
|
||||
private Integer maxHeavyweightTaskNum = 64;
|
||||
/**
|
||||
* Interval(s) of worker health report
|
||||
*/
|
||||
private Integer healthReportInterval = 10;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package org.dromara.common.job.config.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* xxljob配置类
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "xxl.job")
|
||||
public class XxlJobProperties {
|
||||
|
||||
private Boolean enabled;
|
||||
|
||||
private String adminAddresses;
|
||||
|
||||
private String adminAppname;
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private Executor executor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public static class Executor {
|
||||
|
||||
private String appname;
|
||||
|
||||
private String address;
|
||||
|
||||
private String ip;
|
||||
|
||||
private int port;
|
||||
|
||||
private String logPath;
|
||||
|
||||
private int logRetentionDays;
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
org.dromara.common.job.config.XxlJobConfig
|
||||
org.dromara.common.job.config.PowerJobConfig
|
||||
|
||||
Reference in New Issue
Block a user