If user configures hostname for eureka client, do not refresh it

Fixes gh-463
This commit is contained in:
Dave Syer
2015-07-27 17:54:22 +01:00
parent cb732763b5
commit b0b4916d3c
2 changed files with 68 additions and 13 deletions

View File

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

View File

@@ -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();