140 lines
37 KiB
HTML
140 lines
37 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Spring Cloud Zookeeper</title><link rel="stylesheet" type="text/css" href="css/manual-singlepage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div lang="en" class="book"><div class="titlepage"><div><div><h1 class="title"><a name="d0e3"></a>Spring Cloud Zookeeper</h1></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="preface"><a href="#d0e9"></a></span></dt><dt><span class="chapter"><a href="#spring-cloud-zookeeper-install">1. Install Zookeeper</a></span></dt><dt><span class="chapter"><a href="#spring-cloud-zookeeper-discovery">2. Service Discovery with Zookeeper</a></span></dt><dd><dl><dt><span class="section"><a href="#_how_to_activate">2.1. How to activate</a></span></dt><dt><span class="section"><a href="#_registering_with_zookeeper">2.2. Registering with Zookeeper</a></span></dt><dt><span class="section"><a href="#_using_the_discoveryclient">2.3. Using the DiscoveryClient</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-zookeeper-netflix">3. Using Spring Cloud Zookeeper with Spring Cloud Netflix Components</a></span></dt><dd><dl><dt><span class="section"><a href="#_ribbon_with_zookeeper">3.1. Ribbon with Zookeeper</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-zookeeper-service-registry">4. Spring Cloud Zookeeper and Service Registry</a></span></dt><dd><dl><dt><span class="section"><a href="#_instance_status">4.1. Instance Status</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-zookeeper-dependencies">5. Zookeeper Dependencies</a></span></dt><dd><dl><dt><span class="section"><a href="#_using_the_zookeeper_dependencies">5.1. Using the Zookeeper Dependencies</a></span></dt><dt><span class="section"><a href="#_how_to_activate_zookeeper_dependencies">5.2. How to activate Zookeeper Dependencies</a></span></dt><dt><span class="section"><a href="#_setting_up_zookeeper_dependencies">5.3. Setting up Zookeeper Dependencies</a></span></dt><dd><dl><dt><span class="section"><a href="#_aliases">5.3.1. Aliases</a></span></dt><dt><span class="section"><a href="#_path">5.3.2. Path</a></span></dt><dt><span class="section"><a href="#_load_balancer_type">5.3.3. Load balancer type</a></span></dt><dt><span class="section"><a href="#_content_type_template_and_version">5.3.4. Content-Type template and version</a></span></dt><dt><span class="section"><a href="#_default_headers">5.3.5. Default headers</a></span></dt><dt><span class="section"><a href="#_obligatory_dependencies">5.3.6. Obligatory dependencies</a></span></dt><dt><span class="section"><a href="#_stubs">5.3.7. Stubs</a></span></dt></dl></dd><dt><span class="section"><a href="#_configuring_spring_cloud_zookeeper_dependencies">5.4. Configuring Spring Cloud Zookeeper Dependencies</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-zookeeper-dependency-watcher">6. Spring Cloud Zookeeper Dependency Watcher</a></span></dt><dd><dl><dt><span class="section"><a href="#_how_to_activate_2">6.1. How to activate</a></span></dt><dt><span class="section"><a href="#_registering_a_listener">6.2. Registering a listener</a></span></dt><dt><span class="section"><a href="#_presence_checker">6.3. Presence Checker</a></span></dt></dl></dd><dt><span class="chapter"><a href="#spring-cloud-zookeeper-config">7. Distributed Configuration with Zookeeper</a></span></dt><dd><dl><dt><span class="section"><a href="#_how_to_activate_3">7.1. How to activate</a></span></dt><dt><span class="section"><a href="#_customizing">7.2. Customizing</a></span></dt><dt><span class="section"><a href="#_acls">7.3. ACLs</a></span></dt></dl></dd></dl></div><div class="preface"><div class="titlepage"><div><div><h1 class="title"><a name="d0e9" href="#d0e9"></a></h1></div></div></div><p>This project provides Zookeeper integrations for Spring Boot apps through autoconfiguration
|
|
and binding to the Spring Environment and other Spring programming model idioms. With a few
|
|
simple annotations you can quickly enable and configure the common patterns inside your
|
|
application and build large distributed systems with Zookeeper based components. The
|
|
patterns provided include Service Discovery and Configuration.
|
|
Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon), Circuit Breaker
|
|
(Hystrix) are provided by integration with Spring Cloud Netflix.</p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-install" href="#spring-cloud-zookeeper-install"></a>1. Install Zookeeper</h1></div></div></div><p>Please see the <a class="link" href="http://zookeeper.apache.org/doc/current/zookeeperStarted.html" target="_top">installation documentation</a> for instructions on how to install Zookeeper.</p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-discovery" href="#spring-cloud-zookeeper-discovery"></a>2. Service Discovery with Zookeeper</h1></div></div></div><p>Service Discovery is one of the key tenets of a microservice based architecture. Trying to hand configure each client or some form of convention can be very difficult to do and can be very brittle. <a class="link" href="http://curator.apache.org" target="_top">Curator</a>(A java library for Zookeeper) provides Service Discovery services via <a class="link" href="http://curator.apache.org/curator-x-discovery/" target="_top">Service Discovery Extension</a>. Spring Cloud Zookeeper leverages this extension for service registration and discovery.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_how_to_activate" href="#_how_to_activate"></a>2.1 How to activate</h2></div></div></div><p>Including a dependency on <code class="literal">org.springframework.cloud:spring-cloud-starter-zookeeper-discovery</code> will enable auto-configuration that will setup Spring Cloud Zookeeper Discovery.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>You still need to include <code class="literal">org.springframework.boot:spring-boot-starter-web</code> for web functionality.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_registering_with_zookeeper" href="#_registering_with_zookeeper"></a>2.2 Registering with Zookeeper</h2></div></div></div><p>When a client registers with Zookeeper, it provides meta-data about itself such as host and port, id and name.</p><p>Example Zookeeper client:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootApplication</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@EnableDiscoveryClient</span></em>
|
|
<em><span class="hl-annotation" style="color: gray">@RestController</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@RequestMapping("/")</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String home() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Hello world"</span>;
|
|
}
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> SpringApplicationBuilder(Application.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>).web(true).run(args);
|
|
}
|
|
|
|
}</pre><p>(i.e. utterly normal Spring Boot app). If Zookeeper is located somewhere other than <code class="literal">localhost:2181</code>, the configuration is required to locate the server. Example:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">spring:
|
|
cloud:
|
|
zookeeper:
|
|
connect-string: localhost:2181</pre><p>
|
|
</p><div class="caution" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Caution"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="images/caution.png"></td><th align="left">Caution</th></tr><tr><td align="left" valign="top"><p>If you use <a class="link" href="#spring-cloud-zookeeper-config" title="7. Distributed Configuration with Zookeeper">Spring Cloud Zookeeper Config</a>, the above values will need to be placed in <code class="literal">bootstrap.yml</code> instead of <code class="literal">application.yml</code>.</p></td></tr></table></div><p>The default service name, instance id and port, taken from the <code class="literal">Environment</code>, are <code class="literal">${spring.application.name}</code>, the Spring Context ID and <code class="literal">${server.port}</code> respectively.</p><p><code class="literal">@EnableDiscoveryClient</code> makes the app into both a Zookeeper "service" (i.e. it registers itself) and a "client" (i.e. it can query Zookeeper to locate other services).</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_the_discoveryclient" href="#_using_the_discoveryclient"></a>2.3 Using the DiscoveryClient</h2></div></div></div><p>Spring Cloud has support for <a class="link" href="https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#spring-cloud-feign" target="_top">Feign</a> (a REST client builder) and also <a class="link" href="https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#spring-cloud-ribbon" target="_top">Spring <code class="literal">RestTemplate</code></a> using the logical service names instead of physical URLs.</p><p>You can also use the <code class="literal">org.springframework.cloud.client.discovery.DiscoveryClient</code> which provides a simple API for discovery clients that is not specific to Netflix, e.g.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> DiscoveryClient discoveryClient;
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String serviceUrl() {
|
|
List<ServiceInstance> list = discoveryClient.getInstances(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"STORES"</span>);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">if</span> (list != null && list.size() > <span class="hl-number">0</span> ) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> list.get(<span class="hl-number">0</span>).getUri().toString();
|
|
}
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> null;
|
|
}</pre></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-netflix" href="#spring-cloud-zookeeper-netflix"></a>3. Using Spring Cloud Zookeeper with Spring Cloud Netflix Components</h1></div></div></div><p>Spring Cloud Netflix supplies useful tools that work regardless of which <code class="literal">DiscoveryClient</code> implementation is used. Feign, Turbine, Ribbon and Zuul all work with Spring Cloud Zookeeper.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_ribbon_with_zookeeper" href="#_ribbon_with_zookeeper"></a>3.1 Ribbon with Zookeeper</h2></div></div></div><p>Spring Cloud Zookeeper provides an implementation of Ribbon’s <code class="literal">ServerList</code>. When the <code class="literal">spring-cloud-starter-zookeeper-discovery</code> is used, Ribbon is auto-configured to use the <code class="literal">ZookeeperServerList</code> by default.</p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-service-registry" href="#spring-cloud-zookeeper-service-registry"></a>4. Spring Cloud Zookeeper and Service Registry</h1></div></div></div><p>Spring Cloud Zookeeper implements the <code class="literal">ServiceRegistry</code> interface allowing developers to register arbitrary service in a programmatic way.</p><p>The <code class="literal">ServiceInstanceRegistration</code> class offers a <code class="literal">builder()</code> method to create a <code class="literal">Registration</code> object that can be used by the <code class="literal">ServiceRegistry</code>.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> ZookeeperServiceRegistry serviceRegistry;
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> registerThings() {
|
|
ZookeeperRegistration registration = ServiceInstanceRegistration.builder()
|
|
.defaultUriSpec()
|
|
.address(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"anyUrl"</span>)
|
|
.port(<span class="hl-number">10</span>)
|
|
.name(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/a/b/c/d/anotherservice"</span>)
|
|
.build();
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.serviceRegistry.register(registration);
|
|
}</pre><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_instance_status" href="#_instance_status"></a>4.1 Instance Status</h2></div></div></div><p>Netflix Eureka supports having instances registered with the server that are <code class="literal">OUT_OF_SERVICE</code> and not returned as active service instances. This is very useful for behaviors such as blue/green deployments. The Curator Service Discovery recipe does not support this behavior. Taking advantage of the flexible payload has let Spring Cloud Zookeeper implement <code class="literal">OUT_OF_SERVICE</code> by updating some specific metadata and then filtering on that metadata in the Ribbon <code class="literal">ZookeeperServerList</code>. The <code class="literal">ZookeeperServerList</code> filters out all non-null instance statuses that do not equal <code class="literal">UP</code>. If the instance status field is empty, it is considered <code class="literal">UP</code> for backwards compatibility. To change the status of an instance POST <code class="literal">OUT_OF_SERVICE</code> to the <code class="literal">ServiceRegistry</code> instance status actuator endpoint.</p><pre class="literallayout">----
|
|
$ echo -n OUT_OF_SERVICE | http POST http://localhost:8081/service-registry/instance-status
|
|
----</pre><pre class="literallayout">NOTE: The above example uses the `http` command from https://httpie.org</pre></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-dependencies" href="#spring-cloud-zookeeper-dependencies"></a>5. Zookeeper Dependencies</h1></div></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_the_zookeeper_dependencies" href="#_using_the_zookeeper_dependencies"></a>5.1 Using the Zookeeper Dependencies</h2></div></div></div><p>Spring Cloud Zookeeper gives you a possibility to provide dependencies of your application as properties. As dependencies you can understand other applications that are registered
|
|
in Zookeeper and which you would like to call via <a class="link" href="https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#spring-cloud-feign" target="_top">Feign</a> (a REST client builder)
|
|
and also <a class="link" href="https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#spring-cloud-ribbon" target="_top">Spring <code class="literal">RestTemplate</code></a>.</p><p>You can also benefit from the Zookeeper Dependency Watchers functionality that lets you control and monitor what is the state of your dependencies and decide what to do with that.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_how_to_activate_zookeeper_dependencies" href="#_how_to_activate_zookeeper_dependencies"></a>5.2 How to activate Zookeeper Dependencies</h2></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Including a dependency on <code class="literal">org.springframework.cloud:spring-cloud-starter-zookeeper-discovery</code> will enable auto-configuration that will setup Spring Cloud Zookeeper Dependencies.</li><li class="listitem">If you have to have the <code class="literal">spring.cloud.zookeeper.dependencies</code> section properly set up - check the subsequent section for more details then the feature is active</li><li class="listitem">You can have the dependencies turned off even if you’ve provided the dependencies in your properties. Just set the property <code class="literal">spring.cloud.zookeeper.dependency.enabled</code> to false (defaults to <code class="literal">true</code>).</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_setting_up_zookeeper_dependencies" href="#_setting_up_zookeeper_dependencies"></a>5.3 Setting up Zookeeper Dependencies</h2></div></div></div><p>Let’s take a closer look at an example of dependencies representation:</p><p><b>application.yml. </b>
|
|
</p><pre class="screen">spring.application.name: yourServiceName
|
|
spring.cloud.zookeeper:
|
|
dependencies:
|
|
newsletter:
|
|
path: /path/where/newsletter/has/registered/in/zookeeper
|
|
loadBalancerType: ROUND_ROBIN
|
|
contentTypeTemplate: application/vnd.newsletter.$version+json
|
|
version: v1
|
|
headers:
|
|
header1:
|
|
- value1
|
|
header2:
|
|
- value2
|
|
required: false
|
|
stubs: org.springframework:foo:stubs
|
|
mailing:
|
|
path: /path/where/mailing/has/registered/in/zookeeper
|
|
loadBalancerType: ROUND_ROBIN
|
|
contentTypeTemplate: application/vnd.mailing.$version+json
|
|
version: v1
|
|
required: true</pre><p>
|
|
</p><p>Let’s now go through each part of the dependency one by one. The root property name is <code class="literal">spring.cloud.zookeeper.dependencies</code>.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_aliases" href="#_aliases"></a>5.3.1 Aliases</h3></div></div></div><p>Below the root property you have to represent each dependency has by an alias due to the constraints of Ribbon (the application id has to be placed in the URL
|
|
thus you can’t pass any complex path like /foo/bar/name). The alias will be the name that you will use instead of serviceId for <code class="literal">DiscoveryClient</code>, <code class="literal">Feign</code> or <code class="literal">RestTemplate</code>.</p><p>In the aforementioned examples the aliases are <code class="literal">newsletter</code> and <code class="literal">mailing</code>. Example of Feign usage with <code class="literal">newsletter</code> would be:</p><pre class="screen">@FeignClient("newsletter")
|
|
public interface NewsletterService {
|
|
@RequestMapping(method = RequestMethod.GET, value = "/newsletter")
|
|
String getNewsletters();
|
|
}</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_path" href="#_path"></a>5.3.2 Path</h3></div></div></div><p>Represented by <code class="literal">path</code> yaml property.</p><p>Path is the path under which the dependency is registered under Zookeeper. Like presented before Ribbon operates on URLs thus this path is not compliant with its requirement.
|
|
That is why Spring Cloud Zookeeper maps the alias to the proper path.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_load_balancer_type" href="#_load_balancer_type"></a>5.3.3 Load balancer type</h3></div></div></div><p>Represented by <code class="literal">loadBalancerType</code> yaml property.</p><p>If you know what kind of load balancing strategy has to be applied when calling this particular dependency then you can provide it in the yaml file and it will be automatically applied.
|
|
You can choose one of the following load balancing strategies</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">STICKY - once chosen the instance will always be called</li><li class="listitem">RANDOM - picks an instance randomly</li><li class="listitem">ROUND_ROBIN - iterates over instances over and over again</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_content_type_template_and_version" href="#_content_type_template_and_version"></a>5.3.4 Content-Type template and version</h3></div></div></div><p>Represented by <code class="literal">contentTypeTemplate</code> and <code class="literal">version</code> yaml property.</p><p>If you version your api via the <code class="literal">Content-Type</code> header then you don’t want to add this header to each of your requests. Also if you want to call a new version of the API you don’t want to
|
|
roam around your code to bump up the API version. That’s why you can provide a <code class="literal">contentTypeTemplate</code> with a special <code class="literal">$version</code> placeholder. That placeholder will be filled by the value of the
|
|
<code class="literal">version</code> yaml property. Let’s take a look at an example.</p><p>Having the following <code class="literal">contentTypeTemplate</code>:</p><pre class="screen">application/vnd.newsletter.$version+json</pre><p>and the following <code class="literal">version</code>:</p><pre class="screen">v1</pre><p>Will result in setting up of a <code class="literal">Content-Type</code> header for each request:</p><pre class="screen">application/vnd.newsletter.v1+json</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_default_headers" href="#_default_headers"></a>5.3.5 Default headers</h3></div></div></div><p>Represented by <code class="literal">headers</code> map in yaml</p><p>Sometimes each call to a dependency requires setting up of some default headers. In order not to do that in code you can set them up in the yaml file.
|
|
Having the following <code class="literal">headers</code> section:</p><pre class="screen">headers:
|
|
Accept:
|
|
- text/html
|
|
- application/xhtml+xml
|
|
Cache-Control:
|
|
- no-cache</pre><p>Results in adding the <code class="literal">Accept</code> and <code class="literal">Cache-Control</code> headers with appropriate list of values in your HTTP request.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_obligatory_dependencies" href="#_obligatory_dependencies"></a>5.3.6 Obligatory dependencies</h3></div></div></div><p>Represented by <code class="literal">required</code> property in yaml</p><p>If one of your dependencies is required to be up and running when your application is booting then it’s enough to set up the <code class="literal">required: true</code> property in the yaml file.</p><p>If your application can’t localize the required dependency during boot time it will throw an exception and the Spring Context will fail to set up.
|
|
In other words your application won’t be able to start if the required dependency is not registered in Zookeeper.</p><p>You can read more about Spring Cloud Zookeeper Presence Checker in the following sections.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_stubs" href="#_stubs"></a>5.3.7 Stubs</h3></div></div></div><p>You can provide a colon separated path to the JAR containing stubs of the dependency. Example</p><pre class="screen">stubs: org.springframework:foo:stubs</pre><p>means that for a particular dependencies can be found under:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">groupId: <code class="literal">org.springframework</code></li><li class="listitem">artifactId: <code class="literal">foo</code></li><li class="listitem">classifier: <code class="literal">stubs</code> - this is the default value</li></ul></div><p>This is actually equal to</p><pre class="screen">stubs: org.springframework:foo</pre><p>since <code class="literal">stubs</code> is the default classifier.</p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuring_spring_cloud_zookeeper_dependencies" href="#_configuring_spring_cloud_zookeeper_dependencies"></a>5.4 Configuring Spring Cloud Zookeeper Dependencies</h2></div></div></div><p>There is a bunch of properties that you can set to enable / disable parts of Zookeeper Dependencies functionalities.</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">spring.cloud.zookeeper.dependencies</code> - if you don’t set this property you won’t benefit from Zookeeper Dependencies</li><li class="listitem"><code class="literal">spring.cloud.zookeeper.dependency.ribbon.enabled</code> (enabled by default) - Ribbon requires explicit global configuration or a particular one for a dependency. By turning on this property
|
|
runtime load balancing strategy resolution is possible and you can profit from the <code class="literal">loadBalancerType</code> section of the Zookeeper Dependencies. The configuration that needs this property
|
|
has an implementation of <code class="literal">LoadBalancerClient</code> that delegates to the <code class="literal">ILoadBalancer</code> presented in the next bullet</li><li class="listitem"><code class="literal">spring.cloud.zookeeper.dependency.ribbon.loadbalancer</code> (enabled by default) - thanks to this property the custom <code class="literal">ILoadBalancer</code> knows that the part of the URI passed to Ribbon might
|
|
actually be the alias that has to be resolved to a proper path in Zookeeper. Without this property you won’t be able to register applications under nested paths.</li><li class="listitem"><code class="literal">spring.cloud.zookeeper.dependency.headers.enabled</code> (enabled by default) - this property registers such a <code class="literal">RibbonClient</code> that automatically will append appropriate headers and content
|
|
types with version as presented in the Dependency configuration. Without this setting of those two parameters will not be operational.</li><li class="listitem"><code class="literal">spring.cloud.zookeeper.dependency.resttemplate.enabled</code> (enabled by default) - when enabled will modify the request headers of <code class="literal">@LoadBalanced</code> annotated <code class="literal">RestTemplate</code> so that it passes
|
|
headers and content type with version set in Dependency configuration. Wihtout this setting of those two parameters will not be operational.</li></ul></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-dependency-watcher" href="#spring-cloud-zookeeper-dependency-watcher"></a>6. Spring Cloud Zookeeper Dependency Watcher</h1></div></div></div><p>The Dependency Watcher mechanism allows you to register listeners to your dependencies. The functionality is in fact an implementation of the <code class="literal">Observator</code> pattern. When a dependency changes
|
|
its state (UP or DOWN) then some custom logic can be applied.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_how_to_activate_2" href="#_how_to_activate_2"></a>6.1 How to activate</h2></div></div></div><p>Spring Cloud Zookeeper Dependencies functionality needs to be enabled to profit from Dependency Watcher mechanism.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_registering_a_listener" href="#_registering_a_listener"></a>6.2 Registering a listener</h2></div></div></div><p>In order to register a listener you have to implement an interface <code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.DependencyWatcherListener</code> and register it as a bean.
|
|
The interface gives you one method:</p><pre class="screen"> void stateChanged(String dependencyName, DependencyState newState);</pre><p>If you want to register a listener for a particular dependency then the <code class="literal">dependencyName</code> would be the discriminator for your concrete implementation. <code class="literal">newState</code> will provide you with information
|
|
whether your dependency has changed to <code class="literal">CONNECTED</code> or <code class="literal">DISCONNECTED</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_presence_checker" href="#_presence_checker"></a>6.3 Presence Checker</h2></div></div></div><p>Bound with Dependency Watcher is the functionality called Presence Checker. It allows you to provide custom behaviour upon booting of your application to react accordingly to the state
|
|
of your dependencies.</p><p>The default implementation of the abstract <code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier</code> class is the
|
|
<code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.presence.DefaultDependencyPresenceOnStartupVerifier</code> which works in the following way.</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">If the dependency is marked us <code class="literal">required</code> and it’s not in Zookeeper then upon booting your application will throw an exception and shutdown</li><li class="listitem">If dependency is not <code class="literal">required</code> the <code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker</code> will log that application is missing at <code class="literal">WARN</code> level</li></ul></div><p>The functionality can be overriden since the <code class="literal">DefaultDependencyPresenceOnStartupVerifier</code> is registered only when there is no bean of <code class="literal">DependencyPresenceOnStartupVerifier</code>.</p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="spring-cloud-zookeeper-config" href="#spring-cloud-zookeeper-config"></a>7. Distributed Configuration with Zookeeper</h1></div></div></div><p>Zookeeper provides a <a class="link" href="http://zookeeper.apache.org/doc/current/zookeeperOver.html#sc_dataModelNameSpace" target="_top">hierarchical namespace</a> that allows clients to store arbitrary data, such as configuration data. Spring Cloud Zookeeper Config is an alternative to the <a class="link" href="https://github.com/spring-cloud/spring-cloud-config" target="_top">Config Server and Client</a>. Configuration is loaded into the Spring Environment during the special "bootstrap" phase. Configuration is stored in the <code class="literal">/config</code> namespace by default. Multiple <code class="literal">PropertySource</code> instances are created based on the application’s name and the active profiles that mimicks the Spring Cloud Config order of resolving properties. For example, an application with the name "testApp" and with the "dev" profile will have the following property sources created:</p><pre class="screen">config/testApp,dev
|
|
config/testApp
|
|
config/application,dev
|
|
config/application</pre><p>The most specific property source is at the top, with the least specific at the bottom. Properties is the <code class="literal">config/application</code> namespace are applicable to all applications using zookeeper for configuration. Properties in the <code class="literal">config/testApp</code> namespace are only available to the instances of the service named "testApp".</p><p>Configuration is currently read on startup of the application. Sending a HTTP POST to <code class="literal">/refresh</code> will cause the configuration to be reloaded. Watching the configuration namespace (which Zookeeper supports) is not currently implemented, but will be a future addition to this project.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_how_to_activate_3" href="#_how_to_activate_3"></a>7.1 How to activate</h2></div></div></div><p>Including a dependency on <code class="literal">org.springframework.cloud:spring-cloud-starter-zookeeper-config</code> will enable auto-configuration that will setup Spring Cloud Zookeeper Config.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_customizing" href="#_customizing"></a>7.2 Customizing</h2></div></div></div><p>Zookeeper Config may be customized using the following properties:</p><p><b>bootstrap.yml. </b>
|
|
</p><pre class="screen">spring:
|
|
cloud:
|
|
zookeeper:
|
|
config:
|
|
enabled: true
|
|
root: configuration
|
|
defaultContext: apps
|
|
profileSeparator: '::'</pre><p>
|
|
</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">enabled</code> setting this value to "false" disables Zookeeper Config</li><li class="listitem"><code class="literal">root</code> sets the base namespace for configuration values</li><li class="listitem"><code class="literal">defaultContext</code> sets the name used by all applications</li><li class="listitem"><code class="literal">profileSeparator</code> sets the value of the separator used to separate the profile name in property sources with profiles</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_acls" href="#_acls"></a>7.3 ACLs</h2></div></div></div><p>You can add authentication information for Zookeeper ACLs by calling the addAuthInfo method of a
|
|
CuratorFramework bean. One way to accomplish this is by providing your own CuratorFramework bean:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@BoostrapConfiguration</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> CustomCuratorFrameworkConfig {
|
|
|
|
<em><span class="hl-annotation" style="color: gray">@Bean</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> CuratorFramework curatorFramework() {
|
|
CuratorFramework curator = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> CuratorFramework();
|
|
curator.addAuthInfo(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"digest"</span>, <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"user:password"</span>.getBytes());
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> curator;
|
|
}
|
|
|
|
}</pre><p>Consult <a class="link" href="https://github.com/spring-cloud/spring-cloud-zookeeper/blob/master/spring-cloud-zookeeper-core/src/main/java/org/springframework/cloud/zookeeper/ZookeeperAutoConfiguration.java" target="_top">the ZookeeperAutoConfiguration class</a>
|
|
to see how the CuratorFramework bean is configured by default.</p><p>Alternatively, you can add your credentials from a class that depends on the existing
|
|
CuratorFramework bean:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@BoostrapConfiguration</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> DefaultCuratorFrameworkConfig {
|
|
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ZookeeperConfig(CuratorFramework curator) {
|
|
curator.addAuthInfo(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"digest"</span>, <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"user:password"</span>.getBytes());
|
|
}
|
|
|
|
}</pre><p>This must occur during the boostrapping phase. You can register configuration classes to run
|
|
during this phase by annotating them with <code class="literal">@BootstrapConfiguration</code> and including them in a
|
|
comma-separated list set as the value of the property
|
|
<code class="literal">org.springframework.cloud.bootstrap.BootstrapConfiguration</code> in the file
|
|
<code class="literal">resources/META-INF/spring.factories</code>:</p><p><b>resources/META-INF/spring.factories. </b>
|
|
</p><pre class="screen">org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
|
my.project.CustomCuratorFrameworkConfig,\
|
|
my.project.DefaultCuratorFrameworkConfig</pre><p>
|
|
</p></div></div></div></body></html> |