Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1088] support graceful upper and down #1089

Merged
merged 6 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-huawei-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-huawei-router</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be provided scope.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if actuator relate class exists and enable on demand

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

public NacosGracefulEndpoint nacosGracefulEndpoint(NacosServiceRegistry nacosServiceRegistry,
NacosRegistration nacosRegistration, NacosAutoServiceRegistration nacosAutoServiceRegistration,
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosGracefulEndpoint(nacosServiceRegistry, nacosRegistration, nacosAutoServiceRegistration,
nacosDiscoveryProperties);
}
}
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RegisterEnabled 可能产生二义性。 用户配置了这个配置项, 目的是不启用注册中心,还是需要使用这个特性? 在这个特性之前是前者。 所以使用该配置项, 会丢失特性

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registerEnabled是false的时候,AbstractAutoServiceRegistration的start方法中在第一个isEnabled()条件判断处直接返回,不会更新registion中的端口号,需要重新启动start方法去完成注册。

nacosAutoServiceRegistration.start();
} else if (GovernanceProperties.GRASEFUL_STATUS_DOWN.equalsIgnoreCase(status) && isRegistry.get()) {
nacosServiceRegistry.deregister(nacosRegistration);
} else {
LOGGER.warn("operation is not allowed, status: " + status);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
## ---------------------------------------------------------------------------

com.huaweicloud.nacos.NacosAdaptersAutoConfiguration
com.huaweicloud.nacos.graceful.NacosGracefulAutoConfiguration
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading