Polish
This commit is contained in:
@@ -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<DiscoveryHealthIndicator> indicators) {
|
||||
return new DiscoveryCompositeHealthIndicator(aggregator, indicators);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<T> implements
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
private Class<T> annotationClass;
|
||||
|
||||
private Environment environment;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -72,14 +74,13 @@ public abstract class SingleImplementationImportSelector<T> 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<T> implements
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,4 +34,5 @@ import org.springframework.context.annotation.Import;
|
||||
@Inherited
|
||||
@Import(EnableCircuitBreakerImportSelector.class)
|
||||
public @interface EnableCircuitBreaker {
|
||||
|
||||
}
|
||||
|
||||
@@ -33,4 +33,5 @@ public class EnableCircuitBreakerImportSelector extends
|
||||
return new RelaxedPropertyResolver(getEnvironment()).getProperty(
|
||||
"spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String> getServices();
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,4 +50,5 @@ public class DiscoveryCompositeHealthIndicator extends CompositeHealthIndicator
|
||||
return this.delegate.health();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
@@ -34,4 +34,5 @@ public class DiscoveryHeartbeatEvent extends ApplicationEvent {
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
@@ -33,4 +33,5 @@ public class EnableDiscoveryClientImportSelector extends
|
||||
return new RelaxedPropertyResolver(getEnvironment()).getProperty(
|
||||
"spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<T> 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<T> extends ApplicationEvent {
|
||||
public T getConfig() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class NoopDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
|
||||
@@ -40,7 +40,6 @@ import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
|
||||
@@ -22,5 +22,7 @@ import org.springframework.cloud.client.ServiceInstance;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public interface LoadBalancerRequest<T> {
|
||||
|
||||
public T apply(ServiceInstance instance) throws Exception;
|
||||
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ public class SingleImplementationImportSelectorTests {
|
||||
|
||||
public static class MyAnnotationImportSelector extends
|
||||
SingleImplementationImportSelector<MyAnnotation> {
|
||||
|
||||
@Override
|
||||
protected boolean isEnabled() {
|
||||
return true;
|
||||
|
||||
@@ -89,4 +89,5 @@ public class DiscoveryCompositeHealthIndicatorTests {
|
||||
assertEquals("status desciption was wrong", "TestDiscoveryClient",
|
||||
status.getDescription());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user