shutdown server when connect to config server failed (#554)

pull/565/head
lepdou 2 years ago committed by GitHub
parent bf05307e14
commit fc3986cb97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,5 +29,6 @@
- [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/539) - [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/539)
- [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/545) - [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/545)
- [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/550) - [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/550)
- [Optimize: shutdown server when connect to config server failed](https://github.com/Tencent/spring-cloud-tencent/pull/554)
- [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/556) - [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/556)
- [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/559) - [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/559)

@ -28,6 +28,8 @@ import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -38,6 +40,7 @@ import org.springframework.util.CollectionUtils;
* @author lepdou 2022-03-10 * @author lepdou 2022-03-10
*/ */
public class ConfigurationModifier implements PolarisConfigModifier { public class ConfigurationModifier implements PolarisConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class);
private final PolarisConfigProperties polarisConfigProperties; private final PolarisConfigProperties polarisConfigProperties;
@ -70,6 +73,8 @@ public class ConfigurationModifier implements PolarisConfigModifier {
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address"); + " with spring.cloud.polaris.address or spring.cloud.polaris.config.address");
} }
checkAddressAccessible(configAddresses);
configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);
} }
@ -98,4 +103,26 @@ public class ConfigurationModifier implements PolarisConfigModifier {
return configAddresses; return configAddresses;
} }
private void checkAddressAccessible(List<String> 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);
}
}
});
}
} }

@ -52,6 +52,8 @@ public class PolarisConfigProperties {
*/ */
private boolean autoRefresh = true; private boolean autoRefresh = true;
private boolean shutdownIfConnectToConfigServerFailed = true;
/** /**
* When the local configuration is consistent with the remote configuration, whether to * When the local configuration is consistent with the remote configuration, whether to
* preferentially load the remote configuration. * preferentially load the remote configuration.
@ -100,6 +102,14 @@ public class PolarisConfigProperties {
this.autoRefresh = autoRefresh; this.autoRefresh = autoRefresh;
} }
public boolean isShutdownIfConnectToConfigServerFailed() {
return shutdownIfConnectToConfigServerFailed;
}
public void setShutdownIfConnectToConfigServerFailed(boolean shutdownIfConnectToConfigServerFailed) {
this.shutdownIfConnectToConfigServerFailed = shutdownIfConnectToConfigServerFailed;
}
public RefreshType getRefreshType() { public RefreshType getRefreshType() {
return refreshType; return refreshType;
} }

@ -59,6 +59,12 @@
"type": "com.tencent.cloud.polaris.config.enums.RefreshType", "type": "com.tencent.cloud.polaris.config.enums.RefreshType",
"defaultValue": "refresh_context", "defaultValue": "refresh_context",
"description": "Attribute refresh type when config updated. refresh_context or reflect." "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."
} }
] ]
} }

@ -18,6 +18,9 @@
package com.tencent.cloud.polaris.config.condition; 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.PolarisConfigAutoConfiguration;
import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; 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.annotation.SpringValueProcessor;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -45,6 +50,26 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class ConditionalOnReflectRefreshTypeTest { 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 @Test
public void testReflectEnabled() { public void testReflectEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner() ApplicationContextRunner contextRunner = new ApplicationContextRunner()

@ -18,7 +18,9 @@
package com.tencent.cloud.polaris.config.spring.annotation; package com.tencent.cloud.polaris.config.spring.annotation;
import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; 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.SpringValue;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.api.utils.CollectionUtils;
import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -48,6 +52,26 @@ import org.springframework.stereotype.Component;
*/ */
public class SpringValueProcessorTest { 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 @Test
public void springValueFiledProcessorTest() { public void springValueFiledProcessorTest() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner() ApplicationContextRunner contextRunner = new ApplicationContextRunner()

@ -7,6 +7,7 @@ spring:
namespace: default namespace: default
config: config:
connect-remote-server: false connect-remote-server: false
shutdown-if-connect-to-config-server-failed: false
# auto-refresh: true # auto-refresh: true
config: config:
import: import:

@ -18,11 +18,18 @@
package com.tencent.cloud.common.util; 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.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -32,6 +39,8 @@ import org.springframework.util.StringUtils;
*/ */
public final class AddressUtils { public final class AddressUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AddressUtils.class);
private static final String ADDRESS_SEPARATOR = ","; private static final String ADDRESS_SEPARATOR = ",";
private AddressUtils() { private AddressUtils() {
@ -49,4 +58,23 @@ public final class AddressUtils {
} }
return addressList; 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;
}
} }

Loading…
Cancel
Save