From 67b13d3894f2d28fccd53feca63170ac406bc9f0 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 1 Aug 2023 10:50:01 +0800 Subject: [PATCH] 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"; + } + +}