fix:fix consul connect bug. (#217)

pull/284/head
Haotian Zhang 3 years ago committed by GitHub
parent 11a5aea66f
commit 58a82731fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,4 @@
# Change Log
---
- [fix:fix consul connect bug.](https://github.com/Tencent/spring-cloud-tencent/pull/217)

@ -86,7 +86,7 @@
<properties>
<!-- Project revision -->
<revision>1.6.0-Hoxton.SR9-SNAPSHOT</revision>
<revision>1.5.1-Hoxton.SR9-SNAPSHOT</revision>
<!-- Spring Cloud -->
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>

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

@ -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<ServerConnectorConfigImpl> serverConnectorConfigs = configuration.getGlobal().getServerConnectors();
Map<String, String> 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<ServerConnectorConfigImpl> serverConnectorConfigs = configuration.getGlobal().getServerConnectors();
Map<String, String> 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;
}
}
}

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

@ -70,8 +70,8 @@
</developers>
<properties>
<revision>1.6.0-Hoxton.SR9-SNAPSHOT</revision>
<polaris.version>1.7.0-SNAPSHOT</polaris.version>
<revision>1.5.1-Hoxton.SR9-SNAPSHOT</revision>
<polaris.version>1.6.0</polaris.version>
<mocktio.version>4.5.1</mocktio.version>
<byte-buddy.version>1.12.10</byte-buddy.version>

Loading…
Cancel
Save