refactor(运维监控模块): 统一处理Rancher接口异常信息

1. 将所有Rancher远程调用接口的响应调整为Response
2. 在监控运维服务中对Rancher接口响应进行统一处理(携带操作方法名称作为入参)
- 当Rancher远程接口响应Body为空时抛出异常:[xx操作]时发生错误,请联系系统管理员
- 读取Body内容并转换为JSONObject类型,
- 当响应状态码为2xx时,若调用方需要JSONObject类型则直接返回,若需要Boolean类型则返回true
- 当响应状态码不是为2xx时,则解析body对象,读取message字段作为异常信息返回给用户
This commit is contained in:
OTTO 2025-07-25 09:25:00 +08:00
parent 0c94e636e1
commit ddb1a06acc
8 changed files with 155 additions and 180 deletions

View File

@ -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);
}

View File

@ -28,68 +28,57 @@ public class RemoteGatewayFallbackFactory implements FallbackFactory<RemoteGatew
@Override
public Response loginSentinel(String username, String password) {
log.error("网关接口调用:登录流量控制服务失败;异常信息:{}", throwable.getMessage());
throw new ServiceException("接口调用:登录流量控制服务失败");
return null;
}
@Override
public Response loginRancher(JSONObject loginBody) {
log.error("网关接口调用登录Rancher失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用登录Rancher失败");
return null;
}
@Override
public Response loginNacos(Map<String, ?> 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;
}
};
}

View File

@ -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<RemoteRanch
@Override
public RemoteRancherService create(Throwable throwable) {
log.error("Rancher服务调用失败:{}", throwable.getMessage());
return new RemoteRancherService() {
@Override
public JSONObject getK8sNamespaceList(String cluster_id, String token) {
log.error("Rancher接口调用获取K8S命名空间列表失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取K8S命名空间列表失败");
public Response getK8sNamespaceList(String cluster_id, String token) {
return null;
}
@Override
public JSONObject getK8sDaemonsetsList(String cluster_id, String token) {
log.error("Rancher接口调用获取K8S DaemonSet类型应用失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取K8S DaemonSet类型应用失败");
public Response getK8sDaemonsetsList(String cluster_id, String token) {
return null;
}
@Override
public JSONObject getK8sDeployList(String cluster_id, String token) {
log.error("Rancher接口调用获取K8S Deployment类型应用失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取K8S Deployment类型应用失败");
public Response getK8sDeployList(String cluster_id, String token) {
return null;
}
@Override
public JSONObject getK8sStatefulList(String cluster_id, String token) {
log.error("Rancher接口调用获取K8S StatefulSet类型应用失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取K8S StatefulSet类型应用失败");
public Response getK8sStatefulList(String cluster_id, String token) {
return null;
}
@Override
public JSONObject getK8sClusterNodeList(String cluster_id, String token) {
log.error("Rancher接口调用获取K8S集群节点列表失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取K8S集群节点列表失败");
public Response getK8sClusterNodeList(String cluster_id, String token) {
return null;
}
@Override
public JSONObject getK8sApplicationDetail(String cluster_id, String fullKind, String namespace, String applicationName, String token) {
log.error("Rancher接口调用获取K8S应用详情失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取K8S应用详情失败");
public Response getK8sApplicationDetail(String cluster_id, String fullKind, String namespace, String applicationName, String token) {
return null;
}
@Override
public JSONObject getK8sPodListByNamespace(String cluster_id, String namespace, String token) {
log.error("Rancher接口调用根据命名空间获取K8S容器列表失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用根据命名空间获取K8S容器列表失败");
public Response getK8sPodListByNamespace(String cluster_id, String namespace, String token) {
return null;
}
@Override
public JSONObject getK8sServiceByApplicationId(String cluster_id, String nginxApplicationId, String token) {
log.error("Rancher接口调用根据应用Id获取K8S服务失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用根据应用Id获取K8S服务失败");
public Response getK8sServiceByApplicationId(String cluster_id, String nginxApplicationId, String token) {
return null;
}
@Override
public Response deletePod(String namespace, String podName, JSONObject deletePodBody, String token) {
log.error("Rancher接口调用删除容器失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用:删除容器失败");
return null;
}
@Override
public Response updateK8sApplication(String fullKind, String namespace, String applicationName, JSONObject applicationDetail, String userKey) {
log.error("Rancher接口调用更新K8S应用失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用更新K8S应用失败");
return null;
}
@Override
public Response createK8sApplication(String fullName, String yamlContent, String userKey) {
log.error("Rancher接口调用创建K8S应用失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用创建K8S应用失败");
return null;
}
@Override
public JSONObject getApplicationYamlFile(String kindName, String namespace, String applicationName, String token) {
log.error("Rancher接口调用获取应用Yaml文件异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取应用Yaml文件");
public Response getApplicationYamlFile(String kindName, String namespace, String applicationName, String token) {
return null;
}
@Override
public Response editK8sApplication(String fullKind, String namespace, String applicationName, String yamlContent, String token) {
log.error("Rancher接口调用编辑K8S应用Yaml文件失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用编辑K8S应用Yaml文件失败");
return null;
}
@Override
public Response deleteApplication(String fullKind, String namespace, String applicationName, String token) {
log.error("Rancher接口调用删除应用失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用:删除应用失败");
return null;
}
@Override
public JSONObject getK8sConfigmapByNamespace(String namespace, String token) {
log.error("Rancher接口调用获取命名空间下Configmap失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取命名空间下Configmap失败");
public Response getK8sConfigmapByNamespace(String namespace, String token) {
return null;
}
@Override
public JSONObject getK8sSecretByNamespace(String namespace, String token) {
log.error("Rancher接口调用获取命名空间下Secret失败异常信息{}", throwable.getMessage());
throw new ServiceException("接口调用获取命名空间下Secret失败");
public Response getK8sSecretByNamespace(String namespace, String token) {
return null;
}
};
}

View File

@ -10,6 +10,7 @@ import com.microservices.mon.k8s.domain.vo.*;
import com.microservices.mon.k8s.service.IK8sDashboardService;
import com.microservices.mon.k8s.service.IMonCommonAsyncService;
import com.microservices.mon.k8s.utils.PromQLConstant;
import com.microservices.mon.k8s.utils.RancherFeignUtils;
import com.microservices.mon.k8s.utils.SkywalkingGQLConstant;
import com.microservices.system.api.RemoteGatewayService;
import com.microservices.system.api.RemoteRancherService;
@ -38,7 +39,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo
public List<K8sClusterNode> getClusterNodeList() {
List<RancherClusterNodeVo> rancherClusterNodeVoList;
//调用rancher远程接口获取集群节点列表
JSONObject clusterNodeJsonObject = remoteRancherService.getK8sClusterNodeList(cluster_id, SecurityContextHolder.getUserKey());
JSONObject clusterNodeJsonObject = RancherFeignUtils.getResponseBody(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<String> 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 = RancherFeignUtils.getResponseBody(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 = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sPodListByNamespace(cluster_id, namespace, SecurityContextHolder.getUserKey()), "获取容器列表");
if (podListJson != null && podListJson.containsKey("data")) {
List<RancherPodVo> rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class);
List<RancherPodVo> applicationToPodList = rancherPodVoList.stream()
@ -104,7 +105,7 @@ public class K8SDashboardServiceImpl extends CommonService implements IK8sDashbo
public List<ApplicationRequestCountData> getApplicationRequestCount() {
List<ApplicationRequestCountData> applicationRequestCountDataList = new ArrayList<>();
HashMap<String, String> portMap = new HashMap<>();
JSONObject nginxSvcJson = remoteRancherService.getK8sServiceByApplicationId(cluster_id, nginxApplicationId, SecurityContextHolder.getUserKey());
JSONObject nginxSvcJson = RancherFeignUtils.getResponseBody(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<ApplicationFunction> getApplicationFunction(String kind, String namespace, String applicationName, String podIpList) {
ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind);
List<ApplicationFunction> applicationFunctionsList = new ArrayList<>();
JSONObject applicationJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey());
JSONObject applicationJson = RancherFeignUtils.getResponseBody(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<K8sNamespace> k8sNamespaceList = new ArrayList<>();
List<RancherNamespaceVo> rancherNamespaceVoList;
//调用rancher远程接口获取所有命名空间列表
JSONObject namespaceJsonObject = remoteRancherService.getK8sNamespaceList(cluster_id, SecurityContextHolder.getUserKey());
JSONObject namespaceJsonObject = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sNamespaceList(cluster_id, SecurityContextHolder.getUserKey()), "获取K8S命名空间列表");
if (namespaceJsonObject != null && namespaceJsonObject.containsKey("data")) {
rancherNamespaceVoList = namespaceJsonObject.getJSONArray("data").toList(RancherNamespaceVo.class);
for (RancherNamespaceVo rancherNamespaceVo : rancherNamespaceVoList) {

View File

@ -12,6 +12,7 @@ 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.RancherFeignUtils;
import com.microservices.system.api.RemoteRancherService;
import com.microservices.system.api.utils.FeignUtils;
import feign.Response;
@ -47,7 +48,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 = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情");
if (applicationJson == null) {
return k8sApplication;
}
@ -69,18 +70,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 RancherFeignUtils.disposeRancherErrorMsg(response, "删除容器");
}
return false;
}
@Override
public List<K8sApplicationPod> getK8sApplicationPodList(K8sApplication k8sApplication) {
// 获取命名空间下pod列表
JSONObject podListJson = remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sApplication.getNamespace(), SecurityContextHolder.getUserKey());
JSONObject podListJson = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sApplication.getNamespace(), SecurityContextHolder.getUserKey()), "获取应用下容器列表");
List<RancherPodVo> rancherPodVoList;
if (podListJson != null && podListJson.containsKey("data")) {
rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class);
@ -110,7 +107,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 = RancherFeignUtils.getResponseBody(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 +134,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 RancherFeignUtils.disposeRancherErrorMsg(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 RancherFeignUtils.disposeRancherErrorMsg(response, "启动应用");
}
}
case FORCE_STOP: {
@ -170,9 +161,8 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
}
return delPodSuccess;
}
MonUtils.disposeRancherErrorMsg(response, "强制停止应用");
return RancherFeignUtils.disposeRancherErrorMsg(response, "启动应用");
}
break;
}
case RESTART: {
if (metadataJson.containsKey("annotations")) {
@ -203,10 +193,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 RancherFeignUtils.disposeRancherErrorMsg(response, "启动应用");
}
}
@ -216,20 +203,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 RancherFeignUtils.disposeRancherErrorMsg(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 RancherFeignUtils.disposeRancherErrorMsg(response, "启动应用");
}
}
}
@ -253,49 +234,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 RancherFeignUtils.disposeRancherErrorMsg(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 RancherFeignUtils.getResponseBody(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 RancherFeignUtils.disposeRancherErrorMsg(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 RancherFeignUtils.disposeRancherErrorMsg(response, "启动应用");
}
return false;
}
@Override
public List<K8sApplicationEnv> getApplicationEnv(String kind, String namespace, String applicationName) {
List<K8sApplicationEnv> envList = new ArrayList<>();
ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind);
JSONObject applicationJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey());
JSONObject applicationJson = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey()), "获取应用详情");
if (applicationJson == null) {
return envList;
}
@ -336,7 +305,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
@Override
public List<K8sApplicationEnvOptionalParameter> getEnvOptionalParameters(String namespace) {
List<K8sApplicationEnvOptionalParameter> envOptionalParametersList = new ArrayList<>();
JSONObject configmapJson = remoteRancherService.getK8sConfigmapByNamespace(namespace, SecurityContextHolder.getUserKey());
JSONObject configmapJson = RancherFeignUtils.getResponseBody(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 +327,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
}
}
}
JSONObject secretJson = remoteRancherService.getK8sSecretByNamespace(namespace, SecurityContextHolder.getUserKey());
JSONObject secretJson = RancherFeignUtils.getResponseBody(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 +369,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
@Override
public Boolean updateApplicationEnv(String kind, String namespace, String applicationName, List<K8sApplicationEnv> k8sApplicationEnvList) {
ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind);
JSONObject applicationDetailJson = remoteRancherService.getK8sApplicationDetail(cluster_id, applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey());
JSONObject applicationDetailJson = RancherFeignUtils.getResponseBody(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 +398,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 RancherFeignUtils.disposeRancherErrorMsg(response, "启动应用");
}
return false;
}
@Override

View File

@ -12,6 +12,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.RancherFeignUtils;
import com.microservices.system.api.RemoteGatewayService;
import com.microservices.system.api.RemoteRancherService;
import lombok.extern.slf4j.Slf4j;
@ -134,7 +135,7 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm
CustomExecutorFactory.threadPoolExecutor.execute(() -> {
try {
//调用rancher远程接口获取所有daemonsets列表
JSONObject daemonsetsJsonObject = remoteRancherService.getK8sDaemonsetsList(cluster_id, SecurityContextHolder.getUserKey());
JSONObject daemonsetsJsonObject = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sDaemonsetsList(cluster_id, SecurityContextHolder.getUserKey()), "获取DaemonSet类型应用");
if (daemonsetsJsonObject != null && daemonsetsJsonObject.containsKey("data")) {
rancherApplicationVoList.addAll(daemonsetsJsonObject.getJSONArray("data").toList(RancherApplicationVo.class));
}
@ -145,7 +146,7 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm
CustomExecutorFactory.threadPoolExecutor.execute(() -> {
try {
//调用rancher远程接口获取所有deploy列表
JSONObject deployJsonObject = remoteRancherService.getK8sDeployList(cluster_id, SecurityContextHolder.getUserKey());
JSONObject deployJsonObject = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sDeployList(cluster_id, SecurityContextHolder.getUserKey()), "获取Deploy类型应用");
if (deployJsonObject != null && deployJsonObject.containsKey("data")) {
rancherApplicationVoList.addAll(deployJsonObject.getJSONArray("data").toList(RancherApplicationVo.class));
}
@ -156,8 +157,8 @@ public class MonCommonAsyncServiceImpl extends CommonService implements IMonComm
CustomExecutorFactory.threadPoolExecutor.execute(() -> {
try {
//调用rancher远程接口获取所有Stateful列表
JSONObject statefulJsonObject = remoteRancherService
.getK8sStatefulList(cluster_id, SecurityContextHolder.getUserKey());
JSONObject statefulJsonObject = RancherFeignUtils.getResponseBody(remoteRancherService
.getK8sStatefulList(cluster_id, SecurityContextHolder.getUserKey()), "获取StatefulSet类型应用");
if (statefulJsonObject != null && statefulJsonObject.containsKey("data")) {
rancherApplicationVoList.addAll(statefulJsonObject.getJSONArray("data").toList(RancherApplicationVo.class));
}
@ -195,7 +196,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 = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sNamespace.getName(), SecurityContextHolder.getUserKey()), "获取容器列表");
List<RancherPodVo> rancherPodVoList;
if (podListJson != null && podListJson.containsKey("data")) {
rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class);
@ -247,7 +248,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 = RancherFeignUtils.getResponseBody(remoteRancherService.getK8sPodListByNamespace(cluster_id, k8sNamespace.getName(), SecurityContextHolder.getUserKey()), "获取容器列表");
if (podListJson != null && podListJson.containsKey("data")) {
List<RancherPodVo> rancherPodVoList = podListJson.getJSONArray("data").toList(RancherPodVo.class);
@ -262,8 +263,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));

View File

@ -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);
}
}
}

View File

@ -0,0 +1,44 @@
package com.microservices.mon.k8s.utils;
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;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
@Slf4j
public class RancherFeignUtils {
public static JSONObject getResponseBody(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 disposeRancherErrorMsg(Response response, String functionName) {
JSONObject bodyJson = getResponseBody(response, functionName);
return bodyJson != null;
}
}