Commit ddd7979a by Spencer Gibb

Don't refresh the ipAddress if set by user config.

fixes gh-573
parent 3fb26f69
...@@ -308,17 +308,17 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { ...@@ -308,17 +308,17 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
this.hostInfo.override = true; this.hostInfo.override = true;
} }
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
this.hostInfo.override = true;
}
@Override @Override
public String getHostName(boolean refresh) { public String getHostName(boolean refresh) {
if (refresh) { if (refresh && !this.hostInfo.override) {
boolean originalOverride = this.hostInfo.override;
this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo();
this.hostInfo.setOverride(originalOverride);
this.ipAddress = this.hostInfo.getIpAddress(); this.ipAddress = this.hostInfo.getIpAddress();
if (!this.hostInfo.override) {
this.hostname = this.hostInfo.getHostname(); this.hostname = this.hostInfo.getHostname();
} }
}
return this.preferIpAddress ? this.ipAddress : this.hostname; return this.preferIpAddress ? this.ipAddress : this.hostname;
} }
} }
...@@ -43,11 +43,14 @@ public class EurekaInstanceConfigBeanTests { ...@@ -43,11 +43,14 @@ public class EurekaInstanceConfigBeanTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private String hostName; private String hostName;
private String ipAddress;
@Before @Before
public void init() { public void init() {
this.hostName = new InetUtils(new InetUtilsProperties()) InetUtils.HostInfo hostInfo = new InetUtils(new InetUtilsProperties())
.findFirstNonLoopbackHostInfo().getHostname(); .findFirstNonLoopbackHostInfo();
this.hostName = hostInfo.getHostname();
this.ipAddress = hostInfo.getIpAddress();
} }
@After @After
...@@ -111,6 +114,37 @@ public class EurekaInstanceConfigBeanTests { ...@@ -111,6 +114,37 @@ public class EurekaInstanceConfigBeanTests {
} }
@Test @Test
public void initialIpAddress() {
addEnvironment(this.context, "eureka.instance.appGroupName=mygroup");
setupContext();
if (this.ipAddress != null) {
assertEquals(this.ipAddress, getInstanceConfig().getIpAddress());
}
}
@Test
public void refreshIpAddress() {
addEnvironment(this.context, "eureka.instance.appGroupName=mygroup");
setupContext();
ReflectionTestUtils.setField(getInstanceConfig(), "ipAddress", "10.0.0.1");
assertEquals("10.0.0.1", getInstanceConfig().getIpAddress());
getInstanceConfig().getHostName(true);
if (this.ipAddress != null) {
assertEquals(this.ipAddress, getInstanceConfig().getIpAddress());
}
}
@Test
public void refreshIpAddressWhenSetByUser() {
addEnvironment(this.context, "eureka.instance.appGroupName=mygroup");
setupContext();
getInstanceConfig().setIpAddress("10.0.0.1");
assertEquals("10.0.0.1", getInstanceConfig().getIpAddress());
getInstanceConfig().getHostName(true);
assertEquals("10.0.0.1", getInstanceConfig().getIpAddress());
}
@Test
public void testDefaultInitialStatus() { public void testDefaultInitialStatus() {
setupContext(); setupContext();
assertEquals("initialStatus wrong", InstanceStatus.UP, getInstanceConfig() assertEquals("initialStatus wrong", InstanceStatus.UP, getInstanceConfig()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment