diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/GovernanceProperties.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/GovernanceProperties.java index 27da65974..976d064c3 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/GovernanceProperties.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/GovernanceProperties.java @@ -112,6 +112,14 @@ public class GovernanceProperties { public static final String WEBMVC_PUBLICKEY_ACLS = PREFIX + "." + "webmvc.public-key.acls"; + public static final String SERVICECOMB_GRASEFUL_UPPER_DOWN = PREFIX + "." + "graceful.servicecombEngine.enabled"; + + public static final String NACOS_GRASEFUL_UPPER_DOWN = PREFIX + "." + "graceful.nacosEngine.enabled"; + + public static final String GRASEFUL_STATUS_UPPER = "UP"; + + public static final String GRASEFUL_STATUS_DOWN = "DOWN"; + public static class Gateway { private RateLimiting rateLimiting = new RateLimiting(); diff --git a/spring-cloud-huawei-nacos/pom.xml b/spring-cloud-huawei-nacos/pom.xml index 9aaf6fc50..ddeaded21 100644 --- a/spring-cloud-huawei-nacos/pom.xml +++ b/spring-cloud-huawei-nacos/pom.xml @@ -42,6 +42,11 @@ com.huaweicloud spring-cloud-huawei-router + + org.springframework.boot + spring-boot-starter-actuator + provided + \ No newline at end of file diff --git a/spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/graceful/NacosGracefulAutoConfiguration.java b/spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/graceful/NacosGracefulAutoConfiguration.java new file mode 100644 index 000000000..7402baea0 --- /dev/null +++ b/spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/graceful/NacosGracefulAutoConfiguration.java @@ -0,0 +1,44 @@ +/* + + * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.nacos.graceful; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled; +import com.alibaba.cloud.nacos.NacosDiscoveryProperties; +import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration; +import com.alibaba.cloud.nacos.registry.NacosRegistration; +import com.alibaba.cloud.nacos.registry.NacosServiceRegistry; +import com.huaweicloud.common.configration.dynamic.GovernanceProperties; + +@Configuration +@ConditionalOnNacosDiscoveryEnabled +@ConditionalOnClass(name = {"org.springframework.boot.actuate.endpoint.annotation.Endpoint"}) +public class NacosGracefulAutoConfiguration { + @Bean + @ConditionalOnProperty(value = GovernanceProperties.NACOS_GRASEFUL_UPPER_DOWN, havingValue = "true") + public NacosGracefulEndpoint nacosGracefulEndpoint(NacosServiceRegistry nacosServiceRegistry, + NacosRegistration nacosRegistration, NacosAutoServiceRegistration nacosAutoServiceRegistration, + NacosDiscoveryProperties nacosDiscoveryProperties) { + return new NacosGracefulEndpoint(nacosServiceRegistry, nacosRegistration, nacosAutoServiceRegistration, + nacosDiscoveryProperties); + } +} diff --git a/spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/graceful/NacosGracefulEndpoint.java b/spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/graceful/NacosGracefulEndpoint.java new file mode 100644 index 000000000..88a8be027 --- /dev/null +++ b/spring-cloud-huawei-nacos/src/main/java/com/huaweicloud/nacos/graceful/NacosGracefulEndpoint.java @@ -0,0 +1,72 @@ +/* + + * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.nacos.graceful; + +import java.util.concurrent.atomic.AtomicBoolean; + +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; +import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; + +import com.alibaba.cloud.nacos.NacosDiscoveryProperties; +import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration; +import com.alibaba.cloud.nacos.registry.NacosRegistration; +import com.alibaba.cloud.nacos.registry.NacosServiceRegistry; +import com.huaweicloud.common.configration.dynamic.GovernanceProperties; + +@Endpoint(id = "nacos-service-registry") +public class NacosGracefulEndpoint { + private static final Logger LOGGER = LoggerFactory.getLogger(NacosGracefulEndpoint.class); + + private final NacosServiceRegistry nacosServiceRegistry; + + private final NacosRegistration nacosRegistration; + + private final NacosAutoServiceRegistration nacosAutoServiceRegistration; + + private final NacosDiscoveryProperties nacosDiscoveryProperties; + + private final AtomicBoolean isRegistry = new AtomicBoolean(); + + public NacosGracefulEndpoint(NacosServiceRegistry nacosServiceRegistry, NacosRegistration nacosRegistration, + NacosAutoServiceRegistration nacosAutoServiceRegistration, NacosDiscoveryProperties nacosDiscoveryProperties) { + this.nacosServiceRegistry = nacosServiceRegistry; + this.nacosRegistration = nacosRegistration; + this.nacosAutoServiceRegistration = nacosAutoServiceRegistration; + this.nacosDiscoveryProperties = nacosDiscoveryProperties; + } + + @WriteOperation + public void gracefulUpperAndDown(@Nullable String status) { + if (StringUtils.isEmpty(status)) { + return; + } + if (GovernanceProperties.GRASEFUL_STATUS_UPPER.equalsIgnoreCase(status) && !isRegistry.getAndSet(true)) { + nacosDiscoveryProperties.setRegisterEnabled(true); + nacosAutoServiceRegistration.start(); + } else if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status) && isRegistry.get()) { + nacosServiceRegistry.deregister(nacosRegistration); + } else { + LOGGER.warn("operation is not allowed, status: " + status); + } + } +} diff --git a/spring-cloud-huawei-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-huawei-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 58acce618..320826143 100644 --- a/spring-cloud-huawei-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-huawei-nacos/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -17,3 +17,4 @@ ## --------------------------------------------------------------------------- com.huaweicloud.nacos.NacosAdaptersAutoConfiguration +com.huaweicloud.nacos.graceful.NacosGracefulAutoConfiguration diff --git a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulAutoConfiguration.java b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulAutoConfiguration.java new file mode 100644 index 000000000..a472dd3ac --- /dev/null +++ b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulAutoConfiguration.java @@ -0,0 +1,40 @@ +/* + + * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.servicecomb.discovery.graceful; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.huaweicloud.common.configration.dynamic.GovernanceProperties; +import com.huaweicloud.servicecomb.discovery.ConditionalOnServiceCombDiscoveryEnabled; +import com.huaweicloud.servicecomb.discovery.registry.ServiceCombRegistration; +import com.huaweicloud.servicecomb.discovery.registry.ServiceCombServiceRegistry; + +@Configuration +@ConditionalOnServiceCombDiscoveryEnabled +@ConditionalOnClass(name = {"org.springframework.boot.actuate.endpoint.annotation.Endpoint"}) +public class ServicecombGracefulAutoConfiguration { + @Bean + @ConditionalOnProperty(value = GovernanceProperties.SERVICECOMB_GRASEFUL_UPPER_DOWN, havingValue = "true") + public ServicecombGracefulEndpoint servicecombGracefulEndpoint(ServiceCombServiceRegistry serviceCombServiceRegistry, + ServiceCombRegistration serviceCombRegistration) { + return new ServicecombGracefulEndpoint(serviceCombServiceRegistry, serviceCombRegistration); + } +} 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 new file mode 100644 index 000000000..dda82f40a --- /dev/null +++ b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/graceful/ServicecombGracefulEndpoint.java @@ -0,0 +1,56 @@ +/* + + * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.servicecomb.discovery.graceful; + +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; +import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; + +import com.huaweicloud.common.configration.dynamic.GovernanceProperties; +import com.huaweicloud.servicecomb.discovery.registry.ServiceCombRegistration; +import com.huaweicloud.servicecomb.discovery.registry.ServiceCombServiceRegistry; + +@Endpoint(id = "servicecomb-service-registry") +public class ServicecombGracefulEndpoint { + private static final Logger LOGGER = LoggerFactory.getLogger(ServicecombGracefulEndpoint.class); + + private final ServiceCombServiceRegistry serviceCombServiceRegistry; + + private final ServiceCombRegistration serviceCombRegistration; + + public ServicecombGracefulEndpoint(ServiceCombServiceRegistry serviceCombServiceRegistry, + ServiceCombRegistration serviceCombRegistration) { + this.serviceCombServiceRegistry = serviceCombServiceRegistry; + this.serviceCombRegistration = serviceCombRegistration; + } + + @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."); + return; + } + serviceCombServiceRegistry.setStatus(serviceCombRegistration, status.toUpperCase()); + } +} diff --git a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index d22140c96..9a748207b 100644 --- a/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-huawei-service-engine/service-engine-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -21,3 +21,4 @@ com.huaweicloud.servicecomb.discovery.discovery.ServiceCombDiscoveryClientConfig com.huaweicloud.servicecomb.discovery.registry.ServiceCombRegistryAutoConfiguration com.huaweicloud.servicecomb.discovery.discovery.ServiceCombReactiveDiscoveryClientConfiguration com.huaweicloud.servicecomb.discovery.check.RegistryHealthIndicatorConfiguration +com.huaweicloud.servicecomb.discovery.graceful.ServicecombGracefulAutoConfiguration