Update to use new InetUtils bean for nic discovery.

This commit is contained in:
Spencer Gibb
2015-12-22 18:19:38 -07:00
parent c54cb8b827
commit 5778182a8f
2 changed files with 21 additions and 19 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.consul.ConditionalOnConsulEnabled;
import org.springframework.cloud.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -39,8 +40,8 @@ public class ConsulDiscoveryClientConfiguration {
private ConsulClient consulClient;
@Bean
public ConsulLifecycle consulLifecycle() {
return new ConsulLifecycle(consulClient, lifecycleProperties(), consulDiscoveryProperties(), heartbeatProperties());
public ConsulLifecycle consulLifecycle(ConsulDiscoveryProperties discoveryProperties) {
return new ConsulLifecycle(consulClient, lifecycleProperties(), discoveryProperties, heartbeatProperties());
}
@Bean
@@ -60,17 +61,17 @@ public class ConsulDiscoveryClientConfiguration {
}
@Bean
public ConsulDiscoveryProperties consulDiscoveryProperties() {
return new ConsulDiscoveryProperties();
public ConsulDiscoveryProperties consulDiscoveryProperties(InetUtils inetUtils) {
return new ConsulDiscoveryProperties(inetUtils);
}
@Bean
public ConsulDiscoveryClient consulDiscoveryClient(ServerProperties serverProperties) {
return new ConsulDiscoveryClient(consulClient, consulLifecycle(), consulDiscoveryProperties(), serverProperties);
public ConsulDiscoveryClient consulDiscoveryClient(ServerProperties serverProperties, ConsulDiscoveryProperties discoveryProperties) {
return new ConsulDiscoveryClient(consulClient, consulLifecycle(discoveryProperties), discoveryProperties, serverProperties);
}
@Bean
public ConsulCatalogWatch consulCatalogWatch() {
return new ConsulCatalogWatch(consulDiscoveryProperties(), consulClient);
public ConsulCatalogWatch consulCatalogWatch(ConsulDiscoveryProperties discoveryProperties) {
return new ConsulCatalogWatch(discoveryProperties, consulClient);
}
}

View File

@@ -30,6 +30,7 @@ import lombok.Setter;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.util.InetUtils;
/**
* @author Spencer Gibb
@@ -43,7 +44,7 @@ public class ConsulDiscoveryProperties {
@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private String[] hostInfo = initHostInfo();
private InetUtils.HostInfo hostInfo;
private String aclToken;
@@ -59,9 +60,9 @@ public class ConsulDiscoveryProperties {
private String healthCheckInterval = "10s";
private String ipAddress = this.hostInfo[0];
private String ipAddress;
private String hostname = hostInfo[1];
private String hostname;
/**
* Use ip address rather than hostname during registration
@@ -78,15 +79,15 @@ public class ConsulDiscoveryProperties {
private String managementSuffix = MANAGEMENT;
private ConsulDiscoveryProperties() {}
public ConsulDiscoveryProperties(InetUtils inetUtils) {
this.hostInfo = inetUtils.findFirstNonLoopbackHostInfo();
this.ipAddress = this.hostInfo.getIpAddress();
this.hostname = this.hostInfo.getHostname();
}
public String getHostname() {
return this.preferIpAddress ? this.ipAddress : this.hostname;
}
private String[] initHostInfo() {
String[] info = new String[2];
InetAddress address = getFirstNonLoopbackAddress();
info[0] = address.getHostAddress();
info[1] = address.getHostName();
return info;
}
}