Commit da04c537 by Spencer Gibb

use new commons code to provide hostInfo.

Set the default sid from common properties.
parent 8f9acab3
...@@ -16,12 +16,16 @@ ...@@ -16,12 +16,16 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import static org.springframework.cloud.util.IdUtils.getDefaultInstanceId;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
...@@ -44,6 +48,7 @@ import org.springframework.context.ApplicationContext; ...@@ -44,6 +48,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.EurekaInstanceConfig;
...@@ -51,8 +56,6 @@ import com.netflix.appinfo.InstanceInfo; ...@@ -51,8 +56,6 @@ import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import lombok.SneakyThrows;
/** /**
* @author Dave Syer * @author Dave Syer
*/ */
...@@ -68,6 +71,9 @@ public class EurekaClientAutoConfiguration { ...@@ -68,6 +71,9 @@ public class EurekaClientAutoConfiguration {
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}") @Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
int nonSecurePort; int nonSecurePort;
@Autowired
ConfigurableEnvironment env;
@Bean @Bean
@ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean() { public EurekaClientConfigBean eurekaClientConfigBean() {
...@@ -79,6 +85,7 @@ public class EurekaClientAutoConfiguration { ...@@ -79,6 +85,7 @@ public class EurekaClientAutoConfiguration {
public EurekaInstanceConfigBean eurekaInstanceConfigBean() { public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(); EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean();
instance.setNonSecurePort(this.nonSecurePort); instance.setNonSecurePort(this.nonSecurePort);
instance.setSid(getDefaultInstanceId(env));
return instance; return instance;
} }
......
...@@ -16,29 +16,26 @@ ...@@ -16,29 +16,26 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import java.io.IOException; import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.netflix.appinfo.MyDataCenterInfo;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.util.InetUtils.HostInfo;
import com.netflix.appinfo.DataCenterInfo; import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInfo;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -52,7 +49,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { ...@@ -52,7 +49,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
@Getter(AccessLevel.PRIVATE) @Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE)
private HostInfo hostInfo = initHostInfo(); private HostInfo hostInfo = getFirstNonLoopbackHostInfo();
@Value("${spring.application.name:unknown}") @Value("${spring.application.name:unknown}")
private String appname = "unknown"; private String appname = "unknown";
...@@ -86,7 +83,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { ...@@ -86,7 +83,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
private DataCenterInfo dataCenterInfo = new MyDataCenterInfo(DataCenterInfo.Name.MyOwn); private DataCenterInfo dataCenterInfo = new MyDataCenterInfo(DataCenterInfo.Name.MyOwn);
private String ipAddress = this.hostInfo.ipAddress; private String ipAddress = this.hostInfo.getIpAddress();
private String statusPageUrlPath = "/info"; private String statusPageUrlPath = "/info";
...@@ -104,7 +101,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { ...@@ -104,7 +101,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
private String namespace = "eureka"; private String namespace = "eureka";
private String hostname = this.hostInfo.hostname; private String hostname = this.hostInfo.getHostname();
private boolean preferIpAddress = false; private boolean preferIpAddress = false;
...@@ -127,40 +124,6 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { ...@@ -127,40 +124,6 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
return this.securePortEnabled; return this.securePortEnabled;
} }
private HostInfo initHostInfo() {
this.hostInfo = this.hostInfo == null ? new HostInfo() : this.hostInfo;
InetAddress address = getFirstNonLoopbackAddress();
this.hostInfo.ipAddress = address.getHostAddress();
this.hostInfo.hostname = address.getHostName();
return this.hostInfo;
}
//TODO: move this method to s-c-commons
@SneakyThrows
static InetAddress getFirstNonLoopbackAddress() {
try {
for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces();
enumNic.hasMoreElements(); ) {
NetworkInterface ifc = enumNic.nextElement();
if (ifc.isUp()) {
for (Enumeration<InetAddress> enumAddr = ifc.getInetAddresses();
enumAddr.hasMoreElements(); ) {
InetAddress address = enumAddr.nextElement();
if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
return address;
}
}
}
}
}
catch (IOException ex) {
logger.error("Cannot get host info", ex);
}
return InetAddress.getLocalHost();
}
public void setHostname(String hostname) { public void setHostname(String hostname) {
this.hostname = hostname; this.hostname = hostname;
this.hostInfo.override = true; this.hostInfo.override = true;
...@@ -169,18 +132,14 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig { ...@@ -169,18 +132,14 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
@Override @Override
public String getHostName(boolean refresh) { public String getHostName(boolean refresh) {
if (refresh) { if (refresh) {
this.hostInfo = initHostInfo(); boolean originalOverride = this.hostInfo.override;
this.ipAddress = this.hostInfo.ipAddress; this.hostInfo = getFirstNonLoopbackHostInfo();
this.hostInfo.setOverride(originalOverride);
this.ipAddress = this.hostInfo.getIpAddress();
if (!this.hostInfo.override) { if (!this.hostInfo.override) {
this.hostname = this.hostInfo.hostname; this.hostname = this.hostInfo.getHostname();
} }
} }
return this.preferIpAddress ? this.ipAddress : this.hostname; return this.preferIpAddress ? this.ipAddress : this.hostname;
} }
private final class HostInfo {
public boolean override;
private String ipAddress;
private String hostname;
}
} }
...@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.eureka; ...@@ -19,6 +19,7 @@ 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.junit.Assert.assertTrue;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
...@@ -42,8 +43,7 @@ public class EurekaInstanceConfigBeanTests { ...@@ -42,8 +43,7 @@ public class EurekaInstanceConfigBeanTests {
@Before @Before
public void init() { public void init() {
this.hostName = EurekaInstanceConfigBean.getFirstNonLoopbackAddress() this.hostName = getFirstNonLoopbackHostInfo().getHostname();
.getHostName();
} }
@After @After
......
...@@ -3,6 +3,7 @@ package org.springframework.cloud.netflix.eureka; ...@@ -3,6 +3,7 @@ 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.junit.Assert.assertTrue;
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
...@@ -17,19 +18,22 @@ public class InstanceInfoFactoryTests { ...@@ -17,19 +18,22 @@ public class InstanceInfoFactoryTests {
@Test @Test
public void instanceIdIsHostNameByDefault() { public void instanceIdIsHostNameByDefault() {
assertEquals(EurekaInstanceConfigBean.getFirstNonLoopbackAddress().getHostName(), InstanceInfo instanceInfo = setupInstance();
setupInstance().getId()); assertEquals(getFirstNonLoopbackHostInfo().getHostname(),
instanceInfo.getId());
} }
@Test @Test
public void instanceIdIsIpWhenIpPreferred() throws Exception { public void instanceIdIsIpWhenIpPreferred() throws Exception {
assertTrue(setupInstance("eureka.instance.preferIpAddress:true").getId().matches( InstanceInfo instanceInfo = setupInstance("eureka.instance.preferIpAddress:true");
assertTrue(instanceInfo.getId().matches(
"(\\d+\\.){3}\\d+")); "(\\d+\\.){3}\\d+"));
} }
@Test @Test
public void instanceIdIsSidWhenSet() { public void instanceIdIsSidWhenSet() {
assertEquals("special", setupInstance("eureka.instance.sid:special").getId()); InstanceInfo instanceInfo = setupInstance("eureka.instance.sid:special");
assertEquals("special", instanceInfo.getId());
} }
private InstanceInfo setupInstance(String... pairs) { private InstanceInfo setupInstance(String... pairs) {
......
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