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

Co-authored-by: Haotian Zhang <928016560@qq.com>
pull/572/head
lepdou 2 years ago committed by GitHub
parent d70b945d49
commit 9b643fe645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,4 +23,5 @@
- [optimizeadd switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/547)
- [Optimize: refresh @Value by reflect and only refresh affected ConfigurationProperties beans](https://github.com/Tencent/spring-cloud-tencent/pull/548)
- [feature:remove ConditionalOnConfigReflectEnabled annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/551)
- [Optimize:shutdown server when connect to config server failed.](https://github.com/Tencent/spring-cloud-tencent/pull/552)
- [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/560)

@ -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;
@ -37,6 +39,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;
@ -69,6 +72,8 @@ public class ConfigurationModifier implements PolarisConfigModifier {
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address");
}
checkAddressAccessible(configAddresses);
configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);
}
@ -97,4 +102,26 @@ public class ConfigurationModifier implements PolarisConfigModifier {
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);
}
}
});
}
}

@ -53,6 +53,8 @@ public class PolarisConfigProperties {
*/
private boolean autoRefresh = true;
private boolean shutdownIfConnectToConfigServerFailed = true;
/**
* Attribute refresh type.
*/
@ -95,6 +97,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;
}

@ -47,6 +47,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."
}
]
}

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

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

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

@ -28,4 +28,4 @@
</dependency>
</dependencies>
</project>
</project>

Loading…
Cancel
Save