From b0b4916d3c9a74585efd3d401c0b6c89d0b09311 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 27 Jul 2015 17:54:22 +0100 Subject: [PATCH] If user configures hostname for eureka client, do not refresh it Fixes gh-463 --- .../eureka/EurekaInstanceConfigBean.java | 20 ++++-- .../eureka/EurekaInstanceConfigBeanTests.java | 61 ++++++++++++++++--- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java index cf1b2917..92969e1f 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java @@ -113,15 +113,20 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { } private HostInfo initHostInfo() { - HostInfo info = new HostInfo(); + this.hostInfo = this.hostInfo == null ? new HostInfo() : this.hostInfo; try { - info.ipAddress = InetAddress.getLocalHost().getHostAddress(); - info.hostname = InetAddress.getLocalHost().getHostName(); + this.hostInfo.ipAddress = InetAddress.getLocalHost().getHostAddress(); + this.hostInfo.hostname = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException ex) { logger.error("Cannot get host info", ex); } - return info; + return this.hostInfo; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + this.hostInfo.override = true; } @Override @@ -129,18 +134,21 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { if (refresh) { this.hostInfo = initHostInfo(); this.ipAddress = this.hostInfo.ipAddress; - this.hostname = this.hostInfo.hostname; + if (!this.hostInfo.override) { + this.hostname = this.hostInfo.hostname; + } } return this.preferIpAddress ? this.ipAddress : this.hostname; } private final class HostInfo { + public boolean override; private String ipAddress; private String hostname; } private final class IdentifyingDataCenterInfo implements DataCenterInfo, - UniqueIdentifier { + UniqueIdentifier { @Getter @Setter diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java index 8f31e98f..da18bb30 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java @@ -16,30 +16,46 @@ package org.springframework.cloud.netflix.eureka; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; + +import java.net.InetAddress; +import java.net.UnknownHostException; + import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.test.util.ReflectionTestUtils; import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.UniqueIdentifier; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; - /** * @author Dave Syer */ public class EurekaInstanceConfigBeanTests { private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + private String hostName; + + @Before + public void init() { + try { + this.hostName = InetAddress.getLocalHost().getHostName(); + } + catch (UnknownHostException e) { + // Ignore (test must be running in a restricted environment) + } + } @After - public void init() { + public void clear() { if (this.context != null) { this.context.close(); } @@ -67,6 +83,37 @@ public class EurekaInstanceConfigBeanTests { assertEquals(8888, getInstanceConfig().getNonSecurePort()); } + @Test + public void initialHostName() { + addEnvironment(this.context, "eureka.instance.appGroupName=mygroup"); + setupContext(); + if (this.hostName != null) { + assertEquals(this.hostName, getInstanceConfig().getHostname()); + } + } + + @Test + public void refreshHostName() { + addEnvironment(this.context, "eureka.instance.appGroupName=mygroup"); + setupContext(); + ReflectionTestUtils.setField(getInstanceConfig(), "hostname", "marvin"); + assertEquals("marvin", getInstanceConfig().getHostname()); + getInstanceConfig().getHostName(true); + if (this.hostName != null) { + assertEquals(this.hostName, getInstanceConfig().getHostname()); + } + } + + @Test + public void refreshHostNameWhenSetByUser() { + addEnvironment(this.context, "eureka.instance.appGroupName=mygroup"); + setupContext(); + getInstanceConfig().setHostname("marvin"); + assertEquals("marvin", getInstanceConfig().getHostname()); + getInstanceConfig().getHostName(true); + assertEquals("marvin", getInstanceConfig().getHostname()); + } + @Test public void testDefaultInitialStatus() { setupContext(); @@ -89,7 +136,7 @@ public class EurekaInstanceConfigBeanTests { } @Test - public void testPerferIpAddress() throws Exception { + public void testPreferIpAddress() throws Exception { addEnvironment(this.context, "eureka.instance.preferIpAddress:true"); setupContext(); EurekaInstanceConfigBean instance = getInstanceConfig(); @@ -99,7 +146,7 @@ public class EurekaInstanceConfigBeanTests { } @Test - public void testPerferIpAddressInDatacenter() throws Exception { + public void testPreferIpAddressInDatacenter() throws Exception { addEnvironment(this.context, "eureka.instance.preferIpAddress:true"); setupContext(); EurekaInstanceConfigBean instance = getInstanceConfig();