use new commons code to provide hostInfo.
Set the default sid from common properties.
This commit is contained in:
@@ -16,12 +16,16 @@
|
||||
|
||||
package org.springframework.cloud.netflix.eureka;
|
||||
|
||||
import static org.springframework.cloud.util.IdUtils.getDefaultInstanceId;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
@@ -44,6 +48,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
import com.netflix.appinfo.ApplicationInfoManager;
|
||||
import com.netflix.appinfo.EurekaInstanceConfig;
|
||||
@@ -51,8 +56,6 @@ import com.netflix.appinfo.InstanceInfo;
|
||||
import com.netflix.discovery.EurekaClient;
|
||||
import com.netflix.discovery.EurekaClientConfig;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@@ -68,6 +71,9 @@ public class EurekaClientAutoConfiguration {
|
||||
@Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
|
||||
int nonSecurePort;
|
||||
|
||||
@Autowired
|
||||
ConfigurableEnvironment env;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT)
|
||||
public EurekaClientConfigBean eurekaClientConfigBean() {
|
||||
@@ -79,6 +85,7 @@ public class EurekaClientAutoConfiguration {
|
||||
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
|
||||
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean();
|
||||
instance.setNonSecurePort(this.nonSecurePort);
|
||||
instance.setSid(getDefaultInstanceId(env));
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,29 +16,26 @@
|
||||
|
||||
package org.springframework.cloud.netflix.eureka;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.netflix.appinfo.MyDataCenterInfo;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.util.InetUtils.HostInfo;
|
||||
|
||||
import com.netflix.appinfo.DataCenterInfo;
|
||||
import com.netflix.appinfo.EurekaInstanceConfig;
|
||||
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
|
||||
import com.netflix.appinfo.MyDataCenterInfo;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -52,7 +49,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
|
||||
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@Setter(AccessLevel.PRIVATE)
|
||||
private HostInfo hostInfo = initHostInfo();
|
||||
private HostInfo hostInfo = getFirstNonLoopbackHostInfo();
|
||||
|
||||
@Value("${spring.application.name:unknown}")
|
||||
private String appname = "unknown";
|
||||
@@ -86,7 +83,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
|
||||
|
||||
private DataCenterInfo dataCenterInfo = new MyDataCenterInfo(DataCenterInfo.Name.MyOwn);
|
||||
|
||||
private String ipAddress = this.hostInfo.ipAddress;
|
||||
private String ipAddress = this.hostInfo.getIpAddress();
|
||||
|
||||
private String statusPageUrlPath = "/info";
|
||||
|
||||
@@ -104,7 +101,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
|
||||
|
||||
private String namespace = "eureka";
|
||||
|
||||
private String hostname = this.hostInfo.hostname;
|
||||
private String hostname = this.hostInfo.getHostname();
|
||||
|
||||
private boolean preferIpAddress = false;
|
||||
|
||||
@@ -127,40 +124,6 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
|
||||
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) {
|
||||
this.hostname = hostname;
|
||||
this.hostInfo.override = true;
|
||||
@@ -169,18 +132,14 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
|
||||
@Override
|
||||
public String getHostName(boolean refresh) {
|
||||
if (refresh) {
|
||||
this.hostInfo = initHostInfo();
|
||||
this.ipAddress = this.hostInfo.ipAddress;
|
||||
boolean originalOverride = this.hostInfo.override;
|
||||
this.hostInfo = getFirstNonLoopbackHostInfo();
|
||||
this.hostInfo.setOverride(originalOverride);
|
||||
this.ipAddress = this.hostInfo.getIpAddress();
|
||||
if (!this.hostInfo.override) {
|
||||
this.hostname = this.hostInfo.hostname;
|
||||
this.hostname = this.hostInfo.getHostname();
|
||||
}
|
||||
}
|
||||
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;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
|
||||
import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -42,8 +43,7 @@ public class EurekaInstanceConfigBeanTests {
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.hostName = EurekaInstanceConfigBean.getFirstNonLoopbackAddress()
|
||||
.getHostName();
|
||||
this.hostName = getFirstNonLoopbackHostInfo().getHostname();
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
@@ -3,6 +3,7 @@ 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 static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
@@ -17,19 +18,22 @@ public class InstanceInfoFactoryTests {
|
||||
|
||||
@Test
|
||||
public void instanceIdIsHostNameByDefault() {
|
||||
assertEquals(EurekaInstanceConfigBean.getFirstNonLoopbackAddress().getHostName(),
|
||||
setupInstance().getId());
|
||||
InstanceInfo instanceInfo = setupInstance();
|
||||
assertEquals(getFirstNonLoopbackHostInfo().getHostname(),
|
||||
instanceInfo.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
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+"));
|
||||
}
|
||||
|
||||
@Test
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user