-
Notifications
You must be signed in to change notification settings - Fork 223
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
Changes from all commits
cb00424
b1869ed
0e16a78
5227c3e
0904792
7430a73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if actuator relate class exists and enable on demand There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be provided scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed