Skip to content

Commit

Permalink
[#1368] add flag control graceful invalid call service-center api (#1369
Browse files Browse the repository at this point in the history
) (#1374)
  • Loading branch information
chengyouling authored Aug 16, 2024
1 parent cd35831 commit 819f16b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected Object getConfiguration() {
}

@Override
protected boolean isEnabled() {
public boolean isEnabled() {
return this.registryEnabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 819f16b

Please sign in to comment.