Browse Source

Use injected ConfigClientProperties to set defaults

pull/6/head
Dave Syer 10 years ago
parent
commit
c4a53a5233
  1. 18
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceBootstrapConfiguration.java
  2. 10
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceBootstrapConfigurationTests.java

18
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceBootstrapConfiguration.java

@ -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;
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;
@ -48,23 +51,28 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements
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);

10
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceBootstrapConfigurationTests.java

@ -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 {
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 {
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,7 +90,7 @@ 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();
} }

Loading…
Cancel
Save