mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: support lazy init#725
This commit is contained in:
parent
c875ba3d37
commit
882392a5e5
@ -1,11 +1,13 @@
|
||||
package tech.powerjob.worker.background.discovery;
|
||||
package tech.powerjob.common.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 应用信息
|
||||
* WorkerAppInfo
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2023/9/2
|
||||
@ -13,7 +15,7 @@ import lombok.experimental.Accessors;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
public class AppInfo {
|
||||
public class WorkerAppInfo implements Serializable {
|
||||
|
||||
/**
|
||||
* 应用唯一 ID
|
@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import tech.powerjob.common.model.WorkerAppInfo;
|
||||
import tech.powerjob.common.request.ServerDiscoveryRequest;
|
||||
import tech.powerjob.common.response.ResultDTO;
|
||||
import tech.powerjob.common.utils.CommonUtils;
|
||||
@ -50,6 +51,16 @@ public class ServerController implements ServerInfoAware {
|
||||
orElseGet(() -> ResultDTO.failed(String.format("app(%s) is not registered! Please register the app in oms-console first.", appName)));
|
||||
}
|
||||
|
||||
@GetMapping("/assertV2")
|
||||
public ResultDTO<WorkerAppInfo> assertAppNameV2(String appName) {
|
||||
Optional<AppInfoDO> appInfoOpt = appInfoRepository.findByAppName(appName);
|
||||
return appInfoOpt.map(appInfoDO -> {
|
||||
WorkerAppInfo workerAppInfo = new WorkerAppInfo().setAppId(appInfoDO.getId());
|
||||
return ResultDTO.success(workerAppInfo);
|
||||
}).
|
||||
orElseGet(() -> ResultDTO.failed(String.format("app(%s) is not registered! Please register the app in oms-console first.", appName)));
|
||||
}
|
||||
|
||||
@GetMapping("/acquire")
|
||||
public ResultDTO<String> acquireServer(ServerDiscoveryRequest request) {
|
||||
return ResultDTO.success(serverElectionService.elect(request));
|
||||
|
@ -4,6 +4,7 @@ import com.google.common.base.Stopwatch;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import tech.powerjob.common.PowerJobDKey;
|
||||
import tech.powerjob.common.model.WorkerAppInfo;
|
||||
import tech.powerjob.common.utils.CommonUtils;
|
||||
import tech.powerjob.common.utils.NetUtils;
|
||||
import tech.powerjob.common.utils.PropertyUtils;
|
||||
@ -18,7 +19,6 @@ import tech.powerjob.worker.actors.TaskTrackerActor;
|
||||
import tech.powerjob.worker.actors.WorkerActor;
|
||||
import tech.powerjob.worker.background.OmsLogHandler;
|
||||
import tech.powerjob.worker.background.WorkerHealthReporter;
|
||||
import tech.powerjob.worker.background.discovery.AppInfo;
|
||||
import tech.powerjob.worker.background.discovery.PowerJobServerDiscoveryService;
|
||||
import tech.powerjob.worker.background.discovery.ServerDiscoveryService;
|
||||
import tech.powerjob.worker.common.PowerBannerPrinter;
|
||||
@ -75,7 +75,7 @@ public class PowerJobWorker {
|
||||
try {
|
||||
PowerBannerPrinter.print();
|
||||
// 校验 appName
|
||||
AppInfo appInfo = serverDiscoveryService.assertApp();
|
||||
WorkerAppInfo appInfo = serverDiscoveryService.assertApp();
|
||||
workerRuntime.setAppInfo(appInfo);
|
||||
|
||||
// 初始化网络数据,区别对待上报地址和本机绑定地址(对外统一使用上报地址)
|
||||
|
@ -8,6 +8,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import tech.powerjob.common.OmsConstant;
|
||||
import tech.powerjob.common.exception.ImpossibleException;
|
||||
import tech.powerjob.common.exception.PowerJobException;
|
||||
import tech.powerjob.common.model.WorkerAppInfo;
|
||||
import tech.powerjob.common.request.ServerDiscoveryRequest;
|
||||
import tech.powerjob.common.response.ObjectResultDTO;
|
||||
import tech.powerjob.common.serialize.JsonUtils;
|
||||
@ -33,7 +34,7 @@ import java.util.concurrent.TimeUnit;
|
||||
@Slf4j
|
||||
public class PowerJobServerDiscoveryService implements ServerDiscoveryService {
|
||||
|
||||
private final AppInfo appInfo = new AppInfo();
|
||||
private final WorkerAppInfo appInfo = new WorkerAppInfo();
|
||||
|
||||
private String currentServerAddress;
|
||||
|
||||
@ -61,7 +62,7 @@ public class PowerJobServerDiscoveryService implements ServerDiscoveryService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppInfo assertApp() {
|
||||
public WorkerAppInfo assertApp() {
|
||||
try {
|
||||
return assertApp0();
|
||||
} catch (Exception e) {
|
||||
@ -76,7 +77,7 @@ public class PowerJobServerDiscoveryService implements ServerDiscoveryService {
|
||||
throw new ImpossibleException();
|
||||
}
|
||||
|
||||
private AppInfo assertApp0() {
|
||||
private WorkerAppInfo assertApp0() {
|
||||
String appName = config.getAppName();
|
||||
Objects.requireNonNull(appName, "appName can't be empty!");
|
||||
|
||||
@ -97,7 +98,7 @@ public class PowerJobServerDiscoveryService implements ServerDiscoveryService {
|
||||
}
|
||||
|
||||
// 新版本,接口直接下发 AppInfo 内容,后续可扩展安全加密等信息
|
||||
AppInfo serverAppInfo = JsonUtils.parseObject(JsonUtils.toJSONString(resultDataContent), AppInfo.class);
|
||||
WorkerAppInfo serverAppInfo = JsonUtils.parseObject(JsonUtils.toJSONString(resultDataContent), WorkerAppInfo.class);
|
||||
appInfo.setAppId(serverAppInfo.getAppId());
|
||||
return appInfo;
|
||||
} else {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package tech.powerjob.worker.background.discovery;
|
||||
|
||||
import tech.powerjob.common.model.WorkerAppInfo;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
/**
|
||||
@ -14,7 +16,7 @@ public interface ServerDiscoveryService {
|
||||
* 鉴权 & 附带信息下发
|
||||
* @return appInfo
|
||||
*/
|
||||
AppInfo assertApp();
|
||||
WorkerAppInfo assertApp();
|
||||
|
||||
/**
|
||||
* 获取当前的 server 地址
|
||||
|
@ -1,9 +1,9 @@
|
||||
package tech.powerjob.worker.common;
|
||||
|
||||
import lombok.Data;
|
||||
import tech.powerjob.common.model.WorkerAppInfo;
|
||||
import tech.powerjob.remote.framework.transporter.Transporter;
|
||||
import tech.powerjob.worker.background.OmsLogHandler;
|
||||
import tech.powerjob.worker.background.discovery.AppInfo;
|
||||
import tech.powerjob.worker.background.discovery.ServerDiscoveryService;
|
||||
import tech.powerjob.worker.core.executor.ExecutorManager;
|
||||
import tech.powerjob.worker.persistence.TaskPersistenceService;
|
||||
@ -23,7 +23,7 @@ public class WorkerRuntime {
|
||||
/**
|
||||
* App 基础信息
|
||||
*/
|
||||
private AppInfo appInfo;
|
||||
private WorkerAppInfo appInfo;
|
||||
/**
|
||||
* 当前执行器地址
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user