From f402ec17480ae96b6f4dd2777620e67ee10374dc Mon Sep 17 00:00:00 2001 From: OTTO <731554297@example.com> Date: Wed, 10 Sep 2025 09:53:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=BE=AE=E6=9C=8D=E5=8A=A1=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0):=20=E7=94=A8=E6=88=B7=E8=BE=93=E5=85=A5=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E6=A0=BC=E5=BC=8F=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=97=B6=E8=BF=94=E5=9B=9E=E5=8F=8B=E5=A5=BD=E7=9A=84=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E6=8F=90=E7=A4=BA=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/MicroManagementServiceImpl.java | 16 +++++++-------- .../mon/k8s/utils/RancherResponseHandler.java | 20 +++++++++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MicroManagementServiceImpl.java b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MicroManagementServiceImpl.java index 72f027098..57fcb0b48 100644 --- a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MicroManagementServiceImpl.java +++ b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MicroManagementServiceImpl.java @@ -157,7 +157,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM case STOP: { applicationSpecJson.put("replicas", 0); try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "停止应用"); } } case FORCE_STOP: { @@ -178,7 +178,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM } return delPodSuccess; } - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "强制停止应用"); } } case RESTART: { @@ -211,7 +211,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM applicationDetailJson.put("spec", applicationSpecJson); applicationDetailJson.put("metadata", metadataJson); try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "重启应用"); } } @@ -221,14 +221,14 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM applicationSpecJson.put("paused", true); applicationDetailJson.put("spec", applicationSpecJson); try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "暂停应用"); } } case RESUME: { applicationSpecJson.put("paused", false); applicationDetailJson.put("spec", applicationSpecJson); try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "恢复应用"); } } } @@ -252,7 +252,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM public Boolean createApplication(String kind, String yamlContent) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); try (Response response = remoteRancherService.createK8sApplication(applicationType.getFullName(), yamlContent, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "创建应用"); } } @@ -274,7 +274,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM public Boolean deleteApplication(String kind, String namespace, String applicationName) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); try (Response response = remoteRancherService.deleteApplication(applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "删除应用"); } } @@ -416,7 +416,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - return RancherResponseHandler.handleBoolean(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "更新应用环境变量","环境变量格式异常"); } } diff --git a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/RancherResponseHandler.java b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/RancherResponseHandler.java index 915128ef9..925f74e72 100644 --- a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/RancherResponseHandler.java +++ b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/RancherResponseHandler.java @@ -2,6 +2,7 @@ package com.microservices.mon.k8s.utils; import com.alibaba.fastjson2.JSONObject; import com.microservices.common.core.exception.ServiceException; +import com.microservices.common.core.utils.StringUtils; import com.microservices.system.api.utils.FeignUtils; import feign.Response; import lombok.extern.slf4j.Slf4j; @@ -13,7 +14,7 @@ import java.nio.charset.StandardCharsets; @Slf4j public class RancherResponseHandler { - public static JSONObject handleJsonObject(Response response, String functionName) { + public static JSONObject handleJsonObject(Response response, String functionName, String errorMessage) { if (response != null) { Response.Body body = response.body(); if (body != null) { @@ -25,7 +26,12 @@ public class RancherResponseHandler { } if (bodyJson != null && bodyJson.containsKey("message")) { log.error("{}时发生错误:{}", functionName, bodyJson.toJSONString()); - throw new ServiceException("%s时发生错误,错误原因:%s", functionName, bodyJson.getString("message")); + if (StringUtils.isEmpty(errorMessage)) { + throw new ServiceException("%s时发生错误,错误原因:%s", functionName, bodyJson.getString("message")); + } else { + throw new ServiceException("%s时发生错误,错误原因:%s", functionName, errorMessage); + } + } } catch (IOException e) { throw new ServiceException("%s时发生错误,响应数据读取异常", functionName); @@ -36,10 +42,20 @@ public class RancherResponseHandler { throw new ServiceException("%s时发生错误,请联系系统管理员", functionName); } + public static JSONObject handleJsonObject(Response response, String functionName) { + return handleJsonObject(response, functionName, null); + } + public static Boolean handleBoolean(Response response, String functionName) { JSONObject bodyJson = handleJsonObject(response, functionName); return bodyJson != null; } + + public static Boolean handleBoolean(Response response, String functionName, String errorMessage) { + JSONObject bodyJson = handleJsonObject(response, functionName, errorMessage); + return bodyJson != null; + } + }