feat:support customize registered ip address.

pull/47/head
SkyeBeFreeman 4 years ago
parent bbe6a104e2
commit 7c0a390b19

@ -22,6 +22,7 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
/** /**
@ -72,6 +73,11 @@ public class PolarisProperties {
@Value("${server.port:}") @Value("${server.port:}")
private int port; private int port;
/**
* Ip address to be registered.
*/
private String ipAddress;
/** /**
* *
*/ */
@ -107,6 +113,12 @@ public class PolarisProperties {
@Autowired @Autowired
private Environment environment; private Environment environment;
public PolarisProperties(InetUtils inetUtils) {
if (inetUtils != null) {
this.ipAddress = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
}
}
/** /**
* init properties * init properties
* *
@ -218,6 +230,14 @@ public class PolarisProperties {
this.port = port; this.port = port;
} }
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getHealthCheckUrl() { public String getHealthCheckUrl() {
return healthCheckUrl; return healthCheckUrl;
} }
@ -237,6 +257,7 @@ public class PolarisProperties {
", version='" + version + '\'' + ", version='" + version + '\'' +
", protocol='" + protocol + '\'' + ", protocol='" + protocol + '\'' +
", port=" + port + ", port=" + port +
", ipAddress='" + ipAddress + '\'' +
", loadbalancerEnabled=" + loadbalancerEnabled + ", loadbalancerEnabled=" + loadbalancerEnabled +
", policy='" + policy + '\'' + ", policy='" + policy + '\'' +
", registerEnabled=" + registerEnabled + ", registerEnabled=" + registerEnabled +

@ -25,6 +25,7 @@ import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory; import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -41,8 +42,8 @@ public class PolarisDiscoveryAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public PolarisProperties polarisDiscoveryProperties() { public PolarisProperties polarisDiscoveryProperties(InetUtils inetUtils) {
return new PolarisProperties(); return new PolarisProperties(inetUtils);
} }
@Bean(name = "polarisProvider") @Bean(name = "polarisProvider")

@ -48,6 +48,9 @@ public class PolarisRegistration implements Registration, ServiceInstance {
@Override @Override
public String getHost() { public String getHost() {
if (StringUtils.isNotBlank(polarisProperties.getIpAddress())) {
return polarisProperties.getIpAddress();
}
return polarisContext.getConfig().getGlobal().getAPI().getBindIP(); return polarisContext.getConfig().getGlobal().getAPI().getBindIP();
} }

@ -42,6 +42,12 @@
"defaultValue": "${spring.cloud.polaris.version}", "defaultValue": "${spring.cloud.polaris.version}",
"description": "polaris discovery service's username to authenticate." "description": "polaris discovery service's username to authenticate."
}, },
{
"name": "spring.cloud.polaris.discovery.ip-address",
"type": "java.lang.String",
"defaultValue": "",
"description": "ip address to be registered to polaris discovery server."
},
{ {
"name": "spring.cloud.polaris.protocol", "name": "spring.cloud.polaris.protocol",
"type": "java.lang.String", "type": "java.lang.String",
@ -52,7 +58,7 @@
"name": "spring.cloud.polaris.weight", "name": "spring.cloud.polaris.weight",
"type": "java.lang.String", "type": "java.lang.String",
"defaultValue": 100, "defaultValue": 100,
"description": "the weight of polaris instance , use to loadbalance." "description": "the weight of polaris instance , use to load-balance."
} }
] ]
} }

@ -24,12 +24,14 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
public class PolarisPropertiesTest { public class PolarisPropertiesTest {
@Test @Test
public void testInitAndGetSet() { public void testInitAndGetSet() {
PolarisProperties temp = new PolarisProperties(); PolarisProperties temp = new PolarisProperties(new InetUtils(new InetUtilsProperties()));
try { try {
temp.setNamespace(NAMESPACE_TEST); temp.setNamespace(NAMESPACE_TEST);
temp.getNamespace(); temp.getNamespace();

@ -11,7 +11,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-polaris-context</artifactId> <artifactId>spring-cloud-tencent-polaris-context</artifactId>
<name>Spring Cloud Tencent Polaris SDKContext</name> <name>Spring Cloud Tencent Polaris SDK Context</name>
<dependencies> <dependencies>
<!-- Spring Cloud Tencent dependencies start --> <!-- Spring Cloud Tencent dependencies start -->

Loading…
Cancel
Save