Use prefer-ip-address in statusPageUrlPath

prefer-ip-address does not work in statusPageUrlPath when management port != server port

This change gets `prefer-ip-address` via a relaxed property resolver to set the value during the creation of the `EurekaInstanceConfigBean`.

fixes gh-1724
This commit is contained in:
windinn
2017-03-01 02:07:43 +08:00
committed by Spencer Gibb
parent 309b280d0d
commit 129e0ce4b5
2 changed files with 20 additions and 6 deletions

View File

@@ -88,9 +88,6 @@ public class EurekaClientAutoConfiguration {
@Value("${management.port:${MANAGEMENT_PORT:${server.port:${SERVER_PORT:${PORT:8080}}}}}")
private int managementPort;
@Value("${eureka.instance.hostname:${EUREKA_INSTANCE_HOSTNAME:}}")
private String hostname;
@Autowired
private ConfigurableEnvironment env;
@@ -117,15 +114,18 @@ public class EurekaClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) {
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "eureka.instance.");
String hostname = relaxedPropertyResolver.getProperty("hostname");
boolean preferIpAddress = Boolean.parseBoolean(relaxedPropertyResolver.getProperty("preferIpAddress"));
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
instance.setNonSecurePort(this.nonSecurePort);
instance.setInstanceId(getDefaultInstanceId(this.env));
instance.setPreferIpAddress(preferIpAddress);
if (this.managementPort != this.nonSecurePort && this.managementPort != 0) {
if (StringUtils.hasText(this.hostname)) {
instance.setHostname(this.hostname);
if (StringUtils.hasText(hostname)) {
instance.setHostname(hostname);
}
RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "eureka.instance.");
String statusPageUrlPath = relaxedPropertyResolver.getProperty("statusPageUrlPath");
String healthCheckUrlPath = relaxedPropertyResolver.getProperty("healthCheckUrlPath");
if (StringUtils.hasText(statusPageUrlPath)) {

View File

@@ -124,6 +124,20 @@ public class EurekaClientAutoConfigurationTests {
instance.getStatusPageUrl().contains("/myStatusPage"));
}
@Test
public void statusPageUrlAndPreferIpAddress() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",
"management.port=9999", "eureka.instance.hostname=foo",
"eureka.instance.preferIpAddress:true");
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertEquals("statusPageUrl is wrong", "http://" + instance.getIpAddress() + ":9999/info",
instance.getStatusPageUrl());
}
@Test
public void healthCheckUrlPathAndManagementPortKabobCase() {
EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989",