fix:optimize multi service registration and discovery. (#914)

pull/919/head
Haotian Zhang 2 years ago committed by GitHub
parent 7b2f9483af
commit 74f8955290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,3 +4,4 @@
- [fix: fix log feign response stream close bug.](https://github.com/Tencent/spring-cloud-tencent/pull/896) - [fix: fix log feign response stream close bug.](https://github.com/Tencent/spring-cloud-tencent/pull/896)
- [fix:remove the secondary report.](https://github.com/Tencent/spring-cloud-tencent/pull/900) - [fix:remove the secondary report.](https://github.com/Tencent/spring-cloud-tencent/pull/900)
- [fix:optimize instance circuit beaker.](https://github.com/Tencent/spring-cloud-tencent/pull/908) - [fix:optimize instance circuit beaker.](https://github.com/Tencent/spring-cloud-tencent/pull/908)
- [fix:optimize multi service registration and discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/914)

@ -88,16 +88,16 @@
<properties> <properties>
<!-- Project revision --> <!-- Project revision -->
<revision>1.11.0-2020.0.5-SNAPSHOT</revision> <revision>1.11.0-2020.0.6-SNAPSHOT</revision>
<!-- Spring Framework --> <!-- Spring Framework -->
<spring.framework.version>5.3.21</spring.framework.version> <spring.framework.version>5.3.26</spring.framework.version>
<!-- Spring Boot --> <!-- Spring Boot -->
<spring.boot.version>2.4.13</spring.boot.version> <spring.boot.version>2.4.13</spring.boot.version>
<!-- Spring Cloud --> <!-- Spring Cloud -->
<spring.cloud.version>2020.0.5</spring.cloud.version> <spring.cloud.version>2020.0.6</spring.cloud.version>
<!-- Maven Plugin Versions --> <!-- Maven Plugin Versions -->
<jacoco.version>0.8.8</jacoco.version> <jacoco.version>0.8.8</jacoco.version>

@ -28,6 +28,18 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-consul</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-nacos</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-mock-discovery</artifactId> <artifactId>polaris-test-mock-discovery</artifactId>

@ -31,6 +31,8 @@ import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl; import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant; import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant;
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,9 @@ import org.springframework.util.CollectionUtils;
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
public class ConsulConfigModifier implements PolarisConfigModifier { public class ConsulConfigModifier implements PolarisConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsulConfigModifier.class);
private static final String ID = "consul"; private static final String ID = "consul";
private final ConsulContextProperties consulContextProperties; private final ConsulContextProperties consulContextProperties;
@ -49,6 +54,23 @@ public class ConsulConfigModifier implements PolarisConfigModifier {
@Override @Override
public void modify(ConfigurationImpl configuration) { public void modify(ConfigurationImpl configuration) {
if (consulContextProperties != null && consulContextProperties.isEnabled()) { if (consulContextProperties != null && consulContextProperties.isEnabled()) {
// Check if Consul client Available
boolean consulAvailable = false;
try {
consulAvailable = null != Class.forName("com.ecwid.consul.v1.ConsulClient");
}
catch (Throwable ignored) {
}
if (!consulAvailable) {
LOGGER.error("Please import \"connector-consul\" dependency when enabling consul service registration and discovery.\n"
+ "Add dependency configuration below to pom.xml:\n"
+ "<dependency>\n"
+ "\t<groupId>com.tencent.polaris</groupId>\n"
+ "\t<artifactId>connector-consul</artifactId>\n"
+ "</dependency>");
throw new RuntimeException("Dependency \"connector-consul\" not found.");
}
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) { if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>()); configuration.getGlobal().setServerConnectors(new ArrayList<>());
} }

@ -30,6 +30,8 @@ import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl;
import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl; import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl; import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
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;
@ -40,7 +42,6 @@ import org.springframework.util.CollectionUtils;
*/ */
public class NacosConfigModifier implements PolarisConfigModifier { public class NacosConfigModifier implements PolarisConfigModifier {
private static final String ID = "nacos";
/** /**
* nacos username. * nacos username.
*/ */
@ -53,7 +54,8 @@ public class NacosConfigModifier implements PolarisConfigModifier {
* nacos contextPath. * nacos contextPath.
*/ */
public static final String CONTEXT_PATH = "contextPath"; public static final String CONTEXT_PATH = "contextPath";
private static final Logger LOGGER = LoggerFactory.getLogger(NacosConfigModifier.class);
private static final String ID = "nacos";
private final NacosContextProperties nacosContextProperties; private final NacosContextProperties nacosContextProperties;
public NacosConfigModifier(NacosContextProperties nacosContextProperties) { public NacosConfigModifier(NacosContextProperties nacosContextProperties) {
@ -65,6 +67,23 @@ public class NacosConfigModifier implements PolarisConfigModifier {
if (Objects.isNull(nacosContextProperties) || !nacosContextProperties.isEnabled()) { if (Objects.isNull(nacosContextProperties) || !nacosContextProperties.isEnabled()) {
return; return;
} }
// Check if Nacos Available
boolean nacosAvailable = false;
try {
nacosAvailable = null != Class.forName("com.alibaba.nacos.api.naming.NamingService");
}
catch (Throwable ignored) {
}
if (!nacosAvailable) {
LOGGER.error("Please import \"connector-nacos\" dependency when enabling nacos service registration and discovery.\n"
+ "Add dependency configuration below to pom.xml:\n"
+ "<dependency>\n"
+ "\t<groupId>com.tencent.polaris</groupId>\n"
+ "\t<artifactId>connector-nacos</artifactId>\n"
+ "</dependency>");
throw new RuntimeException("Dependency \"connector-nacos\" not found.");
}
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) { if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>()); configuration.getGlobal().setServerConnectors(new ArrayList<>());
} }

@ -111,14 +111,4 @@ public final class JacksonUtils {
throw new RuntimeException("Json to map failed.", e); throw new RuntimeException("Json to map failed.", e);
} }
} }
public static <T> T json2JavaBean(String content, Class<T> valueType) {
try {
return OM.readValue(content, valueType);
}
catch (Exception e) {
LOG.error("json {} to class {} failed. ", content, valueType, e);
throw new RuntimeException("json to class failed.", e);
}
}
} }

@ -17,6 +17,7 @@
package com.tencent.cloud.common.pojo; package com.tencent.cloud.common.pojo;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.Instance;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -38,7 +39,7 @@ public class PolarisServiceInstanceTest {
@Test @Test
@DisplayName("test getters and setters.") @DisplayName("test getters and setters.")
void test() { public void test1() {
Instance secureInstance = mock(Instance.class); Instance secureInstance = mock(Instance.class);
doReturn("test-ID").when(secureInstance).getId(); doReturn("test-ID").when(secureInstance).getId();
doReturn(SERVICE_PROVIDER).when(secureInstance).getService(); doReturn(SERVICE_PROVIDER).when(secureInstance).getService();
@ -53,6 +54,7 @@ public class PolarisServiceInstanceTest {
assertThat(securePolarisServiceInstance.getPort()).isEqualTo(8080); assertThat(securePolarisServiceInstance.getPort()).isEqualTo(8080);
assertThat(securePolarisServiceInstance.isSecure()).isTrue(); assertThat(securePolarisServiceInstance.isSecure()).isTrue();
assertThat(securePolarisServiceInstance.getScheme()).isEqualTo("https"); assertThat(securePolarisServiceInstance.getScheme()).isEqualTo("https");
assertThat(securePolarisServiceInstance.getUri().toString()).isEqualTo("https://1.1.1.1:8080");
Instance insecureInstance = mock(Instance.class); Instance insecureInstance = mock(Instance.class);
doReturn("http").when(insecureInstance).getProtocol(); doReturn("http").when(insecureInstance).getProtocol();
@ -60,4 +62,37 @@ public class PolarisServiceInstanceTest {
assertThat(insecurePolarisServiceInstance.isSecure()).isFalse(); assertThat(insecurePolarisServiceInstance.isSecure()).isFalse();
assertThat(insecurePolarisServiceInstance.getScheme()).isEqualTo("http"); assertThat(insecurePolarisServiceInstance.getScheme()).isEqualTo("http");
} }
@Test
@DisplayName("test equals().")
public void test2() {
DefaultInstance instance1 = new DefaultInstance();
instance1.setId("test-1");
instance1.setProtocol("http");
PolarisServiceInstance polarisServiceInstance1 = new PolarisServiceInstance(instance1);
DefaultInstance instance2 = new DefaultInstance();
instance2.setId("test-1");
instance2.setProtocol("http");
PolarisServiceInstance polarisServiceInstance2 = new PolarisServiceInstance(instance2);
assertThat(polarisServiceInstance1.equals(polarisServiceInstance2)).isTrue();
}
@Test
@DisplayName("test hashCode().")
public void test3() {
DefaultInstance instance1 = new DefaultInstance();
instance1.setId("test-1");
instance1.setProtocol("http");
PolarisServiceInstance polarisServiceInstance1 = new PolarisServiceInstance(instance1);
DefaultInstance instance2 = new DefaultInstance();
instance2.setId("test-1");
instance2.setProtocol("http");
PolarisServiceInstance polarisServiceInstance2 = new PolarisServiceInstance(instance2);
assertThat(polarisServiceInstance1.hashCode()).isEqualTo(polarisServiceInstance2.hashCode());
}
} }

@ -19,10 +19,16 @@ package com.tencent.cloud.common.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.ResolvableType;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Test for {@link BeanFactoryUtils}. * Test for {@link BeanFactoryUtils}.
@ -41,6 +47,11 @@ public class BeanFactoryUtilsTest {
assertThat(childBeanFactory.getBeansOfType(Foo.class)).isEmpty(); assertThat(childBeanFactory.getBeansOfType(Foo.class)).isEmpty();
assertThat(BeanFactoryUtils.getBeans(childBeanFactory, Foo.class).size()).isEqualTo(1); assertThat(BeanFactoryUtils.getBeans(childBeanFactory, Foo.class).size()).isEqualTo(1);
assertThat(BeanFactoryUtils.getBeans(childBeanFactory, Bar.class)).isEmpty(); assertThat(BeanFactoryUtils.getBeans(childBeanFactory, Bar.class)).isEmpty();
MockBeanFactory mockBeanFactory = new MockBeanFactory();
assertThatThrownBy(() -> BeanFactoryUtils.getBeans(mockBeanFactory, Bar.class))
.isExactlyInstanceOf(RuntimeException.class)
.hasMessageContaining("bean factory not support get list bean.");
} }
static class Foo { static class Foo {
@ -50,4 +61,82 @@ public class BeanFactoryUtilsTest {
static class Bar { static class Bar {
} }
static class MockBeanFactory implements BeanFactory {
@Override
public Object getBean(String s) throws BeansException {
return null;
}
@Override
public <T> T getBean(String s, Class<T> aClass) throws BeansException {
return null;
}
@Override
public Object getBean(String s, Object... objects) throws BeansException {
return null;
}
@Override
public <T> T getBean(Class<T> aClass) throws BeansException {
return null;
}
@Override
public <T> T getBean(Class<T> aClass, Object... objects) throws BeansException {
return null;
}
@Override
public <T> ObjectProvider<T> getBeanProvider(Class<T> aClass) {
return null;
}
@Override
public <T> ObjectProvider<T> getBeanProvider(ResolvableType resolvableType) {
return null;
}
@Override
public boolean containsBean(String s) {
return false;
}
@Override
public boolean isSingleton(String s) throws NoSuchBeanDefinitionException {
return false;
}
@Override
public boolean isPrototype(String s) throws NoSuchBeanDefinitionException {
return false;
}
@Override
public boolean isTypeMatch(String s, ResolvableType resolvableType) throws NoSuchBeanDefinitionException {
return false;
}
@Override
public boolean isTypeMatch(String s, Class<?> aClass) throws NoSuchBeanDefinitionException {
return false;
}
@Override
public Class<?> getType(String s) throws NoSuchBeanDefinitionException {
return null;
}
@Override
public Class<?> getType(String s, boolean b) throws NoSuchBeanDefinitionException {
return null;
}
@Override
public String[] getAliases(String s) {
return new String[0];
}
}
} }

@ -25,6 +25,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ -43,6 +45,7 @@ public class JacksonUtilsTest {
sourceMap.put("k2", "v2"); sourceMap.put("k2", "v2");
sourceMap.put("k3", "v3"); sourceMap.put("k3", "v3");
assertThat(JacksonUtils.serialize2Json(sourceMap)).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}"); assertThat(JacksonUtils.serialize2Json(sourceMap)).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}");
assertThat(StringUtils.trimAllWhitespace(JacksonUtils.serialize2Json(sourceMap, true))).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}");
} }
@Test @Test

@ -70,7 +70,7 @@
</developers> </developers>
<properties> <properties>
<revision>1.11.0-2020.0.5-SNAPSHOT</revision> <revision>1.11.0-2020.0.6-SNAPSHOT</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.11.2</polaris.version> <polaris.version>1.11.2</polaris.version>

@ -30,13 +30,13 @@
</dependency> </dependency>
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>com.tencent.polaris</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>--> <!-- <artifactId>connector-consul</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>com.tencent.polaris</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>--> <!-- <artifactId>connector-nacos</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
<dependency> <dependency>

@ -29,13 +29,13 @@
</dependency> </dependency>
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>com.tencent.polaris</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>--> <!-- <artifactId>connector-consul</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>--> <!-- <groupId>com.tencent.polaris</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>--> <!-- <artifactId>connector-nacos</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
</dependencies> </dependencies>

@ -75,16 +75,6 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-consul</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-nacos</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>connector-composite</artifactId> <artifactId>connector-composite</artifactId>

Loading…
Cancel
Save