From 483c391791d01a6f552439adba354e6adc9851a7 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 1 Aug 2023 10:50:01 +0800 Subject: [PATCH 1/2] fix:fix web server wrong open bug. --- .../polaris/registry/PolarisRegistration.java | 28 +++++-- .../registry/PolarisServiceRegistry.java | 1 + .../PolarisRegistrationCustomizerTest.java | 2 + spring-cloud-tencent-commons/pom.xml | 20 +++++ .../cloud/common/constant/SdkVersion.java | 80 +++++++++++++++++++ 5 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/SdkVersion.java diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 1bdf19d16..0a3d09ae8 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -44,7 +44,7 @@ import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFA /** * Registration object of Polaris. * - * @author Haotian Zhang, Andrew Shan, Jie Cheng, changjin wei(魏昌进) + * @author Haotian Zhang, Andrew Shan, Jie Cheng, Palmer.Xu, changjin wei(魏昌进) */ public class PolarisRegistration implements Registration { @@ -64,6 +64,7 @@ public class PolarisRegistration implements Registration { private final boolean isSecure; private final ServletWebServerApplicationContext servletWebServerApplicationContext; private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext; + private final List customizers; private boolean registerEnabled = false; private Map metadata; private int port; @@ -76,12 +77,14 @@ public class PolarisRegistration implements Registration { SDKContext context, StaticMetadataManager staticMetadataManager, @Nullable NacosContextProperties nacosContextProperties, @Nullable ServletWebServerApplicationContext servletWebServerApplicationContext, - @Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) { + @Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext, + @Nullable List registrationCustomizers) { this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisContext = context; this.staticMetadataManager = staticMetadataManager; this.servletWebServerApplicationContext = servletWebServerApplicationContext; this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext; + this.customizers = registrationCustomizers; // generate serviceId if (Objects.isNull(nacosContextProperties)) { @@ -151,15 +154,15 @@ public class PolarisRegistration implements Registration { @Nullable List registrationCustomizers) { PolarisRegistration polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties, context, staticMetadataManager, - nacosContextProperties, servletWebServerApplicationContext, reactiveWebServerApplicationContext); - customize(registrationCustomizers, polarisRegistration); + nacosContextProperties, servletWebServerApplicationContext, reactiveWebServerApplicationContext, + registrationCustomizers); return polarisRegistration; } - public static void customize(List registrationCustomizers, PolarisRegistration registration) { - if (registrationCustomizers != null) { - for (PolarisRegistrationCustomizer customizer : registrationCustomizers) { - customizer.customize(registration); + public void customize() { + if (!CollectionUtils.isEmpty(this.customizers)) { + for (PolarisRegistrationCustomizer customizer : this.customizers) { + customizer.customize(this); } } } @@ -174,6 +177,11 @@ public class PolarisRegistration implements Registration { return host; } + /** + * Should be call after web started. + * + * @return port + */ @Override public int getPort() { if (port <= 0) { @@ -190,6 +198,10 @@ public class PolarisRegistration implements Registration { return port; } + public void setPort(int port) { + this.port = port; + } + @Override public boolean isSecure() { return isSecure; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 557b10735..ebc16be98 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -101,6 +101,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { + PolarisRegistration polarisRegistration = context.getBean(PolarisRegistration.class); + polarisRegistration.customize(); PolarisRegistrationCustomizer customizer = context.getBean(PolarisRegistrationCustomizer.class); verify(customizer, times(1)).customize(any(PolarisRegistration.class)); }); diff --git a/spring-cloud-tencent-commons/pom.xml b/spring-cloud-tencent-commons/pom.xml index 29cd2b0ec..6cc5556be 100644 --- a/spring-cloud-tencent-commons/pom.xml +++ b/spring-cloud-tencent-commons/pom.xml @@ -112,4 +112,24 @@ test + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + + + SCT_SDK_VERSION + ${project.version} + + + + + + diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/SdkVersion.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/SdkVersion.java new file mode 100644 index 000000000..b3f9348b9 --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/SdkVersion.java @@ -0,0 +1,80 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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.tencent.cloud.common.constant; + +import java.lang.invoke.MethodHandles; +import java.net.URL; +import java.util.Enumeration; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Constant for SDK version. + * + * @author Haotian Zhang + */ +public final class SdkVersion { + + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + /** + * SDK version key. + * When packaging through the Maven plug-in, the version information of the Core package is written into META-INF, + * and the SDK version is registered in the registration center when the service is registered. + * Use with maven-jar-plugin in the pom.xml file + */ + public static String SDK_VERSION_KEY = "SCT_SDK_VERSION"; + private static String version; + + private SdkVersion() { + } + + public static String get() { + if (version != null) { + LOG.info("SDK SDK Version: {}", version); + return version; + } + try { + Enumeration resources = SdkVersion.class.getClassLoader().getResources("META-INF/MANIFEST.MF"); + while (resources.hasMoreElements()) { + Manifest manifest = new Manifest(resources.nextElement().openStream()); + Attributes attrs = manifest.getMainAttributes(); + if (attrs == null) { + continue; + } + String name = attrs.getValue("Bundle-Name"); + if (SDK_VERSION_KEY.equals(name)) { + String version = attrs.getValue("Bundle-Version"); + LOG.info("SCT SDK Version: {}", version); + SdkVersion.version = version; + break; + } + } + return version; + } + catch (Exception exception) { + // ignore it + } + LOG.info("could not get bundle : '{}' version, please check MANIFEST.MF format." + + " return default version: UNKNOWN", SDK_VERSION_KEY); + return "UNKNOWN"; + } + +} From 701684ee853ab01a185f49a59c93df27057c7ae8 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 1 Aug 2023 20:27:15 +0800 Subject: [PATCH 2/2] fix:add deregister judgment. (#1075) --- CHANGELOG.md | 1 + .../PolarisAutoServiceRegistration.java | 16 +++++++++++ .../PolarisAutoServiceRegistrationTest.java | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8da8c490e..05ebfe608 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,3 +22,4 @@ - [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1065) - [fix:fix gray release examples bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1068) - [fix:fix router label feign interceptor order.](https://github.com/Tencent/spring-cloud-tencent/pull/1071) +- [fix:add deregister judgment.](https://github.com/Tencent/spring-cloud-tencent/pull/1075) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java index c1d4e9203..96b359096 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java @@ -87,6 +87,22 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat super.registerManagement(); } + @Override + protected void deregister() { + if (!this.registration.isRegisterEnabled()) { + return; + } + super.deregister(); + } + + @Override + protected void deregisterManagement() { + if (!this.registration.isRegisterEnabled()) { + return; + } + super.deregisterManagement(); + } + @Override protected Object getConfiguration() { return this.polarisDiscoveryProperties; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java index 9d78c9033..e1c04a29b 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java @@ -71,6 +71,7 @@ public class PolarisAutoServiceRegistrationTest { @BeforeEach void setUp() { doNothing().when(serviceRegistry).register(nullable(PolarisRegistration.class)); + doNothing().when(serviceRegistry).deregister(nullable(PolarisRegistration.class)); polarisAutoServiceRegistration = new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration, @@ -111,6 +112,32 @@ public class PolarisAutoServiceRegistrationTest { }).doesNotThrowAnyException(); } + @Test + public void testDeregister() { + doReturn(false).when(registration).isRegisterEnabled(); + assertThatCode(() -> { + polarisAutoServiceRegistration.registerManagement(); + }).doesNotThrowAnyException(); + + doReturn(true).when(registration).isRegisterEnabled(); + assertThatCode(() -> { + polarisAutoServiceRegistration.deregister(); + }).doesNotThrowAnyException(); + } + + @Test + public void testDeregisterManagement() { + doReturn(false).when(registration).isRegisterEnabled(); + assertThatCode(() -> { + polarisAutoServiceRegistration.registerManagement(); + }).doesNotThrowAnyException(); + + doReturn(true).when(registration).isRegisterEnabled(); + assertThatCode(() -> { + polarisAutoServiceRegistration.deregisterManagement(); + }).doesNotThrowAnyException(); + } + @Test public void testGetAppName() { doReturn("application").when(environment).getProperty(anyString(), anyString());