feat:support customize registered ip address.

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

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

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

@ -42,6 +42,12 @@
"defaultValue": "${spring.cloud.polaris.version}",
"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",
"type": "java.lang.String",
@ -52,7 +58,7 @@
"name": "spring.cloud.polaris.weight",
"type": "java.lang.String",
"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 org.junit.Test;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
public class PolarisPropertiesTest {
@Test
public void testInitAndGetSet() {
PolarisProperties temp = new PolarisProperties();
PolarisProperties temp = new PolarisProperties(new InetUtils(new InetUtilsProperties()));
try {
temp.setNamespace(NAMESPACE_TEST);
temp.getNamespace();

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

Loading…
Cancel
Save