From 819f16ba452f5b139f758db888055afaf0bc8f85 Mon Sep 17 00:00:00 2001 From: Alex <97039406+chengyouling@users.noreply.github.com> Date: Fri, 16 Aug 2024 11:15:39 +0800 Subject: [PATCH] [#1368] add flag control graceful invalid call service-center api (#1369) (#1374) --- .../graceful/NacosGracefulEndpoint.java | 18 +++++++------- .../NacosAutoServiceRegistration.java | 2 +- .../registry/NacosServiceRegistry.java | 1 + .../graceful/ServicecombGracefulEndpoint.java | 24 +++++++++++++++---- .../registry/ServiceCombServiceRegistry.java | 1 + 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/graceful/NacosGracefulEndpoint.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/graceful/NacosGracefulEndpoint.java index d6026ffd7..0b9561910 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/graceful/NacosGracefulEndpoint.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/graceful/NacosGracefulEndpoint.java @@ -19,7 +19,6 @@ import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; @@ -49,16 +48,19 @@ public NacosGracefulEndpoint(NacosServiceRegistry nacosServiceRegistry, NacosReg @WriteOperation public void gracefulUpperAndDown(@Nullable String status) { - if (StringUtils.isEmpty(status)) { - return; - } - if (GovernanceProperties.GRASEFUL_STATUS_UPPER.equalsIgnoreCase(status)) { + if (GovernanceProperties.GRASEFUL_STATUS_UPPER.equalsIgnoreCase(status) + && !nacosAutoServiceRegistration.isEnabled()) { nacosAutoServiceRegistration.setRegistryEnabled(true); nacosAutoServiceRegistration.registryExtend(); - } else if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status)) { + return; + } + if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status) + && nacosAutoServiceRegistration.isEnabled()) { nacosServiceRegistry.deregister(nacosRegistration); - } else { - LOGGER.warn("operation is not allowed, status: " + status); + nacosAutoServiceRegistration.setRegistryEnabled(false); + return; } + LOGGER.info("operation is not allowed, status: " + status + ", registration_enabled: " + + nacosAutoServiceRegistration.isEnabled()); } } diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosAutoServiceRegistration.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosAutoServiceRegistration.java index e767446f4..e07f614b5 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosAutoServiceRegistration.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosAutoServiceRegistration.java @@ -88,7 +88,7 @@ protected Object getConfiguration() { } @Override - protected boolean isEnabled() { + public boolean isEnabled() { return this.registryEnabled; } diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java index eba82dfa8..0bea8f836 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java @@ -126,6 +126,7 @@ public void deregister(Registration registration) { serviceManager.getNamingService().deregisterInstance(serviceId, group, registration.getHost(), registration.getPort(), nacosDiscoveryProperties.getClusterName()); successCount++; + LOGGER.info("nacos de-register {} {}:{} finished", serviceId, instance.getIp(), instance.getPort()); } catch (Exception e) { LOGGER.error("de-register service [{}] from Nacos Server [{}] failed.", registration.getServiceId(), serviceManager.getServerAddr(), e); diff --git a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulEndpoint.java b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulEndpoint.java index 76236310e..660066474 100644 --- a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulEndpoint.java +++ b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulEndpoint.java @@ -19,7 +19,8 @@ import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.servicecomb.service.center.client.model.MicroserviceInstanceStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; @@ -46,11 +47,24 @@ public ServicecombGracefulEndpoint(ServiceCombServiceRegistry serviceCombService @WriteOperation public void gracefulUpperAndDown(@Nullable String status) { if (StringUtils.isEmpty(status) - || (!GovernanceProperties.GRASEFUL_STATUS_UPPER.equalsIgnoreCase(status) - && !GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status))) { - LOGGER.warn("status input " + status + " is not a valid value."); + || StringUtils.isEmpty(serviceCombRegistration.getMicroserviceInstance().getServiceId()) + || StringUtils.isEmpty(serviceCombRegistration.getMicroserviceInstance().getInstanceId())) { + LOGGER.info("operation is not allowed, status is null or registration is not ok."); return; } - serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase()); + if (GovernanceProperties.GRASEFUL_STATUS_UPPER.equalsIgnoreCase(status) + && MicroserviceInstanceStatus.DOWN == serviceCombRegistration.getMicroserviceInstance().getStatus()) { + serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase()); + LOGGER.info("servicecomb graceful update status success, status: " + status); + return; + } + if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status) + && MicroserviceInstanceStatus.UP == serviceCombRegistration.getMicroserviceInstance().getStatus()) { + serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase()); + LOGGER.info("servicecomb graceful update status success, status: " + status); + return; + } + LOGGER.info("operation is not allowed, status: " + status + ", instanceStatus: " + + serviceCombRegistration.getMicroserviceInstance().getStatus()); } } diff --git a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/registry/ServiceCombServiceRegistry.java b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/registry/ServiceCombServiceRegistry.java index 617f092ce..2ee0784f6 100644 --- a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/registry/ServiceCombServiceRegistry.java +++ b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/registry/ServiceCombServiceRegistry.java @@ -148,6 +148,7 @@ public void setStatus(ServiceCombRegistration registration, String status) { try { serviceCenterClient.updateMicroserviceInstanceStatus(registration.getMicroserviceInstance().getServiceId(), registration.getMicroserviceInstance().getInstanceId(), MicroserviceInstanceStatus.valueOf(status)); + registration.getMicroserviceInstance().setStatus(MicroserviceInstanceStatus.valueOf(status)); } catch (OperationException e) { LOGGER.error("setStatus failed", e); }