Merge pull request #47 from SkyeBeFreeman/main

feat:support customize registered ip address.
pull/51/head
Haotian Zhang 3 years ago committed by GitHub
commit 68cf3d9cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -91,6 +91,7 @@ spring:
| spring.cloud.polaris.discovery.instance-enabled | true | 否 | 当前微服务实例是否可以被访问 | | spring.cloud.polaris.discovery.instance-enabled | true | 否 | 当前微服务实例是否可以被访问 |
| spring.cloud.polaris.discovery.token | 无 | 否 | 鉴权Token | | spring.cloud.polaris.discovery.token | 无 | 否 | 鉴权Token |
| spring.cloud.polaris.discovery.version | null | 否 | 微服务版本 | | spring.cloud.polaris.discovery.version | null | 否 | 微服务版本 |
| spring.cloud.polaris.discovery.ip-address | null | 否 | 注册的IP地址 |
| spring.cloud.polaris.protocol | null | 否 | 微服务协议类型 | | spring.cloud.polaris.protocol | null | 否 | 微服务协议类型 |
| spring.cloud.polaris.weight | 100 | 否 | 微服务权重 | | spring.cloud.polaris.weight | 100 | 否 | 微服务权重 |
| spring.cloud.loadbalancer.polaris.enabled | true | 否 | 是否开启负载均衡 | | spring.cloud.loadbalancer.polaris.enabled | true | 否 | 是否开启负载均衡 |

@ -88,6 +88,7 @@ Taking examples like random weight policy, you can add weight at Polaris control
| spring.cloud.polaris.discovery.instance-enabled | true | false | can current Microservice be visited | | spring.cloud.polaris.discovery.instance-enabled | true | false | can current Microservice be visited |
| spring.cloud.polaris.discovery.token | false | false | Authentication Token | | spring.cloud.polaris.discovery.token | false | false | Authentication Token |
| spring.cloud.polaris.discovery.version | null | false | Microservice Version | | spring.cloud.polaris.discovery.version | null | false | Microservice Version |
| spring.cloud.polaris.discovery.ip-address | null | false | Ip address to be registered |
| spring.cloud.polaris.protocol | null | false | Microservice agreement type | | spring.cloud.polaris.protocol | null | false | Microservice agreement type |
| spring.cloud.polaris.weight | 100 | false | Microservice weight | | spring.cloud.polaris.weight | 100 | false | Microservice weight |
| spring.cloud.loadbalancer.polaris.enabled | true | false | whether to open CLB | | spring.cloud.loadbalancer.polaris.enabled | true | false | whether to open CLB |

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