48 lines
14 KiB
HTML
48 lines
14 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>70. Zookeeper Dependencies</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi__spring_cloud_zookeeper.html" title="Part IX. Spring Cloud Zookeeper"><link rel="prev" href="multi_spring-cloud-zookeeper-service-registry.html" title="69. Spring Cloud Zookeeper and Service Registry"><link rel="next" href="multi_spring-cloud-zookeeper-dependency-watcher.html" title="71. Spring Cloud Zookeeper Dependency Watcher"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">70. Zookeeper Dependencies</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi_spring-cloud-zookeeper-service-registry.html">Prev</a> </td><th width="60%" align="center">Part IX. Spring Cloud Zookeeper</th><td width="20%" align="right"> <a accesskey="n" href="multi_spring-cloud-zookeeper-dependency-watcher.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="spring-cloud-zookeeper-dependencies" href="#spring-cloud-zookeeper-dependencies"></a>70. Zookeeper Dependencies</h2></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>70.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>70.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>70.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>70.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>70.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>70.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>70.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>70.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>70.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>70.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>70.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="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi_spring-cloud-zookeeper-service-registry.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="multi__spring_cloud_zookeeper.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="multi_spring-cloud-zookeeper-dependency-watcher.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">69. Spring Cloud Zookeeper and Service Registry </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top"> 71. Spring Cloud Zookeeper Dependency Watcher</td></tr></table></div></body></html> |