uses a ImportSelector that imports the NoopDiscoveryClientConfiguration if @EnableDiscoveryClient is not used.

fixes gh-6
This commit is contained in:
Spencer Gibb
2015-01-16 13:28:38 -07:00
parent c6a9104f5d
commit d96115c1b8
4 changed files with 53 additions and 5 deletions

View File

@@ -71,6 +71,11 @@ public abstract class SingleImplementationImportSelector<T> implements
SpringFactoriesLoader.loadFactoryNames(this.annotationClass,
this.beanClassLoader)));
if (factories.isEmpty()) {
throw new IllegalStateException("Annotation @" + getSimpleName() +
" found, but there are no implementations. Did you forget to include a starter?");
}
if (factories.size() > 1) {
String factory = factories.get(0);
// there should only every be one DiscoveryClient

View File

@@ -22,9 +22,8 @@ import java.net.UnknownHostException;
import javax.annotation.PostConstruct;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
@@ -42,8 +41,6 @@ import org.springframework.core.env.Environment;
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnMissingClass(name = "com.netflix.discovery.EurekaClientConfig")
@ConditionalOnProperty(value = "eureka.client.enabled", havingValue = "false")
@CommonsLog
public class NoopDiscoveryClientConfiguration implements
ApplicationListener<ContextRefreshedEvent> {

View File

@@ -0,0 +1,46 @@
/*
* 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.client.discovery;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
/**
* Import Selector that only returns a NoopDiscoveryClientConfiguration if the
* @EnableDiscoveryClient annotation has NOT been used.
* @author Spencer Gibb
*/
@CommonsLog
public class NoopDiscoveryClientImportSelector implements
DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata metadata) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
.getAnnotationAttributes(EnableDiscoveryClient.class.getName(), true));
if (attributes == null) {
// @EnableDiscoveryClient has not been used, so configure the noop client
return new String[] { NoopDiscoveryClientConfiguration.class.getName() };
}
return new String[]{};
}
}

View File

@@ -1,4 +1,4 @@
# Bootstrap Configuration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.client.CommonsClientAutoConfiguration,\
org.springframework.cloud.client.discovery.NoopDiscoveryClientConfiguration
org.springframework.cloud.client.discovery.NoopDiscoveryClientImportSelector