Browse Source

only create Discovery*HealthIndicator beans if DiscoveryClient is a bean and spring.cloud.discovery.enable = true.

Allow discovery and circuit breaker to be turned off by properties.

fixes gh-2
pull/5/head
Spencer Gibb 10 years ago
parent
commit
e48dff74e5
  1. 24
      src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java
  2. 15
      src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java
  3. 5
      src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java
  4. 14
      src/main/java/org/springframework/cloud/client/discovery/DiscoveryClientHealthIndicator.java
  5. 5
      src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java
  6. 7
      src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java

24
src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java

@ -2,7 +2,10 @@ package org.springframework.cloud.client; @@ -2,7 +2,10 @@ package org.springframework.cloud.client;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.DiscoveryClientHealthIndicator;
import org.springframework.cloud.client.discovery.DiscoveryCompositeHealthIndicator;
import org.springframework.cloud.client.discovery.DiscoveryHealthIndicator;
@ -20,13 +23,18 @@ import java.util.List; @@ -20,13 +23,18 @@ import java.util.List;
@Order(0)
public class CommonsClientAutoConfiguration {
@Bean
public DiscoveryClientHealthIndicator instancesHealthIndicator() {
return new DiscoveryClientHealthIndicator();
}
@Configuration
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnExpression("${spring.cloud.discovery.enabled:true}")
protected static class HealthConfiguration {
@Bean
public DiscoveryClientHealthIndicator instancesHealthIndicator(DiscoveryClient discoveryClient) {
return new DiscoveryClientHealthIndicator(discoveryClient);
}
@Bean
public DiscoveryCompositeHealthIndicator discoveryHealthIndicator(HealthAggregator aggregator, List<DiscoveryHealthIndicator> indicators) {
return new DiscoveryCompositeHealthIndicator(aggregator, indicators);
}
@Bean
public DiscoveryCompositeHealthIndicator discoveryHealthIndicator(HealthAggregator aggregator, List<DiscoveryHealthIndicator> indicators) {
return new DiscoveryCompositeHealthIndicator(aggregator, indicators);
}
}
}

15
src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java

@ -2,9 +2,11 @@ package org.springframework.cloud.client; @@ -2,9 +2,11 @@ package org.springframework.cloud.client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
@ -19,11 +21,12 @@ import java.util.List; @@ -19,11 +21,12 @@ import java.util.List;
* @author Spencer Gibb
*/
@Slf4j
public abstract class SingleImplementationImportSelector<T> implements DeferredImportSelector, BeanClassLoaderAware {
public abstract class SingleImplementationImportSelector<T> implements DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
protected ClassLoader beanClassLoader;
protected Class<T> annotationClass;
protected Environment environment;
@SuppressWarnings("unchecked")
protected SingleImplementationImportSelector() {
@ -32,6 +35,9 @@ public abstract class SingleImplementationImportSelector<T> implements DeferredI @@ -32,6 +35,9 @@ public abstract class SingleImplementationImportSelector<T> implements DeferredI
@Override
public String[] selectImports(AnnotationMetadata metadata) {
if (!isEnabled()) {
return new String[0];
}
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
.getAnnotationAttributes(annotationClass.getName(),
true));
@ -55,10 +61,17 @@ public abstract class SingleImplementationImportSelector<T> implements DeferredI @@ -55,10 +61,17 @@ public abstract class SingleImplementationImportSelector<T> implements DeferredI
}
protected abstract boolean isEnabled();
protected String getSimpleName() {
return annotationClass.getSimpleName();
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;

5
src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package org.springframework.cloud.client.circuitbreaker;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.cloud.client.SingleImplementationImportSelector;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@ -10,4 +11,8 @@ import org.springframework.core.annotation.Order; @@ -10,4 +11,8 @@ import org.springframework.core.annotation.Order;
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableCircuitBreakerImportSelector extends SingleImplementationImportSelector<EnableCircuitBreaker> {
@Override
protected boolean isEnabled() {
return new RelaxedPropertyResolver(environment).getProperty("spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE);
}
}

14
src/main/java/org/springframework/cloud/client/discovery/DiscoveryClientHealthIndicator.java

@ -20,18 +20,18 @@ public class DiscoveryClientHealthIndicator implements ApplicationContextAware, @@ -20,18 +20,18 @@ public class DiscoveryClientHealthIndicator implements ApplicationContextAware,
private ApplicationContext context;
private int order = Ordered.HIGHEST_PRECEDENCE;
private DiscoveryClient discoveryClient;
public DiscoveryClientHealthIndicator(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@Override
public Health health() {
Health.Builder builder = new Health.Builder();
try {
DiscoveryClient client = context.getBean(DiscoveryClient.class);
if (client == null) {
builder.unknown().withDetail("warning", "No DiscoveryClient found");
return builder.build();
}
List<String> services = client.getServices();
builder.status(new Status("UP", client.description()))
List<String> services = discoveryClient.getServices();
builder.status(new Status("UP", discoveryClient.description()))
.withDetail("services", services);
} catch (Exception e) {
log.error("Error", e);

5
src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package org.springframework.cloud.client.discovery;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.cloud.client.SingleImplementationImportSelector;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@ -10,4 +11,8 @@ import org.springframework.core.annotation.Order; @@ -10,4 +11,8 @@ import org.springframework.core.annotation.Order;
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableDiscoveryClientImportSelector extends SingleImplementationImportSelector<EnableDiscoveryClient> {
@Override
protected boolean isEnabled() {
return new RelaxedPropertyResolver(environment).getProperty("spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
}
}

7
src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java

@ -16,5 +16,10 @@ public class SingleImplementationImportSelectorTests { @@ -16,5 +16,10 @@ public class SingleImplementationImportSelectorTests {
}
public static @interface MyAnnotation {}
public static class MyAnnotationImportSelector extends SingleImplementationImportSelector<MyAnnotation> { }
public static class MyAnnotationImportSelector extends SingleImplementationImportSelector<MyAnnotation> {
@Override
protected boolean isEnabled() {
return true;
}
}
}

Loading…
Cancel
Save