Commit c4a53a52 by Dave Syer

Use injected ConfigClientProperties to set defaults

parent 1f2beba0
...@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration; import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration;
...@@ -27,6 +28,8 @@ import org.springframework.context.ApplicationListener; ...@@ -27,6 +28,8 @@ import org.springframework.context.ApplicationListener;
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;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient; import com.netflix.discovery.DiscoveryClient;
...@@ -46,25 +49,30 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements ...@@ -46,25 +49,30 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements
@Autowired @Autowired
private DiscoveryClient client; private DiscoveryClient client;
@Autowired @Autowired
private ConfigServicePropertySourceLocator delegate; private ConfigClientProperties config;
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(ContextRefreshedEvent event) {
try { try {
log.info("Locating configserver via discovery"); log.info("Locating configserver via discovery");
InstanceInfo server = client.getNextServerFromEureka(delegate.getDiscovery().getServiceId(), Environment environment = event.getApplicationContext().getEnvironment();
if (!(environment instanceof ConfigurableEnvironment)) {
log.info("Environment is not ConfigurableEnvironment so cannot look up configserver");
return;
}
InstanceInfo server = client.getNextServerFromEureka(config.getDiscovery().getServiceId(),
false); false);
String url = server.getHomePageUrl(); String url = server.getHomePageUrl();
if (server.getMetadata().containsKey("password")) { if (server.getMetadata().containsKey("password")) {
String user = server.getMetadata().get("user"); String user = server.getMetadata().get("user");
user = user == null ? "user" : user; user = user == null ? "user" : user;
delegate.setUsername(user); config.setUsername(user);
String password = server.getMetadata().get("password"); String password = server.getMetadata().get("password");
delegate.setPassword(password); config.setPassword(password);
} }
delegate.setUri(url); config.setUri(url);
} }
catch (Exception e) { catch (Exception e) {
log.warn("Could not locate configserver via discovery", e); log.warn("Could not locate configserver via discovery", e);
......
...@@ -22,7 +22,7 @@ import org.junit.Test; ...@@ -22,7 +22,7 @@ import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
...@@ -67,8 +67,7 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests { ...@@ -67,8 +67,7 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
1, 1,
context.getBeanNamesForType(DiscoveryClientConfigServiceBootstrapConfiguration.class).length); context.getBeanNamesForType(DiscoveryClientConfigServiceBootstrapConfiguration.class).length);
Mockito.verify(client).getNextServerFromEureka("CONFIGSERVER", false); Mockito.verify(client).getNextServerFromEureka("CONFIGSERVER", false);
ConfigServicePropertySourceLocator locator = context ConfigClientProperties locator = context.getBean(ConfigClientProperties.class);
.getBean(ConfigServicePropertySourceLocator.class);
assertEquals("http://foo:7001/", locator.getUri()); assertEquals("http://foo:7001/", locator.getUri());
} }
...@@ -78,8 +77,7 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests { ...@@ -78,8 +77,7 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
Mockito.when(client.getNextServerFromEureka("CONFIGSERVER", false)).thenReturn( Mockito.when(client.getNextServerFromEureka("CONFIGSERVER", false)).thenReturn(
info); info);
setup("spring.cloud.config.discovery.enabled=true"); setup("spring.cloud.config.discovery.enabled=true");
ConfigServicePropertySourceLocator locator = context ConfigClientProperties locator = context.getBean(ConfigClientProperties.class);
.getBean(ConfigServicePropertySourceLocator.class);
assertEquals("http://foo:7001/", locator.getUri()); assertEquals("http://foo:7001/", locator.getUri());
assertEquals("bar", locator.getPassword()); assertEquals("bar", locator.getPassword());
assertEquals("user", locator.getUsername()); assertEquals("user", locator.getUsername());
...@@ -92,8 +90,8 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests { ...@@ -92,8 +90,8 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
client); client);
context.register(PropertyPlaceholderAutoConfiguration.class, context.register(PropertyPlaceholderAutoConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class,
ConfigServicePropertySourceLocator.class); ConfigClientProperties.class);
context.refresh(); context.refresh();
} }
} }
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