Added Javadocs
This commit is contained in:
@@ -16,15 +16,17 @@
|
||||
|
||||
package org.springframework.cloud.zookeeper.discovery;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
|
||||
/**
|
||||
* Wrapper annotation to enable Ribbon for Zookeeper
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
|
||||
@@ -25,7 +25,11 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
|
||||
* that sets up Ribbon for Zookeeper.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@@ -34,6 +38,4 @@ import org.springframework.context.annotation.Configuration;
|
||||
@AutoConfigureAfter(RibbonAutoConfiguration.class)
|
||||
@RibbonClients(defaultConfiguration = ZookeeperRibbonClientConfiguration.class)
|
||||
public class RibbonZookeeperAutoConfiguration {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
@@ -33,13 +34,16 @@ import org.springframework.util.ReflectionUtils;
|
||||
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
|
||||
|
||||
/**
|
||||
* Zookeeper version of {@link DiscoveryClient}. Capable of resolving aliases from
|
||||
* {@link ZookeeperDependencies} to service names in Zookeeper.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(ZookeeperDiscoveryClient.class);
|
||||
private static final Log log = LogFactory.getLog(ZookeeperDiscoveryClient.class);
|
||||
|
||||
private ZookeeperServiceDiscovery serviceDiscovery;
|
||||
|
||||
@@ -64,11 +68,9 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient {
|
||||
private static org.springframework.cloud.client.ServiceInstance createServiceInstance(String serviceId, ServiceInstance<ZookeeperInstance> serviceInstance) {
|
||||
boolean secure = serviceInstance.getSslPort() != null;
|
||||
Integer port = serviceInstance.getPort();
|
||||
|
||||
if (secure) {
|
||||
port = serviceInstance.getSslPort();
|
||||
}
|
||||
|
||||
Map<String, String> metadata;
|
||||
if (serviceInstance.getPayload() != null) {
|
||||
metadata = serviceInstance.getPayload().getMetadata();
|
||||
@@ -85,13 +87,10 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient {
|
||||
String serviceIdToQuery = getServiceIdToQuery(serviceId);
|
||||
Collection<ServiceInstance<ZookeeperInstance>> zkInstances = this.serviceDiscovery
|
||||
.getServiceDiscovery().queryForInstances(serviceIdToQuery);
|
||||
|
||||
ArrayList<org.springframework.cloud.client.ServiceInstance> instances = new ArrayList<>();
|
||||
|
||||
List<org.springframework.cloud.client.ServiceInstance> instances = new ArrayList<>();
|
||||
for (ServiceInstance<ZookeeperInstance> instance : zkInstances) {
|
||||
instances.add(createServiceInstance(serviceIdToQuery, instance));
|
||||
}
|
||||
|
||||
return instances;
|
||||
} catch (Exception exception) {
|
||||
ReflectionUtils.rethrowRuntimeException(exception);
|
||||
|
||||
@@ -31,7 +31,11 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.cloud.client.discovery.DiscoveryClient} configuration
|
||||
* for Zookeeper.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(value = "spring.cloud.zookeeper.discovery.enabled", matchIfMissing = true)
|
||||
|
||||
@@ -17,18 +17,22 @@
|
||||
package org.springframework.cloud.zookeeper.discovery;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.actuate.health.HealthIndicator} that presents
|
||||
* the status of all instances registered in Zookeeper.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperDiscoveryHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(ZookeeperDiscoveryHealthIndicator.class);
|
||||
private static final Log log = LogFactory.getLog(ZookeeperDiscoveryHealthIndicator.class);
|
||||
|
||||
private ZookeeperServiceDiscovery serviceDiscovery;
|
||||
private ZookeeperDependencies zookeeperDependencies;
|
||||
|
||||
@@ -22,16 +22,29 @@ import java.util.Map;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Properties related to Zookeeper's Service Discovery.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.zookeeper.discovery")
|
||||
public class ZookeeperDiscoveryProperties {
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* Root Zookeeper folder in which all instances are registered
|
||||
*/
|
||||
private String root = "/services";
|
||||
|
||||
/**
|
||||
* The URI specification to resolve during service registration in Zookeeper
|
||||
*/
|
||||
private String uriSpec = "{scheme}://{address}:{port}";
|
||||
|
||||
/**
|
||||
* Predefined host with which a service can register itself in Zookeeper. Corresponds
|
||||
* to the {code address} from the URI spec.
|
||||
*/
|
||||
private String instanceHost;
|
||||
|
||||
/**
|
||||
@@ -82,13 +95,11 @@ public class ZookeeperDiscoveryProperties {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("ZookeeperDiscoveryProperties{");
|
||||
sb.append("enabled=").append(this.enabled);
|
||||
sb.append(", root='").append(this.root).append('\'');
|
||||
sb.append(", uriSpec='").append(this.uriSpec).append('\'');
|
||||
sb.append(", instanceHost='").append(this.instanceHost).append('\'');
|
||||
sb.append(", metadata=").append(this.metadata);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
return "ZookeeperDiscoveryProperties{" + "enabled=" + this.enabled +
|
||||
", root='" + this.root + '\'' +
|
||||
", uriSpec='" + this.uriSpec + '\'' +
|
||||
", instanceHost='" + this.instanceHost + '\'' +
|
||||
", metadata=" + this.metadata +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,10 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents the default payload of a registered service in Zookeeper.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperInstance {
|
||||
private String id;
|
||||
@@ -63,11 +66,9 @@ public class ZookeeperInstance {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("ZookeeperInstance{");
|
||||
sb.append("id='").append(this.id).append('\'');
|
||||
sb.append(", name='").append(this.name).append('\'');
|
||||
sb.append(", metadata=").append(this.metadata);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
return "ZookeeperInstance{" + "id='" + this.id + '\'' +
|
||||
", name='" + this.name + '\'' +
|
||||
", metadata=" + this.metadata +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,11 @@ import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Zookeeper {@link org.springframework.cloud.client.discovery.DiscoveryLifecycle}
|
||||
* that uses {@link ZookeeperServiceDiscovery} to register and de-register instances.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperLifecycle extends AbstractDiscoveryLifecycle {
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.netflix.loadbalancer.PingUrl;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -45,16 +46,16 @@ import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity
|
||||
/**
|
||||
* Preprocessor that configures defaults for zookeeper-discovered ribbon clients. Such as:
|
||||
* <code>@zone</code>, NIWSServerListClassName, DeploymentContextBasedVipAddresses,
|
||||
* NFLoadBalancerRuleClassName, NIWSServerListFilterClassName and more
|
||||
* NFLoadBalancerRuleClassName, NIWSServerListFilterClassName and more.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @author Dave Syer
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
public class ZookeeperRibbonClientConfiguration {
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(ZookeeperRibbonClientConfiguration.class);
|
||||
private static final Log log = LogFactory.getLog(ZookeeperRibbonClientConfiguration.class);
|
||||
|
||||
protected static final String VALUE_NOT_SET = "__not__set__";
|
||||
protected static final String DEFAULT_NAMESPACE = "ribbon";
|
||||
|
||||
@@ -21,7 +21,10 @@ import com.netflix.loadbalancer.Server;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
|
||||
/**
|
||||
* A Zookeeper version of a {@link Server Ribbon Server}
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperServer extends Server {
|
||||
|
||||
|
||||
@@ -32,8 +32,13 @@ import org.springframework.util.StringUtils;
|
||||
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
|
||||
|
||||
/**
|
||||
* Zookeeper version of {@link AbstractServerList} that returns the list of
|
||||
* servers on which instances are ran. The implementation is capable of resolving
|
||||
* the servers from {@link ZookeeperDependencies}.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperServerList extends AbstractServerList<ZookeeperServer> {
|
||||
|
||||
@@ -80,7 +85,6 @@ public class ZookeeperServerList extends AbstractServerList<ZookeeperServer> {
|
||||
for (ServiceInstance<ZookeeperInstance> instance : instances) {
|
||||
servers.add(new ZookeeperServer(instance));
|
||||
}
|
||||
|
||||
return servers;
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -34,7 +34,11 @@ import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Service discovery for Zookeeper that sets up {@link ServiceDiscovery}
|
||||
* and {@link ServiceInstance}.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperServiceDiscovery implements ApplicationContextAware {
|
||||
|
||||
@@ -104,6 +108,10 @@ public class ZookeeperServiceDiscovery implements ApplicationContextAware {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One can override this method to provide custom way of registering a service
|
||||
* instance (e.g. when no payload is required).
|
||||
*/
|
||||
protected void configureServiceInstance(AtomicReference<ServiceInstance<ZookeeperInstance>> serviceInstance,
|
||||
String appName,
|
||||
ApplicationContext context,
|
||||
@@ -124,7 +132,9 @@ public class ZookeeperServiceDiscovery implements ApplicationContextAware {
|
||||
}
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
/**
|
||||
* One can override this method to provide custom way of registering {@link ServiceDiscovery}
|
||||
*/
|
||||
protected void configureServiceDiscovery(AtomicReference<ServiceDiscovery<ZookeeperInstance>> serviceDiscovery,
|
||||
CuratorFramework curator, ZookeeperDiscoveryProperties properties,
|
||||
InstanceSerializer<ZookeeperInstance> instanceSerializer,
|
||||
|
||||
@@ -6,13 +6,20 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies;
|
||||
|
||||
/**
|
||||
* An {@link Iterable} representing registered Zookeeper instances. If using
|
||||
* {@link ZookeeperDependencies} it will return a list of registered Zookeeper instances
|
||||
* corresponding to the ones defined in the dependencies.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperServiceInstances implements Iterable<ServiceInstance<ZookeeperInstance>> {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(ZookeeperServiceInstances.class);
|
||||
private static final Log log = LogFactory.getLog(ZookeeperServiceInstances.class);
|
||||
|
||||
private final ZookeeperServiceDiscovery serviceDiscovery;
|
||||
private final ZookeeperDependencies zookeeperDependencies;
|
||||
|
||||
@@ -31,7 +31,11 @@ import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* A {@link TreeCacheListener} that sends {@link HeartbeatEvent} when an
|
||||
* entry inside Zookeeper has changed.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperServiceWatch implements
|
||||
ApplicationListener<InstanceRegisteredEvent<?>>, TreeCacheListener,
|
||||
|
||||
@@ -16,17 +16,18 @@
|
||||
|
||||
package org.springframework.cloud.zookeeper.discovery.dependency;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* Annotation to turn off a feature if Zookeeper dependencies have NOT been passed
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@@ -16,18 +16,19 @@
|
||||
|
||||
package org.springframework.cloud.zookeeper.discovery.dependency;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* Annotation to turn on a feature if Zookeeper dependencies have been passed
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@@ -29,17 +29,18 @@ import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* LoadBalancer that delegates to other rules depending on the provided load balancing strategy
|
||||
* in the {@link ZookeeperDependency#getLoadBalancerType()}
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DependenciesBasedLoadBalancer extends DynamicServerListLoadBalancer {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(DependenciesBasedLoadBalancer.class);
|
||||
private static final Log log = LogFactory.getLog(DependenciesBasedLoadBalancer.class);
|
||||
|
||||
private final Map<String, IRule> ruleCache = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
* was turned on
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DependenciesNotPassedCondition extends DependenciesPassedCondition {
|
||||
|
||||
|
||||
@@ -15,19 +15,20 @@
|
||||
*/
|
||||
package org.springframework.cloud.zookeeper.discovery.dependency;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Condition that verifies if the Dependencies have been passed in an appropriate
|
||||
* place in the application properties.
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DependenciesPassedCondition extends SpringBootCondition {
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.util.StringUtils;
|
||||
* to /${spring.cloud.zookeeper.prefix}/${spring.application.name}
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DependencyEnvironmentPostProcessor
|
||||
implements EnvironmentPostProcessor, Ordered {
|
||||
|
||||
@@ -41,10 +41,11 @@ import feign.Request;
|
||||
import feign.Response;
|
||||
|
||||
/**
|
||||
* Configuration for ensuring that headers are set for a given dependency when
|
||||
* Feign is used.
|
||||
*
|
||||
* Configuration for ensuring that headers are set for a given dependency.
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnDependenciesPassed
|
||||
|
||||
@@ -37,9 +37,10 @@ import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Customizes RestTemplate to support passing of params from dependency
|
||||
* Customizes RestTemplate to support passing of params from dependency.
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfigureAfter(DependencyRibbonAutoConfiguration.class)
|
||||
@ConditionalOnRibbonZookeeper
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -38,7 +39,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
* Provides LoadBalancerClient that at runtime can pick proper load balancing strategy
|
||||
* basing on the Zookeeper dependencies from properties
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@AutoConfigureBefore(RibbonAutoConfiguration.class)
|
||||
@ConditionalOnRibbonZookeeper
|
||||
@@ -46,8 +48,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
@ConditionalOnDependenciesPassed
|
||||
public class DependencyRibbonAutoConfiguration {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(DependencyRibbonAutoConfiguration.class);
|
||||
private static final Log log = LogFactory.getLog(DependencyRibbonAutoConfiguration.class);
|
||||
|
||||
@Autowired ApplicationContext applicationContext;
|
||||
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
package org.springframework.cloud.zookeeper.discovery.dependency;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public enum LoadBalancerType {
|
||||
STICKY, RANDOM, ROUND_ROBIN;
|
||||
STICKY, RANDOM, ROUND_ROBIN
|
||||
}
|
||||
|
||||
@@ -26,17 +26,18 @@ import com.netflix.loadbalancer.IRule;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Load balancing rule that returns always the same instance.
|
||||
*
|
||||
* Ported from {@code org.apache.curator.x.discovery.strategies.StickyStrategy}
|
||||
*
|
||||
* author: Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class StickyRule extends AbstractLoadBalancerRule {
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(StickyRule.class);
|
||||
private static final Log log = LogFactory.getLog(StickyRule.class);
|
||||
private final IRule masterStrategy;
|
||||
private final AtomicReference<Server> ourInstance = new AtomicReference<>(null);
|
||||
private final AtomicInteger instanceNumber = new AtomicInteger(-1);
|
||||
|
||||
@@ -5,9 +5,10 @@ import java.util.Arrays;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Representation of a stubs location
|
||||
* Representation of a stubs location.
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class StubsConfiguration {
|
||||
private static final String DEFAULT_STUBS_CLASSIFIER = "stubs";
|
||||
|
||||
@@ -30,7 +30,8 @@ import org.springframework.util.StringUtils;
|
||||
/**
|
||||
* Representation of this service's dependencies in Zookeeper
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.zookeeper")
|
||||
public class ZookeeperDependencies {
|
||||
|
||||
@@ -23,12 +23,10 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Provides autoconfiguration for Zookeeper dependency set up in properties.
|
||||
*
|
||||
* @see ZookeeperDependencies
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* Provides AutoConfiguration for Zookeeper dependency set up in properties.
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
|
||||
@@ -25,8 +25,11 @@ import org.springframework.util.StringUtils;
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* Represents a particular dependency of Zookeeper instance.
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Spencer Gibb
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ZookeeperDependency {
|
||||
|
||||
|
||||
@@ -33,11 +33,12 @@ import org.springframework.util.ReflectionUtils;
|
||||
* This Dependency Watcher will verify the presence of dependencies upon startup and registers listeners
|
||||
* to changing of state of dependencies during the application's lifecycle.
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Michal Chmielarz, 4financeIT
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @see DependencyPresenceOnStartupVerifier
|
||||
* @see DependencyWatcherListener
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Michal Chmielarz, 4financeIT
|
||||
*/
|
||||
public class DefaultDependencyWatcher implements DependencyRegistrationHookProvider, ApplicationListener<InstanceRegisteredEvent<?>> {
|
||||
|
||||
|
||||
@@ -21,7 +21,8 @@ import java.io.IOException;
|
||||
* Implementations of this interface are required to register dependency registration hooks
|
||||
* on startup and their cleaning upon application context shutdown.
|
||||
*
|
||||
* @author <a href="http://www.4financeit.com>4financeIT</a>
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface DependencyRegistrationHookProvider {
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ package org.springframework.cloud.zookeeper.discovery.watcher;
|
||||
*
|
||||
* Represents a dependency's Zookeeper connection state
|
||||
*
|
||||
* @author <a href="http://www.4financeit.com>4financeIT</a>
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public enum DependencyState {
|
||||
CONNECTED,
|
||||
|
||||
@@ -18,22 +18,22 @@ package org.springframework.cloud.zookeeper.discovery.watcher;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.state.ConnectionState;
|
||||
import org.apache.curator.x.discovery.ServiceCache;
|
||||
import org.apache.curator.x.discovery.details.ServiceCacheListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* Informs all the DependencyWatcherListeners that a dependency's state has changed
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Tomasz Nurkiewicz, 4financeIT
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DependencyStateChangeListenerRegistry implements ServiceCacheListener {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(DependencyStateChangeListenerRegistry.class);
|
||||
private static final Log log = LogFactory.getLog(DependencyStateChangeListenerRegistry.class);
|
||||
|
||||
private final List<DependencyWatcherListener> listeners;
|
||||
private final String dependencyName;
|
||||
|
||||
@@ -35,9 +35,10 @@ import java.util.List;
|
||||
* Provides hooks for observing dependency lifecycle in Zookeeper.
|
||||
* Needs custom dependencies to be set in order to work.
|
||||
*
|
||||
* @see ZookeeperDependencies
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @see ZookeeperDependencies
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@@ -58,8 +59,8 @@ public class DependencyWatcherAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
public DependencyRegistrationHookProvider dependencyWatcher(
|
||||
ZookeeperServiceDiscovery serviceDiscovery,
|
||||
DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier,
|
||||
ZookeeperDependencies zookeeperDependencies) {
|
||||
DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier,
|
||||
ZookeeperDependencies zookeeperDependencies) {
|
||||
return new DefaultDependencyWatcher(serviceDiscovery,
|
||||
dependencyPresenceOnStartupVerifier,
|
||||
this.dependencyWatcherListeners,
|
||||
|
||||
@@ -19,7 +19,10 @@ package org.springframework.cloud.zookeeper.discovery.watcher;
|
||||
* Performs logic upon change of state of a dependency {@link DependencyState}
|
||||
* in the service discovery system.
|
||||
*
|
||||
* @author <a href="http://www.4financeit.com>4financeIT</a>
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @see org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies
|
||||
*/
|
||||
public interface DependencyWatcherListener {
|
||||
|
||||
@@ -28,8 +31,6 @@ public interface DependencyWatcherListener {
|
||||
*
|
||||
* @param dependencyName - alias from microservice configuration
|
||||
* @param newState - new state of the dependency
|
||||
*
|
||||
* @see org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies
|
||||
*/
|
||||
void stateChanged(String dependencyName, DependencyState newState);
|
||||
}
|
||||
|
||||
@@ -19,9 +19,10 @@ package org.springframework.cloud.zookeeper.discovery.watcher.presence;
|
||||
* By default passes logging dependency checker in order not to shutdown the application
|
||||
* if dependency is missing
|
||||
*
|
||||
* @see LogMissingDependencyChecker
|
||||
* @author Marcin Grzejszczak
|
||||
* @version 1.0.0
|
||||
*
|
||||
* @author <a href="http://www.4financeit.com>4financeIT</a>
|
||||
* @see LogMissingDependencyChecker
|
||||
*/
|
||||
public class DefaultDependencyPresenceOnStartupVerifier extends DependencyPresenceOnStartupVerifier {
|
||||
public DefaultDependencyPresenceOnStartupVerifier() {
|
||||
|
||||
@@ -18,17 +18,17 @@ package org.springframework.cloud.zookeeper.discovery.watcher.presence;
|
||||
import org.apache.curator.x.discovery.ServiceCache;
|
||||
|
||||
/**
|
||||
*
|
||||
* Verifier that checks for presence of mandatory dependencies and delegates to an optional
|
||||
* presence checker verification of presence of optional dependencies.
|
||||
*
|
||||
* The default implementation of required dependencies will result in shutting down of the application
|
||||
* if the dependency is missing.
|
||||
*
|
||||
* @see FailOnMissingDependencyChecker
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Tomasz Szymanski, 4financeIT
|
||||
* @version 1.0.0
|
||||
*
|
||||
* @see FailOnMissingDependencyChecker
|
||||
*/
|
||||
public abstract class DependencyPresenceOnStartupVerifier {
|
||||
private static final PresenceChecker MANDATORY_DEPENDENCY_CHECKER = new FailOnMissingDependencyChecker();
|
||||
|
||||
@@ -20,11 +20,11 @@ import java.util.List;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
|
||||
/**
|
||||
*
|
||||
* Will result in throwing an exception if there are no running instances of the dependency
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Adam Chudzik, 4financeIT
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class FailOnMissingDependencyChecker implements PresenceChecker {
|
||||
@Override
|
||||
|
||||
@@ -18,24 +18,25 @@ package org.springframework.cloud.zookeeper.discovery.watcher.presence;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
|
||||
/**
|
||||
*
|
||||
* Will log the missing microservice dependency
|
||||
*
|
||||
* @author Marcin Grzejszczak, 4financeIT
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Tomasz Dziurko, 4financeIT
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class LogMissingDependencyChecker implements PresenceChecker {
|
||||
|
||||
private static final Log log = org.apache.commons.logging.LogFactory
|
||||
.getLog(LogMissingDependencyChecker.class);
|
||||
private static final Log log = LogFactory.getLog(LogMissingDependencyChecker.class);
|
||||
|
||||
@Override
|
||||
public void checkPresence(String dependencyName, List<ServiceInstance<?>> serviceInstances) {
|
||||
if (serviceInstances.isEmpty()) {
|
||||
log.warn("Microservice dependency with name ["+dependencyName+"] is missing.");
|
||||
log.warn("Microservice dependency with name [" + dependencyName
|
||||
+ "] is missing.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
package org.springframework.cloud.zookeeper.discovery.watcher.presence;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.4financeit.com>4financeIT</a>
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class NoInstancesRunningException extends RuntimeException {
|
||||
public NoInstancesRunningException(String dependencyName) {
|
||||
|
||||
@@ -23,7 +23,8 @@ import org.apache.curator.x.discovery.ServiceInstance;
|
||||
* The implementation of this interface will be called upon checking if a dependency with a given name
|
||||
* is present upon startup within the provided service instances.
|
||||
*
|
||||
* @author <a href="http://www.4financeit.com>4financeIT</a>
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface PresenceChecker {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user