Skip to content

Commit

Permalink
[#1368] add flag control graceful invalid call service-center api
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyouling committed Aug 15, 2024
1 parent 192c571 commit 7e617b1
Show file tree
Hide file tree
Showing 2 changed files with 42 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 All @@ -40,25 +39,38 @@ public class NacosGracefulEndpoint {

private final NacosAutoServiceRegistration nacosAutoServiceRegistration;

private boolean register_enabled = false;

private boolean deregister_enabled = false;

public NacosGracefulEndpoint(NacosServiceRegistry nacosServiceRegistry, NacosRegistration nacosRegistration,
NacosAutoServiceRegistration nacosAutoServiceRegistration) {
this.nacosServiceRegistry = nacosServiceRegistry;
this.nacosRegistration = nacosRegistration;
this.nacosAutoServiceRegistration = nacosAutoServiceRegistration;
if (nacosRegistration.getNacosDiscoveryProperties().isRegisterEnabled()) {
deregister_enabled = true;
} else {
register_enabled = true;
}
}

@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) && register_enabled) {
nacosAutoServiceRegistration.setRegistryEnabled(true);
nacosAutoServiceRegistration.registryExtend();
} else if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status)) {
register_enabled = false;
deregister_enabled = true;
return;
}
if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status) && deregister_enabled) {
nacosServiceRegistry.deregister(nacosRegistration);
} else {
LOGGER.warn("operation is not allowed, status: " + status);
register_enabled = true;
deregister_enabled = false;
return;
}
LOGGER.warn("operation is not allowed, status: " + status + ", register_enabled: " + register_enabled
+ ", deregister_enabled: " + deregister_enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import javax.annotation.Nullable;

import org.apache.commons.lang.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 @@ -37,20 +37,36 @@ public class ServicecombGracefulEndpoint {

private final ServiceCombRegistration serviceCombRegistration;

private boolean up_enabled = false;

private boolean down_enabled = false;

public ServicecombGracefulEndpoint(ServiceCombServiceRegistry serviceCombServiceRegistry,
ServiceCombRegistration serviceCombRegistration) {
this.serviceCombServiceRegistry = serviceCombServiceRegistry;
this.serviceCombRegistration = serviceCombRegistration;
if (MicroserviceInstanceStatus.DOWN == serviceCombRegistration.getMicroserviceInstance().getStatus()) {
up_enabled = true;
} else {
down_enabled = true;
}
}

@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.");
if (GovernanceProperties.GRASEFUL_STATUS_UPPER.equalsIgnoreCase(status) && up_enabled) {
serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase());
up_enabled = false;
down_enabled = true;
return;
}
if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status) && down_enabled) {
serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase());
up_enabled = true;
down_enabled = false;
return;
}
serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase());
LOGGER.warn("operation is not allowed, status: " + status + ", up_enabled: " + up_enabled
+ ", down_enabled: " + down_enabled);
}
}

0 comments on commit 7e617b1

Please sign in to comment.