fix:fix web server wrong open bug.

pull/1076/head
Haotian Zhang 1 year ago
parent 7c2528c05f
commit 663f3395c9

@ -44,7 +44,7 @@ import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFA
/** /**
* Registration object of Polaris. * 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 { public class PolarisRegistration implements Registration {
@ -64,6 +64,7 @@ public class PolarisRegistration implements Registration {
private final boolean isSecure; private final boolean isSecure;
private final ServletWebServerApplicationContext servletWebServerApplicationContext; private final ServletWebServerApplicationContext servletWebServerApplicationContext;
private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext; private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext;
private final List<PolarisRegistrationCustomizer> customizers;
private boolean registerEnabled = false; private boolean registerEnabled = false;
private Map<String, String> metadata; private Map<String, String> metadata;
private int port; private int port;
@ -76,12 +77,14 @@ public class PolarisRegistration implements Registration {
SDKContext context, StaticMetadataManager staticMetadataManager, SDKContext context, StaticMetadataManager staticMetadataManager,
@Nullable NacosContextProperties nacosContextProperties, @Nullable NacosContextProperties nacosContextProperties,
@Nullable ServletWebServerApplicationContext servletWebServerApplicationContext, @Nullable ServletWebServerApplicationContext servletWebServerApplicationContext,
@Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) { @Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext,
@Nullable List<PolarisRegistrationCustomizer> registrationCustomizers) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.polarisContext = context; this.polarisContext = context;
this.staticMetadataManager = staticMetadataManager; this.staticMetadataManager = staticMetadataManager;
this.servletWebServerApplicationContext = servletWebServerApplicationContext; this.servletWebServerApplicationContext = servletWebServerApplicationContext;
this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext; this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext;
this.customizers = registrationCustomizers;
// generate serviceId // generate serviceId
if (Objects.isNull(nacosContextProperties)) { if (Objects.isNull(nacosContextProperties)) {
@ -151,15 +154,15 @@ public class PolarisRegistration implements Registration {
@Nullable List<PolarisRegistrationCustomizer> registrationCustomizers) { @Nullable List<PolarisRegistrationCustomizer> registrationCustomizers) {
PolarisRegistration polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties, PolarisRegistration polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties,
polarisContextProperties, consulContextProperties, context, staticMetadataManager, polarisContextProperties, consulContextProperties, context, staticMetadataManager,
nacosContextProperties, servletWebServerApplicationContext, reactiveWebServerApplicationContext); nacosContextProperties, servletWebServerApplicationContext, reactiveWebServerApplicationContext,
customize(registrationCustomizers, polarisRegistration); registrationCustomizers);
return polarisRegistration; return polarisRegistration;
} }
public static void customize(List<PolarisRegistrationCustomizer> registrationCustomizers, PolarisRegistration registration) { public void customize() {
if (registrationCustomizers != null) { if (!CollectionUtils.isEmpty(this.customizers)) {
for (PolarisRegistrationCustomizer customizer : registrationCustomizers) { for (PolarisRegistrationCustomizer customizer : this.customizers) {
customizer.customize(registration); customizer.customize(this);
} }
} }
} }
@ -174,6 +177,11 @@ public class PolarisRegistration implements Registration {
return host; return host;
} }
/**
* Should be call after web started.
*
* @return port
*/
@Override @Override
public int getPort() { public int getPort() {
if (port <= 0) { if (port <= 0) {
@ -190,6 +198,10 @@ public class PolarisRegistration implements Registration {
return port; return port;
} }
public void setPort(int port) {
this.port = port;
}
@Override @Override
public boolean isSecure() { public boolean isSecure() {
return isSecure; return isSecure;

@ -101,6 +101,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
LOGGER.warn("No service to register for polaris client..."); LOGGER.warn("No service to register for polaris client...");
return; return;
} }
registration.customize();
String serviceId = registration.getServiceId(); String serviceId = registration.getServiceId();
// Register instance. // Register instance.

@ -60,6 +60,8 @@ public class PolarisRegistrationCustomizerTest {
@Test @Test
public void testCustomize() { public void testCustomize() {
this.contextRunner.run(context -> { this.contextRunner.run(context -> {
PolarisRegistration polarisRegistration = context.getBean(PolarisRegistration.class);
polarisRegistration.customize();
PolarisRegistrationCustomizer customizer = context.getBean(PolarisRegistrationCustomizer.class); PolarisRegistrationCustomizer customizer = context.getBean(PolarisRegistrationCustomizer.class);
verify(customizer, times(1)).customize(any(PolarisRegistration.class)); verify(customizer, times(1)).customize(any(PolarisRegistration.class));
}); });

@ -113,4 +113,23 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Bundle-Name>SCT_SDK_VERSION</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

@ -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<URL> 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";
}
}
Loading…
Cancel
Save