From 58a82731fe15e7f853341805d2f56afbe9826d58 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 1 Jun 2022 17:41:22 +0800 Subject: [PATCH] fix:fix consul connect bug. (#217) --- CHANGELOG.md | 2 +- pom.xml | 2 +- .../consul/ConsulContextProperties.java | 16 ++++ .../consul/ConsulContextPropertiesTest.java | 80 +++++++++---------- .../src/test/resources/application-test.yml | 24 ++++++ spring-cloud-tencent-dependencies/pom.xml | 4 +- 6 files changed, 84 insertions(+), 44 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 15c44dd50..a582e3497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ # Change Log --- - +- [fix:fix consul connect bug.](https://github.com/Tencent/spring-cloud-tencent/pull/217) diff --git a/pom.xml b/pom.xml index a6e0ce538..a23de3dc1 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ - 1.6.0-Hoxton.SR9-SNAPSHOT + 1.5.1-Hoxton.SR9-SNAPSHOT Hoxton.SR9 diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java index b24c72897..2efbd93d6 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java @@ -73,6 +73,22 @@ public class ConsulContextProperties { @Value("${spring.cloud.consul.discovery.prefer-ip-address:#{'false'}}") private boolean preferIpAddress; + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + public boolean isEnabled() { return enabled; } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/consul/ConsulContextPropertiesTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/consul/ConsulContextPropertiesTest.java index ac4afe13a..9366eed7d 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/consul/ConsulContextPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/consul/ConsulContextPropertiesTest.java @@ -20,16 +20,16 @@ package com.tencent.cloud.polaris.extend.consul; import java.util.List; import java.util.Map; -import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl; import org.junit.Test; +import org.junit.runner.RunWith; -import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; import static com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY; import static com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey.IP_ADDRESS_KEY; @@ -44,47 +44,47 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Haotian Zhang */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConsulContextPropertiesTest.TestApplication.class) +@ActiveProfiles("test") public class ConsulContextPropertiesTest { + @Autowired + private ConsulContextProperties consulContextProperties; + + @Autowired + private SDKContext sdkContext; + + @Test + public void testDefaultInitialization() { + assertThat(consulContextProperties).isNotNull(); + assertThat(consulContextProperties.isEnabled()).isTrue(); + assertThat(consulContextProperties.getHost()).isEqualTo("127.0.0.1"); + assertThat(consulContextProperties.getPort()).isEqualTo(8500); + assertThat(consulContextProperties.isRegister()).isTrue(); + assertThat(consulContextProperties.isDiscoveryEnabled()).isTrue(); + } + @Test public void testModify() { - ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner().withConfiguration( - AutoConfigurations.of(PolarisContextAutoConfiguration.class, - ConsulContextPropertiesTest.TestConfiguration.class, - DiscoveryPropertiesAutoConfiguration.class)) - .withPropertyValues("spring.cloud.consul.discovery.register=true") - .withPropertyValues("spring.cloud.consul.discovery.enabled=true") - .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) - .withPropertyValues("spring.cloud.consul.discovery.instance-id=ins-test") - .withPropertyValues("spring.cloud.consul.discovery.prefer-ip-address=true") - .withPropertyValues("spring.cloud.consul.discovery.ip-address=" + HOST); - applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(SDKContext.class); - SDKContext sdkContext = context.getBean(SDKContext.class); - com.tencent.polaris.api.config.Configuration configuration = sdkContext.getConfig(); - List serverConnectorConfigs = configuration.getGlobal().getServerConnectors(); - Map metadata = null; - for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) { - if (serverConnectorConfig.getId().equals("consul")) { - metadata = serverConnectorConfig.getMetadata(); - } + assertThat(sdkContext).isNotNull(); + com.tencent.polaris.api.config.Configuration configuration = sdkContext.getConfig(); + List serverConnectorConfigs = configuration.getGlobal().getServerConnectors(); + Map metadata = null; + for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) { + if (serverConnectorConfig.getId().equals("consul")) { + metadata = serverConnectorConfig.getMetadata(); } - assertThat(metadata).isNotNull(); - assertThat(metadata.get(SERVICE_NAME_KEY)).isEqualTo(SERVICE_PROVIDER); - assertThat(metadata.get(INSTANCE_ID_KEY)).isEqualTo("ins-test"); - assertThat(metadata.get(PREFER_IP_ADDRESS_KEY)).isEqualTo("true"); - assertThat(metadata.get(IP_ADDRESS_KEY)).isEqualTo(HOST); - }); + } + assertThat(metadata).isNotNull(); + assertThat(metadata.get(SERVICE_NAME_KEY)).isEqualTo(SERVICE_PROVIDER); + assertThat(metadata.get(INSTANCE_ID_KEY)).isEqualTo("ins-test"); + assertThat(metadata.get(PREFER_IP_ADDRESS_KEY)).isEqualTo("true"); + assertThat(metadata.get(IP_ADDRESS_KEY)).isEqualTo(HOST); } - @Configuration - static class TestConfiguration { + @SpringBootApplication + protected static class TestApplication { - @Bean - public ConsulContextProperties consulContextProperties() { - ConsulContextProperties consulContextProperties = new ConsulContextProperties(); - consulContextProperties.setEnabled(true); - return consulContextProperties; - } } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml b/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml new file mode 100644 index 000000000..8fbdbcc48 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml @@ -0,0 +1,24 @@ +server: + port: 48084 +spring: + application: + name: java_provider_test + cloud: + polaris: + address: grpc://127.0.0.1:8091 + namespace: Test + enabled: true + discovery: + enabled: true + register: true + consul: + port: 8500 + host: 127.0.0.1 + enabled: true + discovery: + enabled: true + register: true + instance-id: ins-test + service-name: ${spring.application.name} + ip-address: 127.0.0.1 + prefer-ip-address: true diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 9ddc48031..24c8ef382 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,8 +70,8 @@ - 1.6.0-Hoxton.SR9-SNAPSHOT - 1.7.0-SNAPSHOT + 1.5.1-Hoxton.SR9-SNAPSHOT + 1.6.0 4.5.1 1.12.10