Browse Source
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-65pull/6/head
7 changed files with 161 additions and 3 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* 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); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/* |
||||
* 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(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue