fix:fix web server wrong open bug.

pull/1075/head
Haotian Zhang 11 months ago
parent e64676acfb
commit 483c391791

@ -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<PolarisRegistrationCustomizer> customizers;
private boolean registerEnabled = false;
private Map<String, String> 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<PolarisRegistrationCustomizer> 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<PolarisRegistrationCustomizer> 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<PolarisRegistrationCustomizer> 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;

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

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

@ -112,4 +112,24 @@
<scope>test</scope>
</dependency>
</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>

@ -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