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

pull/921/head
Haotian Zhang 2 years ago committed by GitHub
parent eae5f68c4c
commit 6daa381190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
# Change Log
---
- [fix: fix log feign response stream close bug.](https://github.com/Tencent/spring-cloud-tencent/pull/897)
- [fix:fix log feign response stream close bug.](https://github.com/Tencent/spring-cloud-tencent/pull/897)
- [fix:remove the secondary report.](https://github.com/Tencent/spring-cloud-tencent/pull/899)
- [fix:optimize instance circuit beaker.](https://github.com/Tencent/spring-cloud-tencent/pull/909)
- [fix:optimize instance circuit beaker.](https://github.com/Tencent/spring-cloud-tencent/pull/909)
- [fix:optimize multi service registration and discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/912)

@ -91,7 +91,7 @@
<revision>1.11.0-2021.0.6-SNAPSHOT</revision>
<!-- Spring Framework -->
<spring.framework.version>5.3.25</spring.framework.version>
<spring.framework.version>5.3.26</spring.framework.version>
<!-- Spring Boot -->
<spring.boot.version>2.6.14</spring.boot.version>

@ -28,6 +28,18 @@
<scope>test</scope>
</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>
<groupId>com.tencent.polaris</groupId>
<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.plugins.connector.common.constant.ConsulConstant;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
@ -38,6 +40,9 @@ import org.springframework.util.CollectionUtils;
* @author lingxiao.wlx
*/
public class ConsulConfigModifier implements PolarisConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsulConfigModifier.class);
private static final String ID = "consul";
private final ConsulContextProperties consulContextProperties;
@ -49,6 +54,23 @@ public class ConsulConfigModifier implements PolarisConfigModifier {
@Override
public void modify(ConfigurationImpl configuration) {
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())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>());
}

@ -31,6 +31,8 @@ import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl;
import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
@ -41,7 +43,6 @@ import org.springframework.util.CollectionUtils;
*/
public class NacosConfigModifier implements PolarisConfigModifier {
private static final String ID = "nacos";
/**
* nacos username.
*/
@ -54,7 +55,8 @@ public class NacosConfigModifier implements PolarisConfigModifier {
* nacos 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;
public NacosConfigModifier(NacosContextProperties nacosContextProperties) {
@ -66,6 +68,23 @@ public class NacosConfigModifier implements PolarisConfigModifier {
if (Objects.isNull(nacosContextProperties) || !nacosContextProperties.isEnabled()) {
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())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>());
}

@ -111,14 +111,4 @@ public final class JacksonUtils {
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;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.Instance;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -38,7 +39,7 @@ public class PolarisServiceInstanceTest {
@Test
@DisplayName("test getters and setters.")
void test() {
public void test1() {
Instance secureInstance = mock(Instance.class);
doReturn("test-ID").when(secureInstance).getId();
doReturn(SERVICE_PROVIDER).when(secureInstance).getService();
@ -53,6 +54,7 @@ public class PolarisServiceInstanceTest {
assertThat(securePolarisServiceInstance.getPort()).isEqualTo(8080);
assertThat(securePolarisServiceInstance.isSecure()).isTrue();
assertThat(securePolarisServiceInstance.getScheme()).isEqualTo("https");
assertThat(securePolarisServiceInstance.getUri().toString()).isEqualTo("https://1.1.1.1:8080");
Instance insecureInstance = mock(Instance.class);
doReturn("http").when(insecureInstance).getProtocol();
@ -60,4 +62,37 @@ public class PolarisServiceInstanceTest {
assertThat(insecurePolarisServiceInstance.isSecure()).isFalse();
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.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.RootBeanDefinition;
import org.springframework.core.ResolvableType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Test for {@link BeanFactoryUtils}.
@ -41,6 +47,11 @@ public class BeanFactoryUtilsTest {
assertThat(childBeanFactory.getBeansOfType(Foo.class)).isEmpty();
assertThat(BeanFactoryUtils.getBeans(childBeanFactory, Foo.class).size()).isEqualTo(1);
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 {
@ -50,4 +61,82 @@ public class BeanFactoryUtilsTest {
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.mockito.junit.jupiter.MockitoExtension;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ -43,6 +45,7 @@ public class JacksonUtilsTest {
sourceMap.put("k2", "v2");
sourceMap.put("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

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

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

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

Loading…
Cancel
Save