Commit 9c416bf6 by Dave Syer

Use getHostname() instead of field in DatacenterInfo

Fixes gh-122
parent 3bbe4ae2
...@@ -136,11 +136,11 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { ...@@ -136,11 +136,11 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
public String getId() { public String getId() {
String instanceId = metadataMap.get("instanceId"); String instanceId = metadataMap.get("instanceId");
if (instanceId != null) { if (instanceId != null) {
String old = hostname; String old = getHostname();
String id = old.endsWith(instanceId) ? old : old + ":" + instanceId; String id = old.endsWith(instanceId) ? old : old + ":" + instanceId;
return id; return id;
} }
return hostname; return getHostname();
} }
} }
......
/* /*
* Copyright 2013-2014 the original author or authors. Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
* You may obtain a copy of the License at You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and See the License for the specific language governing permissions and
* limitations under the License. limitations under the License.
*/ */
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
import org.junit.After; import org.junit.After;
...@@ -106,6 +107,24 @@ public class EurekaInstanceConfigBeanTests { ...@@ -106,6 +107,24 @@ public class EurekaInstanceConfigBeanTests {
.getInitialStatus()); .getInitialStatus());
} }
@Test
public void testPerferIpAddress() throws Exception {
addEnvironment(context, "eureka.instance.preferIpAddress:true");
setupContext();
assertTrue("Wrong hostname: " + getInstanceConfig().getHostname(),
getInstanceConfig().getHostname().startsWith("127.0."));
}
@Test
public void testPerferIpAddressInDatacenter() throws Exception {
addEnvironment(context, "eureka.instance.preferIpAddress:true");
setupContext();
String id = ((UniqueIdentifier) getInstanceConfig().getDataCenterInfo()).getId();
assertTrue("Wrong hostname: " + id, id.startsWith("127.0."));
}
private void setupContext() { private void setupContext() {
context.register(PropertyPlaceholderAutoConfiguration.class, context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class); TestConfiguration.class);
...@@ -116,30 +135,6 @@ public class EurekaInstanceConfigBeanTests { ...@@ -116,30 +135,6 @@ public class EurekaInstanceConfigBeanTests {
return context.getBean(EurekaInstanceConfigBean.class); return context.getBean(EurekaInstanceConfigBean.class);
} }
/*
* @Test public void serviceUrlWithCompositePropertySource() { CompositePropertySource
* source = new CompositePropertySource("composite");
* context.getEnvironment().getPropertySources().addFirst(source);
* source.addPropertySource(new MapPropertySource("config", Collections .<String,
* Object> singletonMap("eureka.client.serviceUrl.defaultZone",
* "http://example.com")));
* context.register(PropertyPlaceholderAutoConfiguration.class,
* TestConfiguration.class); context.refresh();
* assertEquals("{defaultZone=http://example.com}",
* context.getBean(EurekaInstanceConfigBean.class).getServiceUrl().toString());
* assertEquals( "[http://example.com]",
* context.getBean(EurekaInstanceConfigBean.class)
* .getEurekaServerServiceUrls("defaultZone").toString()); }
*
* @Test public void serviceUrlWithDefault() {
* EnvironmentTestUtils.addEnvironment(context,
* "eureka.client.serviceUrl.defaultZone:",
* "eureka.client.serviceUrl.default:http://example.com");
* context.register(PropertyPlaceholderAutoConfiguration.class,
* TestConfiguration.class); context.refresh(); assertEquals( "[http://example.com]",
* context.getBean(EurekaInstanceConfigBean.class)
* .getEurekaServerServiceUrls("defaultZone").toString()); }
*/
@Configuration @Configuration
@EnableConfigurationProperties(EurekaInstanceConfigBean.class) @EnableConfigurationProperties(EurekaInstanceConfigBean.class)
protected static class TestConfiguration { protected static class TestConfiguration {
......
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