Commit b001fea3 by Dave Syer

Re-order configs and add refresh scope for lazy loading

so that Eureka InstanceInfo (which is designed to be immutable) can effectively be mutated (by using a different instance) after the dynamic port is available.
parent 3dfb8b5d
...@@ -25,9 +25,7 @@ import org.springframework.cloud.client.ServiceInstance; ...@@ -25,9 +25,7 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.cloud.netflix.eureka.DiscoveryClientConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
...@@ -47,7 +45,7 @@ import lombok.extern.apachecommons.CommonsLog; ...@@ -47,7 +45,7 @@ import lombok.extern.apachecommons.CommonsLog;
@ConditionalOnClass({ EurekaClient.class, ConfigServicePropertySourceLocator.class }) @ConditionalOnClass({ EurekaClient.class, ConfigServicePropertySourceLocator.class })
@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false) @ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false)
@Configuration @Configuration
@Import({EurekaClientAutoConfiguration.class, DiscoveryClientConfiguration.class}) @Import(EurekaClientAutoConfiguration.class)
@CommonsLog @CommonsLog
public class DiscoveryClientConfigServiceBootstrapConfiguration { public class DiscoveryClientConfigServiceBootstrapConfiguration {
......
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.eureka;
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.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.DiscoveryClient.DiscoveryClientOptionalArgs;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
/**
* @author Spencer Gibb
*/
@Configuration
public class DiscoveryClientConfiguration {
@Bean
public DiscoveryClient discoveryClient(EurekaInstanceConfig config,
EurekaClient client) {
return new EurekaDiscoveryClient(config, client);
}
@Configuration
@ConditionalOnMissingRefreshScope
protected static class EurekaClientConfiguration {
@Autowired
private ApplicationContext context;
@Autowired(required = false)
private DiscoveryClientOptionalArgs optionalArgs;
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@SneakyThrows
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config) {
return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context);
}
}
@Configuration
@ConditionalOnRefreshScope
protected static class RefreshableEurekaClientConfiguration {
@Autowired
private ApplicationContext context;
@Autowired(required = false)
private DiscoveryClientOptionalArgs optionalArgs;
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@SneakyThrows
@org.springframework.cloud.context.config.annotation.RefreshScope
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config, EurekaInstanceConfig instance) {
return new CloudEurekaClient(applicationInfoManager, config, optionalArgs, this.context);
}
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnMissingRefreshScopeCondition.class)
@interface ConditionalOnMissingRefreshScope {
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnRefreshScopeCondition.class)
@interface ConditionalOnRefreshScope {
}
private static class OnMissingRefreshScopeCondition extends AnyNestedCondition {
public OnMissingRefreshScopeCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnMissingClass("org.springframework.cloud.context.scope.refresh.RefreshScope")
static class MissingClass {
}
@ConditionalOnMissingBean(RefreshScope.class)
static class MissingScope {
}
}
private static class OnRefreshScopeCondition extends AllNestedConditions {
public OnRefreshScopeCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnClass(RefreshScope.class)
@ConditionalOnBean(RefreshScope.class)
static class FoundScope {
}
}
}
...@@ -16,31 +16,46 @@ ...@@ -16,31 +16,46 @@
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.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
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;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.client.CommonsClientAutoConfiguration; import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.actuator.HasFeatures; import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration; import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
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.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; 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;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient.DiscoveryClientOptionalArgs;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import static org.springframework.cloud.util.IdUtils.getDefaultInstanceId;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Spencer Gibb * @author Spencer Gibb
...@@ -77,21 +92,118 @@ public class EurekaClientAutoConfiguration { ...@@ -77,21 +92,118 @@ 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.setInstanceId(getDefaultInstanceId(env)); instance.setInstanceId(getDefaultInstanceId(this.env));
return instance; return instance;
} }
@Bean @Bean
@ConditionalOnMissingBean(ApplicationInfoManager.class) public DiscoveryClient discoveryClient(EurekaInstanceConfig config,
public ApplicationInfoManager applicationInfoManager(EurekaInstanceConfig config, EurekaClient client) {
InstanceInfo instanceInfo) { return new EurekaDiscoveryClient(config, client);
}
@Configuration
@ConditionalOnMissingRefreshScope
protected static class EurekaClientConfiguration {
@Autowired
private ApplicationContext context;
@Autowired(required = false)
private DiscoveryClientOptionalArgs optionalArgs;
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config) {
return new CloudEurekaClient(applicationInfoManager, config,
this.optionalArgs, this.context);
}
@Bean
@ConditionalOnMissingBean(value = ApplicationInfoManager.class, search = SearchStrategy.CURRENT)
public ApplicationInfoManager eurekaApplicationInfoManager(
EurekaInstanceConfig config) {
InstanceInfo instanceInfo = new InstanceInfoFactory().create(config);
return new ApplicationInfoManager(config, instanceInfo); return new ApplicationInfoManager(config, instanceInfo);
} }
}
@Configuration
@ConditionalOnRefreshScope
protected static class RefreshableEurekaClientConfiguration {
@Autowired
private ApplicationContext context;
@Autowired(required = false)
private DiscoveryClientOptionalArgs optionalArgs;
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@org.springframework.cloud.context.config.annotation.RefreshScope
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config, EurekaInstanceConfig instance) {
applicationInfoManager.getInfo(); // force initialization
return new CloudEurekaClient(applicationInfoManager, config,
this.optionalArgs, this.context);
}
@Bean @Bean
@ConditionalOnMissingBean(InstanceInfo.class) @ConditionalOnMissingBean(value = ApplicationInfoManager.class, search = SearchStrategy.CURRENT)
public InstanceInfo instanceInfo(EurekaInstanceConfig config) { @org.springframework.cloud.context.config.annotation.RefreshScope
return new InstanceInfoFactory().create(config); public ApplicationInfoManager eurekaApplicationInfoManager(
EurekaInstanceConfig config) {
InstanceInfo instanceInfo = new InstanceInfoFactory().create(config);
return new ApplicationInfoManager(config, instanceInfo);
}
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnMissingRefreshScopeCondition.class)
@interface ConditionalOnMissingRefreshScope {
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnRefreshScopeCondition.class)
@interface ConditionalOnRefreshScope {
}
private static class OnMissingRefreshScopeCondition extends AnyNestedCondition {
public OnMissingRefreshScopeCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnMissingClass("org.springframework.cloud.context.scope.refresh.RefreshScope")
static class MissingClass {
}
@ConditionalOnMissingBean(RefreshAutoConfiguration.class)
static class MissingScope {
}
}
private static class OnRefreshScopeCondition extends AllNestedConditions {
public OnRefreshScopeCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnClass(RefreshScope.class)
@ConditionalOnBean(RefreshAutoConfiguration.class)
static class FoundScope {
}
} }
} }
...@@ -16,13 +16,9 @@ ...@@ -16,13 +16,9 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.health.HealthAggregator; import org.springframework.boot.actuate.health.HealthAggregator;
...@@ -40,7 +36,6 @@ import org.springframework.context.ApplicationContext; ...@@ -40,7 +36,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle; import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
...@@ -48,11 +43,11 @@ import org.springframework.core.Ordered; ...@@ -48,11 +43,11 @@ import org.springframework.core.Ordered;
import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.HealthCheckHandler; import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig; import com.netflix.discovery.EurekaClientConfig;
import org.springframework.util.ReflectionUtils;
import lombok.extern.apachecommons.CommonsLog;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -64,7 +59,6 @@ import org.springframework.util.ReflectionUtils; ...@@ -64,7 +59,6 @@ import org.springframework.util.ReflectionUtils;
@EnableConfigurationProperties @EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class) @ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
@Import(DiscoveryClientConfiguration.class)
@CommonsLog @CommonsLog
public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Ordered { public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Ordered {
...@@ -89,22 +83,18 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -89,22 +83,18 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
@Autowired @Autowired
private EurekaClient eurekaClient; private EurekaClient eurekaClient;
@Autowired
private InstanceInfo instanceInfo;
@Override @Override
public void start() { public void start() {
// only set the port if the nonSecurePort is 0 and this.port != 0 // only set the port if the nonSecurePort is 0 and this.port != 0
if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) { if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) {
this.instanceConfig.setNonSecurePort(this.port.get()); this.instanceConfig.setNonSecurePort(this.port.get());
setInstanceInfoPort();
} }
// only initialize if nonSecurePort is greater than 0 and it isn't already running // only initialize if nonSecurePort is greater than 0 and it isn't already running
// because of containerPortInitializer below // because of containerPortInitializer below
if (!this.running.get() && this.instanceConfig.getNonSecurePort() > 0) { if (!this.running.get() && this.instanceConfig.getNonSecurePort() > 0) {
this.eurekaClient.getApplications(); // force initialization maybeInitializeClient();
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("Registering application " + this.instanceConfig.getAppname() log.info("Registering application " + this.instanceConfig.getAppname()
...@@ -124,11 +114,10 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -124,11 +114,10 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
} }
} }
@SneakyThrows private void maybeInitializeClient() {
private void setInstanceInfoPort() { // force initialization of possibly scoped proxies
Field port = ReflectionUtils.findField(InstanceInfo.class, "port"); this.applicationInfoManager.getInfo();
ReflectionUtils.makeAccessible(port); this.eurekaClient.getApplications();
port.setInt(this.instanceInfo, this.port.get());
} }
@Override @Override
...@@ -174,9 +163,12 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order ...@@ -174,9 +163,12 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
@EventListener(EmbeddedServletContainerInitializedEvent.class) @EventListener(EmbeddedServletContainerInitializedEvent.class)
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
// TODO: take SSL into account when Spring Boot 1.2 is available // TODO: take SSL into account when Spring Boot 1.2 is available
EurekaDiscoveryClientConfiguration.this.port.compareAndSet(0, int localPort = event.getEmbeddedServletContainer().getPort();
event.getEmbeddedServletContainer().getPort()); if (this.port.get() == 0) {
EurekaDiscoveryClientConfiguration.this.start(); log.info("Updating port to " + localPort);
this.port.compareAndSet(0, localPort);
start();
}
} }
@Configuration @Configuration
......
...@@ -16,18 +16,9 @@ ...@@ -16,18 +16,9 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.logging.Log;
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 org.springframework.cloud.util.InetUtils.HostInfo;
...@@ -36,6 +27,13 @@ import com.netflix.appinfo.DataCenterInfo; ...@@ -36,6 +27,13 @@ import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus; import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInfo; import com.netflix.appinfo.MyDataCenterInfo;
import static org.springframework.cloud.util.InetUtils.getFirstNonLoopbackHostInfo;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Spencer Gibb * @author Spencer Gibb
...@@ -44,8 +42,6 @@ import com.netflix.appinfo.MyDataCenterInfo; ...@@ -44,8 +42,6 @@ import com.netflix.appinfo.MyDataCenterInfo;
@ConfigurationProperties("eureka.instance") @ConfigurationProperties("eureka.instance")
public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
private static final Log logger = LogFactory.getLog(EurekaInstanceConfigBean.class);
@Getter(AccessLevel.PRIVATE) @Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE)
private HostInfo hostInfo = getFirstNonLoopbackHostInfo(); private HostInfo hostInfo = getFirstNonLoopbackHostInfo();
...@@ -80,7 +76,8 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { ...@@ -80,7 +76,8 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
private Map<String, String> metadataMap = new HashMap<>(); private Map<String, String> metadataMap = new HashMap<>();
private DataCenterInfo dataCenterInfo = new MyDataCenterInfo(DataCenterInfo.Name.MyOwn); private DataCenterInfo dataCenterInfo = new MyDataCenterInfo(
DataCenterInfo.Name.MyOwn);
private String ipAddress = this.hostInfo.getIpAddress(); private String ipAddress = this.hostInfo.getIpAddress();
...@@ -115,7 +112,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig { ...@@ -115,7 +112,7 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig {
if (this.instanceId == null && this.metadataMap != null) { if (this.instanceId == null && this.metadataMap != null) {
return this.metadataMap.get("instanceId"); return this.metadataMap.get("instanceId");
} }
return instanceId; return this.instanceId;
} }
@Override @Override
......
...@@ -16,10 +16,6 @@ ...@@ -16,10 +16,6 @@
package org.springframework.cloud.netflix.config; package org.springframework.cloud.netflix.config;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
...@@ -36,6 +32,10 @@ import com.netflix.appinfo.ApplicationInfoManager; ...@@ -36,6 +32,10 @@ import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient; import com.netflix.discovery.EurekaClient;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
/** /**
* @author Dave Syer * @author Dave Syer
*/ */
...@@ -57,8 +57,7 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests { ...@@ -57,8 +57,7 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
public void onWhenRequested() throws Exception { public void onWhenRequested() throws Exception {
setup("spring.cloud.config.discovery.enabled=true", setup("spring.cloud.config.discovery.enabled=true",
"eureka.instance.metadataMap.foo:bar", "eureka.instance.metadataMap.foo:bar",
"eureka.instance.nonSecurePort:7001", "eureka.instance.nonSecurePort:7001", "eureka.instance.hostname:foo");
"eureka.instance.hostname:foo");
assertEquals(1, this.context.getBeanNamesForType( assertEquals(1, this.context.getBeanNamesForType(
DiscoveryClientConfigServiceAutoConfiguration.class).length); DiscoveryClientConfigServiceAutoConfiguration.class).length);
EurekaClient eurekaClient = this.context.getParent().getBean(EurekaClient.class); EurekaClient eurekaClient = this.context.getParent().getBean(EurekaClient.class);
...@@ -93,10 +92,10 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests { ...@@ -93,10 +92,10 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
protected static class EnvironmentKnobbler { protected static class EnvironmentKnobbler {
@Bean @Bean
public EurekaClient eurekaClient(InstanceInfo info) { public EurekaClient eurekaClient(ApplicationInfoManager manager) {
InstanceInfo info = manager.getInfo();
EurekaClient client = Mockito.mock(EurekaClient.class); EurekaClient client = Mockito.mock(EurekaClient.class);
given(client.getNextServerFromEureka("CONFIGSERVER", false)) given(client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(info);
.willReturn(info);
return client; return client;
} }
......
...@@ -16,19 +16,20 @@ ...@@ -16,19 +16,20 @@
package org.springframework.cloud.netflix.eureka; package org.springframework.cloud.netflix.eureka;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.netflix.eureka.DiscoveryClientConfiguration.ConditionalOnRefreshScope; import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration.ConditionalOnRefreshScope;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
/** /**
* @author Dave Syer * @author Dave Syer
* *
...@@ -47,7 +48,7 @@ public class ConditionalOnRefreshScopeTests { ...@@ -47,7 +48,7 @@ public class ConditionalOnRefreshScopeTests {
@Test @Test
public void refreshScopeIncluded() { public void refreshScopeIncluded() {
this.context = new SpringApplicationBuilder(RefreshAutoConfiguration.class, this.context = new SpringApplicationBuilder(RefreshAutoConfiguration.class,
Beans.class).web(false).showBanner(false).run(); Beans.class).web(false).bannerMode(Mode.OFF).run();
assertNotNull(this.context.getBean( assertNotNull(this.context.getBean(
org.springframework.cloud.context.scope.refresh.RefreshScope.class)); org.springframework.cloud.context.scope.refresh.RefreshScope.class));
assertEquals("foo", this.context.getBean("foo")); assertEquals("foo", this.context.getBean("foo"));
...@@ -56,7 +57,7 @@ public class ConditionalOnRefreshScopeTests { ...@@ -56,7 +57,7 @@ public class ConditionalOnRefreshScopeTests {
@Test @Test
public void refreshScopeNotIncluded() { public void refreshScopeNotIncluded() {
this.context = new SpringApplicationBuilder(Beans.class).web(false) this.context = new SpringApplicationBuilder(Beans.class).web(false)
.showBanner(false).run(); .bannerMode(Mode.OFF).run();
assertFalse(this.context.containsBean("foo")); assertFalse(this.context.containsBean("foo"));
} }
......
...@@ -18,8 +18,10 @@ package org.springframework.cloud.netflix.eureka; ...@@ -18,8 +18,10 @@ package org.springframework.cloud.netflix.eureka;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -40,25 +42,39 @@ public class EurekaClientAutoConfigurationTests { ...@@ -40,25 +42,39 @@ public class EurekaClientAutoConfigurationTests {
} }
} }
private void setupContext() { private void setupContext(Class<?>... config) {
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.context.register(PropertyPlaceholderAutoConfiguration.class);
TestConfiguration.class); for (Class<?> value : config) {
this.context.register(value);
}
this.context.register(TestConfiguration.class);
this.context.refresh(); this.context.refresh();
} }
@Test @Test
public void nonSecurePort2() { public void nonSecurePortPeriods() {
testNonSecurePort("server.port"); testNonSecurePort("server.port");
} }
@Test @Test
public void nonSecurePort3() { public void nonSecurePortUnderscores() {
testNonSecurePort("SERVER_PORT"); testNonSecurePort("SERVER_PORT");
} }
@Test @Test
public void nonSecurePort4() { public void nonSecurePort() {
testNonSecurePort("PORT"); testNonSecurePort("PORT");
assertEquals("eurekaClient",
this.context.getBeanDefinition("eurekaClient").getFactoryMethodName());
}
@Test
public void refreshScopedBeans() {
setupContext(RefreshAutoConfiguration.class);
assertEquals(ScopedProxyFactoryBean.class.getName(),
this.context.getBeanDefinition("eurekaClient").getBeanClassName());
assertEquals(ScopedProxyFactoryBean.class.getName(), this.context
.getBeanDefinition("eurekaApplicationInfoManager").getBeanClassName());
} }
private void testNonSecurePort(String propName) { private void testNonSecurePort(String propName) {
......
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