diff --git a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteRancherService.java b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteRancherService.java index 5cbc00877..4554648ad 100644 --- a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteRancherService.java +++ b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteRancherService.java @@ -23,41 +23,41 @@ import java.util.Map; @FeignClient(contextId = "RemoteRancherService", value = ServiceNameConstants.GATEWAY_SERVICE, fallbackFactory = RemoteRancherFallbackFactory.class) public interface RemoteRancherService { @GetMapping("/rancher/v3/clusters/{cluster_id}/namespaces") - JSONObject getK8sNamespaceList(@PathVariable("cluster_id") String cluster_id, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sNamespaceList(@PathVariable("cluster_id") String cluster_id, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.daemonsets?exclude=metadata.managedFields") - JSONObject getK8sDaemonsetsList(@PathVariable("cluster_id") String cluster_id, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); - - @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.deployments?exclude=metadata.managedFields") - JSONObject getK8sDeployList(@PathVariable("cluster_id") String cluster_id, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); - - @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.statefulsets?exclude=metadata.managedFields") - JSONObject getK8sStatefulList(@PathVariable("cluster_id") String cluster_id, + Response getK8sDaemonsetsList(@PathVariable("cluster_id") String cluster_id, @RequestHeader(TokenConstants.AUTHENTICATION) String token); + @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.deployments?exclude=metadata.managedFields") + Response getK8sDeployList(@PathVariable("cluster_id") String cluster_id, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); + + @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.statefulsets?exclude=metadata.managedFields") + Response getK8sStatefulList(@PathVariable("cluster_id") String cluster_id, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); + @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/nodes?exclude=metadata.managedFields") - JSONObject getK8sClusterNodeList(@PathVariable("cluster_id") String cluster_id, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sClusterNodeList(@PathVariable("cluster_id") String cluster_id, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/{fullKind}/{namespace}/{applicationName}") - JSONObject getK8sApplicationDetail(@PathVariable("cluster_id") String cluster_id, - @PathVariable("fullKind") String fullKind, - @PathVariable("namespace") String namespace, - @PathVariable("applicationName") String applicationName, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sApplicationDetail(@PathVariable("cluster_id") String cluster_id, + @PathVariable("fullKind") String fullKind, + @PathVariable("namespace") String namespace, + @PathVariable("applicationName") String applicationName, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/pods/{namespace}") - JSONObject getK8sPodListByNamespace(@PathVariable("cluster_id") String cluster_id, - @PathVariable("namespace") String namespace, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sPodListByNamespace(@PathVariable("cluster_id") String cluster_id, + @PathVariable("namespace") String namespace, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/services/{nginxApplicationId}") - JSONObject getK8sServiceByApplicationId(@PathVariable("cluster_id") String cluster_id, - @PathVariable("nginxApplicationId") String nginxApplicationId, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sServiceByApplicationId(@PathVariable("cluster_id") String cluster_id, + @PathVariable("nginxApplicationId") String nginxApplicationId, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @DeleteMapping("/rancher/v1/pods/{namespace}/{podName}") Response deletePod(@PathVariable("namespace") String namespace, @@ -78,10 +78,10 @@ public interface RemoteRancherService { @RequestHeader(TokenConstants.AUTHENTICATION) String userKey); @GetMapping("/rancher/apis/apps/v1/namespaces/{namespace}/{kindName}/{applicationName}") - JSONObject getApplicationYamlFile(@PathVariable("kindName") String kindName, - @PathVariable("namespace") String namespace, - @PathVariable("applicationName") String applicationName, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getApplicationYamlFile(@PathVariable("kindName") String kindName, + @PathVariable("namespace") String namespace, + @PathVariable("applicationName") String applicationName, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @PutMapping(value = "/rancher/v1/{fullKind}/{namespace}/{applicationName}", consumes = {Constants.YAML_CONTENT_TYPE}) Response editK8sApplication(@PathVariable("fullKind") String fullKind, @@ -97,10 +97,10 @@ public interface RemoteRancherService { @RequestHeader(TokenConstants.AUTHENTICATION) String token); @GetMapping("/rancher/v1/configmaps/{namespace}") - JSONObject getK8sConfigmapByNamespace(@PathVariable("namespace") String namespace, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sConfigmapByNamespace(@PathVariable("namespace") String namespace, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); @GetMapping("/rancher/v1/secrets/{namespace}") - JSONObject getK8sSecretByNamespace(@PathVariable("namespace") String namespace, - @RequestHeader(TokenConstants.AUTHENTICATION) String token); + Response getK8sSecretByNamespace(@PathVariable("namespace") String namespace, + @RequestHeader(TokenConstants.AUTHENTICATION) String token); } diff --git a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java index b2f6c0d44..ce1730c75 100644 --- a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java +++ b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java @@ -28,68 +28,57 @@ public class RemoteGatewayFallbackFactory implements FallbackFactory formParams) { - log.error("网关接口调用:登录注册中心/配置中心失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:登录注册中心/配置中心失败"); + return null; } @Override public Response loginPortainer(JSONObject loginBody) { - log.error("网关接口调用:登录Portainer失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:登录Portainer失败"); + return null; } @Override public JSONObject getPrometheusQueryRes(String query, String token) { - log.error("网关接口调用:获取Prometheus查询失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:获取Prometheus查询失败"); + return null; } @Override public JSONObject getPrometheusQueryRangeRes(String query, Long step, Long start, Long end, String token) { - log.error("网关接口调用:获取Prometheus范围查询失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:获取Prometheus范围查询失败"); + return null; } @Override public JSONObject executeSkywalkingGql(GraphqlQueryVo gqlBody) { - log.error("网关接口调用:执行链路追踪请求失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:执行链路追踪请求失败"); + return null; } @Override public JSONObject getSentinelAppList(String token) { - log.error("网关接口调用:获取流量控制应用列表失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:获取流量控制应用列表失败"); + return null; } @Override public JSONObject getNacosServiceList(String serviceNameParam, String token) { - log.error("网关接口调用:获取注册中心服务列表失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:获取注册中心服务列表失败"); + return null; } @Override public JSONObject getGatewayNacosConfigList(String dataId, String token) { - log.error("网关接口调用:获取网关配置文件列表失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:获取网关配置文件列表失败"); + return null; } @Override public JSONObject getNacosConfigList(String ip, String token) { - log.error("网关接口调用:获取配置文件列表失败;异常信息:{}", throwable.getMessage()); - throw new ServiceException("接口调用:获取配置文件列表失败"); + return null; } }; } diff --git a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteRancherFallbackFactory.java b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteRancherFallbackFactory.java index 15f5d72f1..3c07de54c 100644 --- a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteRancherFallbackFactory.java +++ b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteRancherFallbackFactory.java @@ -4,12 +4,15 @@ import com.alibaba.fastjson2.JSONObject; import com.microservices.common.core.exception.ServiceException; import com.microservices.system.api.RemoteGatewayService; import com.microservices.system.api.RemoteRancherService; +import com.microservices.system.api.utils.FeignUtils; +import feign.FeignException; import feign.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; +import java.nio.charset.StandardCharsets; import java.util.Map; /** @@ -24,101 +27,86 @@ public class RemoteRancherFallbackFactory implements FallbackFactory getClusterNodeList() { List rancherClusterNodeVoList; //调用rancher远程接口获取集群节点列表 - JSONObject clusterNodeJsonObject = remoteRancherService.getK8sClusterNodeList(cluster_id, SecurityContextHolder.getUserKey()); + JSONObject clusterNodeJsonObject = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sClusterNodeList(cluster_id, SecurityContextHolder.getUserKey()), "获取K8S集群节点"); if (clusterNodeJsonObject != null && clusterNodeJsonObject.containsKey("data")) { rancherClusterNodeVoList = clusterNodeJsonObject.getJSONArray("data").toList(RancherClusterNodeVo.class); return rancherClusterNodeVoList.stream().map(RancherClusterNodeVo::toK8sClusterNode).collect(Collectors.toList()); @@ -49,7 +50,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo @Override public List getApplicationPodIdList(String kind, String namespace, String applicationName) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); - JSONObject applicationJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + JSONObject applicationJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情"); RancherApplicationVo rancherApplicationVo = applicationJson.toJavaObject(RancherApplicationVo.class); if (rancherApplicationVo != null && rancherApplicationVo.getMetadata() != null && rancherApplicationVo.getMetadata().getRelationships() != null) { JSONArray relationships = rancherApplicationVo.getMetadata().getRelationships(); @@ -59,7 +60,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo String selector = relationship.getString("selector"); if (StringUtils.isNotEmpty(selector) && selector.contains("app=")) { String appName = selector.substring(selector.indexOf("=") + 1); - JSONObject podListJson = remoteRancherService.getK8sPodListByNamespace(cluster_id, namespace, SecurityContextHolder.getUserKey()); + JSONObject podListJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sPodListByNamespace(cluster_id, namespace, SecurityContextHolder.getUserKey()), "获取容器列表"); if (podListJson != null && podListJson.containsKey("data")) { List rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class); List applicationToPodList = rancherPodVoList.stream() @@ -104,7 +105,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo public List getApplicationRequestCount() { List applicationRequestCountDataList = new ArrayList<>(); HashMap portMap = new HashMap<>(); - JSONObject nginxSvcJson = remoteRancherService.getK8sServiceByApplicationId(cluster_id, nginxApplicationId, SecurityContextHolder.getUserKey()); + JSONObject nginxSvcJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sServiceByApplicationId(cluster_id, nginxApplicationId, SecurityContextHolder.getUserKey()), "获取统一请求入口服务"); if (nginxSvcJson != null) { RancherServiceVo rancherServiceVo = nginxSvcJson.toJavaObject(RancherServiceVo.class); for (RancherServicePortsVo rancherServicePortsVo : rancherServiceVo.getSpec().getPorts()) { @@ -185,7 +186,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo public List getApplicationFunction(String kind, String namespace, String applicationName, String podIpList) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); List applicationFunctionsList = new ArrayList<>(); - JSONObject applicationJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + JSONObject applicationJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情"); if (applicationJson == null) { return applicationFunctionsList; } @@ -333,7 +334,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo List k8sNamespaceList = new ArrayList<>(); List rancherNamespaceVoList; //调用rancher远程接口获取所有命名空间列表 - JSONObject namespaceJsonObject = remoteRancherService.getK8sNamespaceList(cluster_id, SecurityContextHolder.getUserKey()); + JSONObject namespaceJsonObject = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sNamespaceList(cluster_id, SecurityContextHolder.getUserKey()), "获取K8S命名空间列表"); if (namespaceJsonObject != null && namespaceJsonObject.containsKey("data")) { rancherNamespaceVoList = namespaceJsonObject.getJSONArray("data").toList(RancherNamespaceVo.class); for (RancherNamespaceVo rancherNamespaceVo : rancherNamespaceVoList) { 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 4c147ce12..2a5e692ba 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 @@ -1,7 +1,5 @@ package com.microservices.mon.k8s.service.impl; -import com.alibaba.cloud.commons.io.IOUtils; -import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.microservices.common.core.context.SecurityContextHolder; @@ -11,7 +9,7 @@ import com.microservices.mon.k8s.domain.*; import com.microservices.mon.k8s.domain.vo.*; import com.microservices.mon.k8s.service.IMicroManagementService; import com.microservices.mon.k8s.service.IMonCommonAsyncService; -import com.microservices.mon.k8s.utils.MonUtils; +import com.microservices.mon.k8s.utils.RancherResponseHandler; import com.microservices.system.api.RemoteRancherService; import com.microservices.system.api.utils.FeignUtils; import feign.Response; @@ -19,8 +17,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; @@ -47,7 +43,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM public K8sApplication getApplicationDetail(String kind, String namespace, String applicationName) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); K8sApplication k8sApplication = new K8sApplication(); - JSONObject applicationJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + JSONObject applicationJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情"); if (applicationJson == null) { return k8sApplication; } @@ -69,18 +65,14 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM deletePodBody.put("gracePeriod", 0); } try (Response response = remoteRancherService.deletePod(namespace, podName, deletePodBody, SecurityContextHolder.getUserKey())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "删除容器"); + return RancherResponseHandler.handleBoolean(response, "删除容器"); } - return false; } @Override public List getK8sApplicationPodList(K8sApplication k8sApplication) { // 获取命名空间下pod列表 - JSONObject podListJson = remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sApplication.getNamespace(), SecurityContextHolder.getUserKey()); + JSONObject podListJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sApplication.getNamespace(), SecurityContextHolder.getUserKey()), "获取应用下容器列表"); List rancherPodVoList; if (podListJson != null && podListJson.containsKey("data")) { rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class); @@ -110,7 +102,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM if ("DaemonSet".equals(kind) && !applicationStartAndStopType.getIsDaemonSetOperation()) { throw new ServiceException("该应用操作类型不支持(操作类型[%s])", operationKey); } - JSONObject applicationDetailJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + JSONObject applicationDetailJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情"); if (applicationDetailJson == null || !applicationDetailJson.containsKey("metadata") || !applicationDetailJson.containsKey("spec")) { throw new ServiceException("该应用不存在或应用数据异常(应用类型[%s],命名空间[%s],应用名称[%s],)", kind, namespace, applicationName); } @@ -137,19 +129,13 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM } applicationSpecJson.put("replicas", 1); try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "启动应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } } case STOP: { applicationSpecJson.put("replicas", 0); try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "停止应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } } case FORCE_STOP: { @@ -170,9 +156,8 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM } return delPodSuccess; } - MonUtils.disposeRancherErrorMsg(response, "强制停止应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } - break; } case RESTART: { if (metadataJson.containsKey("annotations")) { @@ -203,10 +188,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())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "重启应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } } @@ -216,20 +198,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())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(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())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "继续应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } } } @@ -253,49 +229,37 @@ 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())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "创建应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } - return false; } @Override public JSONObject getApplicationYamlFile(String kind, String namespace, String applicationName) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); - return remoteRancherService.getApplicationYamlFile(applicationType.getName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + return RancherResponseHandler.handleJsonObject(remoteRancherService.getApplicationYamlFile(applicationType.getName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用yaml文件"); } @Override public Boolean editApplication(String kind, String namespace, String applicationName, String yamlContent) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); try (Response response = remoteRancherService.editK8sApplication(applicationType.getFullName(), namespace, applicationName, yamlContent, SecurityContextHolder.getUserKey())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "编辑应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } - return false; } @Override 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())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "删除应用"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } - return false; } @Override public List getApplicationEnv(String kind, String namespace, String applicationName) { List envList = new ArrayList<>(); ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); - JSONObject applicationJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + JSONObject applicationJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情"); if (applicationJson == null) { return envList; } @@ -336,7 +300,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM @Override public List getEnvOptionalParameters(String namespace) { List envOptionalParametersList = new ArrayList<>(); - JSONObject configmapJson = remoteRancherService.getK8sConfigmapByNamespace(namespace, SecurityContextHolder.getUserKey()); + JSONObject configmapJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sConfigmapByNamespace(namespace, SecurityContextHolder.getUserKey()), "获取命名空间下所有ConfigMap"); if (configmapJson != null && configmapJson.containsKey("data")) { JSONArray dataJsonArray = configmapJson.getJSONArray("data"); for (int i = 0; i < dataJsonArray.size(); i++) { @@ -358,7 +322,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM } } } - JSONObject secretJson = remoteRancherService.getK8sSecretByNamespace(namespace, SecurityContextHolder.getUserKey()); + JSONObject secretJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sSecretByNamespace(namespace, SecurityContextHolder.getUserKey()), "获取命名空间下所有Secret"); if (secretJson != null && secretJson.containsKey("data")) { JSONArray dataJsonArray = secretJson.getJSONArray("data"); for (int i = 0; i < dataJsonArray.size(); i++) { @@ -400,7 +364,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM @Override public Boolean updateApplicationEnv(String kind, String namespace, String applicationName, List k8sApplicationEnvList) { ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind); - JSONObject applicationDetailJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()); + JSONObject applicationDetailJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情"); if (applicationDetailJson == null || !applicationDetailJson.containsKey("spec")) { throw new ServiceException("该应用不存在或应用数据异常(应用类型[%s],命名空间[%s],应用名称[%s],)", kind, namespace, applicationName); } @@ -429,12 +393,8 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) { - if (FeignUtils.isSuccess(response)) { - return true; - } - MonUtils.disposeRancherErrorMsg(response, "更新应用环境变量"); + return RancherResponseHandler.handleBoolean(response, "启动应用"); } - return false; } @Override diff --git a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MonCommonAsyncServiceImpl.java b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MonCommonAsyncServiceImpl.java index 351647e57..e7b59b2a0 100644 --- a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MonCommonAsyncServiceImpl.java +++ b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/service/impl/MonCommonAsyncServiceImpl.java @@ -1,6 +1,5 @@ package com.microservices.mon.k8s.service.impl; -import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.microservices.common.core.context.SecurityContextHolder; import com.microservices.common.core.exception.ServiceException; @@ -12,6 +11,7 @@ import com.microservices.mon.k8s.service.IK8sDashboardService; import com.microservices.mon.k8s.service.IMonCommonAsyncService; import com.microservices.mon.k8s.utils.CustomExecutorFactory; import com.microservices.mon.k8s.utils.PromQLConstant; +import com.microservices.mon.k8s.utils.RancherResponseHandler; import com.microservices.system.api.RemoteGatewayService; import com.microservices.system.api.RemoteRancherService; import lombok.extern.slf4j.Slf4j; @@ -134,7 +134,7 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm CustomExecutorFactory.threadPoolExecutor.execute(() -> { try { //调用rancher远程接口获取所有daemonsets列表 - JSONObject daemonsetsJsonObject = remoteRancherService.getK8sDaemonsetsList(cluster_id, SecurityContextHolder.getUserKey()); + JSONObject daemonsetsJsonObject = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sDaemonsetsList(cluster_id, SecurityContextHolder.getUserKey()), "获取DaemonSet类型应用"); if (daemonsetsJsonObject != null && daemonsetsJsonObject.containsKey("data")) { rancherApplicationVoList.addAll(daemonsetsJsonObject.getJSONArray("data").toList(RancherApplicationVo.class)); } @@ -145,7 +145,7 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm CustomExecutorFactory.threadPoolExecutor.execute(() -> { try { //调用rancher远程接口获取所有deploy列表 - JSONObject deployJsonObject = remoteRancherService.getK8sDeployList(cluster_id, SecurityContextHolder.getUserKey()); + JSONObject deployJsonObject = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sDeployList(cluster_id, SecurityContextHolder.getUserKey()), "获取Deploy类型应用"); if (deployJsonObject != null && deployJsonObject.containsKey("data")) { rancherApplicationVoList.addAll(deployJsonObject.getJSONArray("data").toList(RancherApplicationVo.class)); } @@ -156,8 +156,8 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm CustomExecutorFactory.threadPoolExecutor.execute(() -> { try { //调用rancher远程接口获取所有Stateful列表 - JSONObject statefulJsonObject = remoteRancherService - .getK8sStatefulList(cluster_id, SecurityContextHolder.getUserKey()); + JSONObject statefulJsonObject = RancherResponseHandler.handleJsonObject(remoteRancherService + .getK8sStatefulList(cluster_id, SecurityContextHolder.getUserKey()), "获取StatefulSet类型应用"); if (statefulJsonObject != null && statefulJsonObject.containsKey("data")) { rancherApplicationVoList.addAll(statefulJsonObject.getJSONArray("data").toList(RancherApplicationVo.class)); } @@ -195,7 +195,7 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm .collect(Collectors.toList()); if (needPod) { // 获取命名空间下pod列表 - JSONObject podListJson = remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sNamespace.getName(), SecurityContextHolder.getUserKey()); + JSONObject podListJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sNamespace.getName(), SecurityContextHolder.getUserKey()), "获取容器列表"); List rancherPodVoList; if (podListJson != null && podListJson.containsKey("data")) { rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class); @@ -247,7 +247,7 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm try { K8sNamespace k8sNamespace = k8sNamespaceList.get(finalI); // 获取命名空间下pod列表 - JSONObject podListJson = remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sNamespace.getName(), SecurityContextHolder.getUserKey()); + JSONObject podListJson = RancherResponseHandler.handleJsonObject(remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sNamespace.getName(), SecurityContextHolder.getUserKey()), "获取容器列表"); if (podListJson != null && podListJson.containsKey("data")) { List rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class); @@ -262,8 +262,8 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm try { podCountDownLatch.await(); } catch (InterruptedException e) { - log.error("获取可视化面板POD列表失败:{0}", e); - throw new ServiceException("获取可视化面板POD列表失败"); + log.error("获取可视化面板容器列表失败:{0}", e); + throw new ServiceException("获取可视化面板容器列表失败"); } podList.sort(Comparator.comparing(K8sApplicationPod::getName)); podList.sort(Comparator.comparing(K8sApplicationPod::getNamespace)); diff --git a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/MonUtils.java b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/MonUtils.java index a3ad6ec17..803b71ac4 100644 --- a/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/MonUtils.java +++ b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/MonUtils.java @@ -4,6 +4,7 @@ import com.alibaba.cloud.commons.io.IOUtils; import com.alibaba.fastjson2.JSONObject; import com.microservices.common.core.exception.ServiceException; import com.microservices.common.core.utils.DateUtils; +import com.microservices.system.api.utils.FeignUtils; import feign.Response; import lombok.extern.slf4j.Slf4j; @@ -39,18 +40,4 @@ public class MonUtils { double days = hours / 24; return String.format("%.1f 天", days); } - - public static void disposeRancherErrorMsg(Response response, String functionName) { - Response.Body body = response.body(); - try { - String bodyStr = IOUtils.toString(body.asInputStream(), StandardCharsets.UTF_8); - JSONObject bodyJson = JSONObject.parseObject(bodyStr); - if (bodyJson != null && bodyJson.containsKey("message")) { - log.error("{}时发生错误:{}", functionName, bodyJson.toJSONString()); - throw new ServiceException("%s时发生错误,错误原因:%s", functionName, bodyJson.getString("message")); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } } 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 new file mode 100644 index 000000000..46446cde9 --- /dev/null +++ b/microservices-modules/microservices-modules-mon/src/main/java/com/microservices/mon/k8s/utils/RancherResponseHandler.java @@ -0,0 +1,42 @@ +package com.microservices.mon.k8s.utils; + +import com.alibaba.fastjson2.JSONObject; +import com.microservices.common.core.exception.ServiceException; +import com.microservices.system.api.utils.FeignUtils; +import feign.Response; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +@Slf4j +public class RancherResponseHandler { + + public static JSONObject handleJsonObject(Response response, String functionName) { + Response.Body body = response.body(); + if (body != null) { + try { + String bodyStr = IOUtils.toString(body.asInputStream(), StandardCharsets.UTF_8); + JSONObject bodyJson = JSONObject.parseObject(bodyStr); + if (FeignUtils.isSuccess(response)) { + return bodyJson; + } + if (bodyJson != null && bodyJson.containsKey("message")) { + log.error("{}时发生错误:{}", functionName, bodyJson.toJSONString()); + throw new ServiceException("%s时发生错误,错误原因:%s", functionName, bodyJson.getString("message")); + } + } catch (IOException e) { + throw new ServiceException("%s时发生错误,响应数据读取异常", functionName); + } + } + throw new ServiceException("%s时发生错误,请联系系统管理员", functionName); + } + + + public static Boolean handleBoolean(Response response, String functionName) { + JSONObject bodyJson = handleJsonObject(response, functionName); + return bodyJson != null; + } + +}