From b7af6e610c61eabf4876ddeb7a4477644b07770b Mon Sep 17 00:00:00 2001 From: lepdou Date: Tue, 6 Sep 2022 11:25:16 +0800 Subject: [PATCH] shutdown server when connect to config server failed (#553) --- CHANGELOG.md | 1 + .../polaris/config/ConfigurationModifier.java | 27 ++++++++++++++++++ .../config/PolarisConfigProperties.java | 10 +++++++ ...itional-spring-configuration-metadata.json | 6 ++++ .../ConditionalOnReflectRefreshTypeTest.java | 25 +++++++++++++++++ .../annotation/SpringValueProcessorTest.java | 24 ++++++++++++++++ .../cloud/common/util/AddressUtils.java | 28 +++++++++++++++++++ 7 files changed, 121 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7db6c0252..472c8184b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,5 +28,6 @@ - [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/538) - [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/544) - [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/549) +- [Optimize: shutdown server when connect to config server failed](https://github.com/Tencent/spring-cloud-tencent/pull/553) - [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/555) - [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/558) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java index 9953b3c67..d505d3788 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -28,6 +28,8 @@ import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.util.CollectionUtils; @@ -38,6 +40,7 @@ import org.springframework.util.CollectionUtils; * @author lepdou 2022-03-10 */ public class ConfigurationModifier implements PolarisConfigModifier { + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class); private final PolarisConfigProperties polarisConfigProperties; @@ -70,6 +73,8 @@ public class ConfigurationModifier implements PolarisConfigModifier { + " with spring.cloud.polaris.address or spring.cloud.polaris.config.address"); } + checkAddressAccessible(configAddresses); + configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); } @@ -98,4 +103,26 @@ public class ConfigurationModifier implements PolarisConfigModifier { return configAddresses; } + + private void checkAddressAccessible(List configAddresses) { + // check address can connect + configAddresses.forEach(address -> { + String[] ipPort = address.split(":"); + + if (ipPort.length != 2) { + throw new IllegalArgumentException("Config server address (" + address + ") is wrong, please check address like grpc://183.47.111.8:8091."); + } + + if (!AddressUtils.accessible(ipPort[0], Integer.parseInt(ipPort[1]), 3000)) { + String errMsg = "Config server address (" + address + ") can not be connected. Please check your config in bootstrap.yml" + + " with spring.cloud.polaris.address or spring.cloud.polaris.config.address."; + if (polarisConfigProperties.isShutdownIfConnectToConfigServerFailed()) { + throw new IllegalArgumentException(errMsg); + } + else { + LOGGER.error(errMsg); + } + } + }); + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index bdd1821e0..235b40cc9 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -52,6 +52,8 @@ public class PolarisConfigProperties { */ private boolean autoRefresh = true; + private boolean shutdownIfConnectToConfigServerFailed = true; + /** * Attribute refresh type. */ @@ -94,6 +96,14 @@ public class PolarisConfigProperties { this.autoRefresh = autoRefresh; } + public boolean isShutdownIfConnectToConfigServerFailed() { + return shutdownIfConnectToConfigServerFailed; + } + + public void setShutdownIfConnectToConfigServerFailed(boolean shutdownIfConnectToConfigServerFailed) { + this.shutdownIfConnectToConfigServerFailed = shutdownIfConnectToConfigServerFailed; + } + public RefreshType getRefreshType() { return refreshType; } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 8889e99f8..b394c540a 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -65,6 +65,12 @@ "type": "com.tencent.cloud.polaris.config.enums.RefreshType", "defaultValue": "refresh_context", "description": "Attribute refresh type when config updated. refresh_context or reflect." + }, + { + "name": "spring.cloud.polaris.config.shutdown-if-connect-to-config-server-failed", + "type": "java.lang.Boolean", + "defaultValue": true, + "description": "Whether shutdown server if connect to config server failed." } ] } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java index f2a2d76b4..80b8dbbcc 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java @@ -18,6 +18,9 @@ package com.tencent.cloud.polaris.config.condition; +import java.io.IOException; +import java.net.ServerSocket; + import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; @@ -28,6 +31,8 @@ import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -45,6 +50,26 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class ConditionalOnReflectRefreshTypeTest { + private static ServerSocket serverSocket; + + @BeforeClass + public static void before() { + new Thread(() -> { + try { + serverSocket = new ServerSocket(8093); + serverSocket.accept(); + } + catch (IOException e) { + //ignore + } + }).start(); + } + + @AfterClass + public static void after() throws IOException { + serverSocket.close(); + } + @Test public void testReflectEnabled() { ApplicationContextRunner contextRunner = new ApplicationContextRunner() diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java index fb57206bb..6ad500ae3 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java @@ -18,7 +18,9 @@ package com.tencent.cloud.polaris.config.spring.annotation; +import java.io.IOException; import java.lang.reflect.Method; +import java.net.ServerSocket; import java.util.Collection; import java.util.Objects; import java.util.Optional; @@ -28,7 +30,9 @@ import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.spring.property.SpringValue; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.polaris.api.utils.CollectionUtils; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; @@ -48,6 +52,26 @@ import org.springframework.stereotype.Component; */ public class SpringValueProcessorTest { + private static ServerSocket serverSocket; + + @BeforeClass + public static void before() { + new Thread(() -> { + try { + serverSocket = new ServerSocket(8093); + serverSocket.accept(); + } + catch (IOException e) { + //ignore + } + }).start(); + } + + @AfterClass + public static void after() throws IOException { + serverSocket.close(); + } + @Test public void springValueFiledProcessorTest() { ApplicationContextRunner contextRunner = new ApplicationContextRunner() diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java index 0faf3d66f..277a842db 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java @@ -18,11 +18,18 @@ package com.tencent.cloud.common.util; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.springframework.util.StringUtils; /** @@ -32,6 +39,8 @@ import org.springframework.util.StringUtils; */ public final class AddressUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(AddressUtils.class); + private static final String ADDRESS_SEPARATOR = ","; private AddressUtils() { @@ -49,4 +58,23 @@ public final class AddressUtils { } return addressList; } + + public static boolean accessible(String ip, int port, int timeout) { + Socket socket = new Socket(); + try { + socket.connect(new InetSocketAddress(InetAddress.getByName(ip), port), timeout); + } + catch (IOException e) { + return false; + } + finally { + try { + socket.close(); + } + catch (IOException e) { + LOGGER.error("Close socket connection failed.", e); + } + } + return true; + } }