Commit d2988b21 by Dave Syer

Add metadata to Eureka entry for config server

If the prefix is set we also set configPath=<prefix> in the instance metadata, so config clients can pick it up and add it to the home page URL. Fixes gh-64, fixes gh-65
parent ed053e9b
...@@ -43,6 +43,11 @@ ...@@ -43,6 +43,11 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-core</artifactId> <artifactId>spring-cloud-netflix-core</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version> <version>1.0.0.BUILD-SNAPSHOT</version>
</dependency> </dependency>
......
...@@ -30,6 +30,11 @@ ...@@ -30,6 +30,11 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId> <groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId> <artifactId>archaius-core</artifactId>
<optional>true</optional> <optional>true</optional>
......
...@@ -35,6 +35,9 @@ import com.netflix.appinfo.InstanceInfo; ...@@ -35,6 +35,9 @@ import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient; import com.netflix.discovery.DiscoveryClient;
/** /**
* Bootstrap configuration for a config client that wants to lookup the config server via
* discovery.
*
* @author Dave Syer * @author Dave Syer
* *
*/ */
...@@ -62,8 +65,8 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements ...@@ -62,8 +65,8 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements
log.info("Environment is not ConfigurableEnvironment so cannot look up configserver"); log.info("Environment is not ConfigurableEnvironment so cannot look up configserver");
return; return;
} }
InstanceInfo server = client.getNextServerFromEureka(config.getDiscovery().getServiceId(), InstanceInfo server = client.getNextServerFromEureka(config.getDiscovery()
false); .getServiceId(), 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");
...@@ -72,6 +75,13 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements ...@@ -72,6 +75,13 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements
String password = server.getMetadata().get("password"); String password = server.getMetadata().get("password");
config.setPassword(password); config.setPassword(password);
} }
if (server.getMetadata().containsKey("configPath")) {
String path = server.getMetadata().get("configPath");
if (url.endsWith("/") && path.startsWith("/")) {
url = url.substring(0, url.length()-1);
}
url = url + path;
}
config.setUri(url); config.setUri(url);
} }
catch (Exception e) { catch (Exception e) {
......
/*
* Copyright 2013-2014 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.config;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.config.server.ConfigServerProperties;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import com.netflix.discovery.DiscoveryClient;
/**
* Extra configuration for config server if it happens to be a Eureka instance.
* @author Dave Syer
*
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnClass({EurekaInstanceConfigBean.class, DiscoveryClient.class, ConfigServerProperties.class})
public class EurekaClientConfigServerAutoConfiguration {
@Autowired(required=false)
private EurekaInstanceConfigBean instance;
@Autowired(required=false)
private ConfigServerProperties server;
@PostConstruct
public void init() {
if (instance==null || server==null) {
return;
}
String prefix = server.getPrefix();
if (StringUtils.hasText(prefix)) {
instance.getMetadataMap().put("configPath", prefix);
}
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration,\ org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration,\
org.springframework.cloud.netflix.config.EurekaClientConfigServerAutoConfiguration,\
org.springframework.cloud.netflix.feign.FeignAutoConfiguration,\ org.springframework.cloud.netflix.feign.FeignAutoConfiguration,\
org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\ org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration,\ org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration,\
......
...@@ -83,6 +83,16 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests { ...@@ -83,6 +83,16 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
assertEquals("user", locator.getUsername()); assertEquals("user", locator.getUsername());
} }
@Test
public void setsPath() throws Exception {
info.getMetadata().put("configPath", "/bar");
Mockito.when(client.getNextServerFromEureka("CONFIGSERVER", false)).thenReturn(
info);
setup("spring.cloud.config.discovery.enabled=true");
ConfigClientProperties locator = context.getBean(ConfigClientProperties.class);
assertEquals("http://foo:7001/bar", locator.getUri());
}
private void setup(String... env) { private void setup(String... env) {
context = new AnnotationConfigApplicationContext(); context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, env); EnvironmentTestUtils.addEnvironment(context, env);
......
/*
* Copyright 2013-2014 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.config;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.ConfigServerProperties;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.netflix.appinfo.EurekaInstanceConfig;
/**
* @author Dave Syer
*
*/
public class EurekaClientConfigServerAutoConfigurationTests {
private ConfigurableApplicationContext context;
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test
public void offByDefault() throws Exception {
context = new AnnotationConfigApplicationContext(
EurekaClientConfigServerAutoConfiguration.class);
assertEquals(0,
context.getBeanNamesForType(EurekaInstanceConfigBean.class).length);
}
@Test
public void onWhenRequested() throws Exception {
setup("spring.cloud.config.server.prefix=/config");
assertEquals(1, context.getBeanNamesForType(EurekaInstanceConfig.class).length);
EurekaInstanceConfig instance = context.getBean(EurekaInstanceConfig.class);
assertEquals("/config", instance.getMetadataMap().get("configPath"));
}
private void setup(String... env) {
context = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class,
EurekaClientConfigServerAutoConfiguration.class,
ConfigServerProperties.class, EurekaInstanceConfigBean.class).web(false)
.properties(env).run();
}
}
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