From 5168937f933a3913f6cdf85c8481cdfcef2b8ee7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 14 Jan 2015 12:11:18 -0800 Subject: [PATCH] Polish --- .../CommonsClientAutoConfiguration.java | 5 +++ .../cloud/client/DefaultServiceInstance.java | 6 ++++ .../cloud/client/ServiceInstance.java | 5 ++- .../SingleImplementationImportSelector.java | 10 +++--- .../circuitbreaker/EnableCircuitBreaker.java | 1 + .../EnableCircuitBreakerImportSelector.java | 1 + .../discovery/AbstractDiscoveryLifecycle.java | 12 +++++-- .../client/discovery/DiscoveryClient.java | 5 ++- .../DiscoveryClientHealthIndicator.java | 2 ++ .../DiscoveryCompositeHealthIndicator.java | 1 + .../discovery/DiscoveryHealthIndicator.java | 4 ++- .../discovery/DiscoveryHeartbeatEvent.java | 1 + .../discovery/EnableDiscoveryClient.java | 8 ++--- .../EnableDiscoveryClientImportSelector.java | 1 + .../discovery/InstanceRegisteredEvent.java | 6 +++- .../discovery/ManagementServerPortUtils.java | 32 +++++++++---------- .../client/discovery/NoopDiscoveryClient.java | 1 - .../NoopDiscoveryClientConfiguration.java | 1 - .../loadbalancer/LoadBalancerRequest.java | 2 ++ ...ngleImplementationImportSelectorTests.java | 1 + ...iscoveryCompositeHealthIndicatorTests.java | 1 + 21 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java b/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java index 901082d2..a2da7c3c 100644 --- a/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java +++ b/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java @@ -20,6 +20,7 @@ import java.util.List; import org.springframework.boot.actuate.health.HealthAggregator; import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; @@ -32,6 +33,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; /** + * {@link EnableAutoConfiguration Auto-configuration} for Spring Cloud Commons Client. + * * @author Spencer Gibb */ @Configuration @@ -43,6 +46,7 @@ public class CommonsClientAutoConfiguration { @ConditionalOnBean(DiscoveryClient.class) @ConditionalOnExpression("${spring.cloud.discovery.enabled:true}") protected static class HealthConfiguration { + @Bean public DiscoveryClientHealthIndicator instancesHealthIndicator( DiscoveryClient discoveryClient) { @@ -54,5 +58,6 @@ public class CommonsClientAutoConfiguration { HealthAggregator aggregator, List indicators) { return new DiscoveryCompositeHealthIndicator(aggregator, indicators); } + } } diff --git a/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java b/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java index 537246f1..160324d9 100644 --- a/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java +++ b/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java @@ -19,11 +19,17 @@ package org.springframework.cloud.client; import lombok.Data; /** + * Default implementation of {@link ServiceInstance}. + * * @author Spencer Gibb */ @Data public class DefaultServiceInstance implements ServiceInstance { + private final String serviceId; + private final String host; + private final int port; + } diff --git a/src/main/java/org/springframework/cloud/client/ServiceInstance.java b/src/main/java/org/springframework/cloud/client/ServiceInstance.java index 778c78ad..80877212 100644 --- a/src/main/java/org/springframework/cloud/client/ServiceInstance.java +++ b/src/main/java/org/springframework/cloud/client/ServiceInstance.java @@ -17,9 +17,12 @@ package org.springframework.cloud.client; /** - * @author Spencer Gibb TODO: name? Server? HostAndPort? Instance? + * @author Spencer Gibb */ public interface ServiceInstance { + + // TODO: name? Server? HostAndPort? Instance? + public String getServiceId(); public String getHost(); diff --git a/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java b/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java index 98f6c0ee..6ec50eba 100644 --- a/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java +++ b/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java @@ -35,6 +35,7 @@ import org.springframework.util.Assert; /** * Selects a single configuration to load defined by the generic type T. + * * @author Spencer Gibb * @author Dave Syer */ @@ -45,6 +46,7 @@ public abstract class SingleImplementationImportSelector implements private ClassLoader beanClassLoader; private Class annotationClass; + private Environment environment; @SuppressWarnings("unchecked") @@ -72,14 +74,13 @@ public abstract class SingleImplementationImportSelector implements if (factories.size() > 1) { String factory = factories.get(0); // there should only every be one DiscoveryClient - log.warn( - "More than one implementation of @{}. Using {} out of available {}", - getSimpleName(), factory, factories); + log.warn("More than one implementation " + + "of @{}. Using {} out of available {}", getSimpleName(), factory, + factories); factories = Collections.singletonList(factory); } return factories.toArray(new String[factories.size()]); - } protected abstract boolean isEnabled(); @@ -105,4 +106,5 @@ public abstract class SingleImplementationImportSelector implements public void setBeanClassLoader(ClassLoader classLoader) { this.beanClassLoader = classLoader; } + } diff --git a/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreaker.java b/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreaker.java index 355e5f2a..d180b91f 100644 --- a/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreaker.java +++ b/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreaker.java @@ -34,4 +34,5 @@ import org.springframework.context.annotation.Import; @Inherited @Import(EnableCircuitBreakerImportSelector.class) public @interface EnableCircuitBreaker { + } diff --git a/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java b/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java index be49854c..06b76a5b 100644 --- a/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java +++ b/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java @@ -33,4 +33,5 @@ public class EnableCircuitBreakerImportSelector extends return new RelaxedPropertyResolver(getEnvironment()).getProperty( "spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE); } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java b/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java index 8df45ec3..db0f4d30 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java +++ b/src/main/java/org/springframework/cloud/client/discovery/AbstractDiscoveryLifecycle.java @@ -31,9 +31,13 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, ApplicationContextAware { protected boolean autoStartup = true; + protected boolean running; + protected int order = 0; + protected ApplicationContext context; + protected Environment environment; @Override @@ -84,12 +88,13 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, protected abstract boolean isEnabled(); protected String getManagementServiceId() { - return this.context.getId() + ":management"; // TODO: configurable management - // suffix + return this.context.getId() + ":management"; + // TODO: configurable management suffix } protected String getManagementServiceName() { - return getAppName() + ":management"; // TODO: configurable management suffix + return getAppName() + ":management"; + // TODO: configurable management suffix } protected Integer getManagementPort() { @@ -130,4 +135,5 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, public int getPhase() { return 0; } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java index 5e7c96cd..a5ea9756 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java +++ b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClient.java @@ -23,8 +23,10 @@ import org.springframework.cloud.client.ServiceInstance; /** * @author Spencer Gibb */ -// TODO: merge with LoadBalancerClient? public interface DiscoveryClient { + + // TODO: merge with LoadBalancerClient? + public String description(); /** @@ -45,4 +47,5 @@ public interface DiscoveryClient { * @return all known service id's */ public List getServices(); + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClientHealthIndicator.java b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClientHealthIndicator.java index 7d2bb2f3..1ab6d367 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClientHealthIndicator.java +++ b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryClientHealthIndicator.java @@ -31,6 +31,7 @@ import org.springframework.core.Ordered; public class DiscoveryClientHealthIndicator implements DiscoveryHealthIndicator, Ordered { private int order = Ordered.HIGHEST_PRECEDENCE; + private DiscoveryClient discoveryClient; public DiscoveryClientHealthIndicator(DiscoveryClient discoveryClient) { @@ -65,4 +66,5 @@ public class DiscoveryClientHealthIndicator implements DiscoveryHealthIndicator, public void setOrder(int order) { this.order = order; } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicator.java b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicator.java index f4f09984..8ca2c899 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicator.java +++ b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicator.java @@ -50,4 +50,5 @@ public class DiscoveryCompositeHealthIndicator extends CompositeHealthIndicator return this.delegate.health(); } } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHealthIndicator.java b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHealthIndicator.java index 0bc96ef2..9a93dcbc 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHealthIndicator.java +++ b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHealthIndicator.java @@ -22,10 +22,12 @@ import org.springframework.boot.actuate.health.Health; * @author Spencer Gibb */ public interface DiscoveryHealthIndicator { - public String getName(); + + String getName(); /** * @return an indication of health */ Health health(); + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHeartbeatEvent.java b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHeartbeatEvent.java index 3865f5d3..caa58a47 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHeartbeatEvent.java +++ b/src/main/java/org/springframework/cloud/client/discovery/DiscoveryHeartbeatEvent.java @@ -34,4 +34,5 @@ public class DiscoveryHeartbeatEvent extends ApplicationEvent { public Object getValue() { return this.value; } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java index b61631ad..2c503a2a 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java +++ b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java @@ -16,10 +16,6 @@ package org.springframework.cloud.client.discovery; -/** - * @author Spencer Gibb - */ - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; @@ -29,10 +25,14 @@ import java.lang.annotation.Target; import org.springframework.context.annotation.Import; +/** + * @author Spencer Gibb + */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(EnableDiscoveryClientImportSelector.class) public @interface EnableDiscoveryClient { + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java index 48873973..e23e96ef 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java +++ b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java @@ -33,4 +33,5 @@ public class EnableDiscoveryClientImportSelector extends return new RelaxedPropertyResolver(getEnvironment()).getProperty( "spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE); } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/InstanceRegisteredEvent.java b/src/main/java/org/springframework/cloud/client/discovery/InstanceRegisteredEvent.java index 61466820..6bf3ed3e 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/InstanceRegisteredEvent.java +++ b/src/main/java/org/springframework/cloud/client/discovery/InstanceRegisteredEvent.java @@ -20,14 +20,17 @@ import org.springframework.context.ApplicationEvent; /** * Event to be published after the local service instance registers itself with a - * discovery service + * discovery service. + * * @author Spencer Gibb */ @SuppressWarnings("serial") public class InstanceRegisteredEvent extends ApplicationEvent { + private T config; /** + * Create a new {@link InstanceRegisteredEvent} instance. * @param source the component that published the event (never {@code null}) * @param config the configuration of the instance */ @@ -39,4 +42,5 @@ public class InstanceRegisteredEvent extends ApplicationEvent { public T getConfig() { return this.config; } + } diff --git a/src/main/java/org/springframework/cloud/client/discovery/ManagementServerPortUtils.java b/src/main/java/org/springframework/cloud/client/discovery/ManagementServerPortUtils.java index 5834ac0c..e0a3564e 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/ManagementServerPortUtils.java +++ b/src/main/java/org/springframework/cloud/client/discovery/ManagementServerPortUtils.java @@ -27,13 +27,28 @@ import org.springframework.web.context.WebApplicationContext; */ public class ManagementServerPortUtils { + public static ManagementServerPort get(BeanFactory beanFactory) { + return ManagementServerPort.get(beanFactory); + } + + public static boolean isDifferent(BeanFactory beanFactory) { + return get(beanFactory) == ManagementServerPort.DIFFERENT; + } + + public static boolean isDisabled(BeanFactory beanFactory) { + return get(beanFactory) == ManagementServerPort.DISABLE; + } + + public static boolean isSame(BeanFactory beanFactory) { + return get(beanFactory) == ManagementServerPort.SAME; + } + // TODO: copied from EndpointWebMvcAutoConfiguration.ManagementServerPort public static enum ManagementServerPort { DISABLE, SAME, DIFFERENT; public static ManagementServerPort get(BeanFactory beanFactory) { - ServerProperties serverProperties; try { serverProperties = beanFactory.getBean(ServerProperties.class); @@ -66,19 +81,4 @@ public class ManagementServerPortUtils { } }; - public static ManagementServerPort get(BeanFactory beanFactory) { - return ManagementServerPort.get(beanFactory); - } - - public static boolean isDifferent(BeanFactory beanFactory) { - return get(beanFactory) == ManagementServerPort.DIFFERENT; - } - - public static boolean isDisabled(BeanFactory beanFactory) { - return get(beanFactory) == ManagementServerPort.DISABLE; - } - - public static boolean isSame(BeanFactory beanFactory) { - return get(beanFactory) == ManagementServerPort.SAME; - } } diff --git a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java index c6dabf1f..8e59d434 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java +++ b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java @@ -23,7 +23,6 @@ import org.springframework.cloud.client.ServiceInstance; /** * @author Dave Syer - * */ public class NoopDiscoveryClient implements DiscoveryClient { diff --git a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java index 8837e2e1..b381cb77 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java +++ b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java @@ -40,7 +40,6 @@ import org.springframework.core.env.Environment; /** * @author Dave Syer - * */ @Configuration @EnableConfigurationProperties diff --git a/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRequest.java b/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRequest.java index 02ebc496..4d0b4c31 100644 --- a/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRequest.java +++ b/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRequest.java @@ -22,5 +22,7 @@ import org.springframework.cloud.client.ServiceInstance; * @author Spencer Gibb */ public interface LoadBalancerRequest { + public T apply(ServiceInstance instance) throws Exception; + } diff --git a/src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java b/src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java index 48e0cea3..0a616491 100644 --- a/src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java +++ b/src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java @@ -37,6 +37,7 @@ public class SingleImplementationImportSelectorTests { public static class MyAnnotationImportSelector extends SingleImplementationImportSelector { + @Override protected boolean isEnabled() { return true; diff --git a/src/test/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicatorTests.java b/src/test/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicatorTests.java index f852b647..d84d2cfc 100644 --- a/src/test/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicatorTests.java +++ b/src/test/java/org/springframework/cloud/client/discovery/DiscoveryCompositeHealthIndicatorTests.java @@ -89,4 +89,5 @@ public class DiscoveryCompositeHealthIndicatorTests { assertEquals("status desciption was wrong", "TestDiscoveryClient", status.getDescription()); } + }