Zookeeper Dependencies

Using the Zookeeper Dependencies

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 through OpenFeign (a REST client builder), RestTemplate and WebClient via Spring Cloud Loadbalancer.spring-doc.cn

You can also use the Zookeeper Dependency Watchers functionality to control and monitor the state of your dependencies.spring-doc.cn

Activating Zookeeper Dependencies

Including a dependency on org.springframework.cloud:spring-cloud-starter-zookeeper-discovery enables autoconfiguration that sets up Spring Cloud Zookeeper Dependencies. Even if you provide the dependencies in your properties, you can turn off the dependencies. To do so, set the spring.cloud.zookeeper.dependency.enabled property to false (it defaults to true).spring-doc.cn

Setting up Zookeeper Dependencies

Consider the following example of dependency representation:spring-doc.cn

application.yml
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

The next few sections go through each part of the dependency one by one. The root property name is spring.cloud.zookeeper.dependencies.spring-doc.cn

Aliases

Below the root property you have to represent each dependency as an alias. This is due to the constraints of Spring Cloud LoadBalancer, which requires that the application ID be placed in the URL. Consequently, you cannot pass any complex path, suchas /myApp/myRoute/name). The alias is the name you use instead of the serviceId for DiscoveryClient, Feign, or RestTemplate.spring-doc.cn

In the previous examples, the aliases are newsletter and mailing. The following example shows Feign usage with a newsletter alias:spring-doc.cn

@FeignClient("newsletter")
public interface NewsletterService {
        @RequestMapping(method = RequestMethod.GET, value = "/newsletter")
        String getNewsletters();
}

Path

The path is represented by the path YAML property and is the path under which the dependency is registered under Zookeeper. As described in the previous section, Spring Cloud LoadBalancer operates on URLs. As a result, this path is not compliant with its requirement. That is why Spring Cloud Zookeeper maps the alias to the proper path.spring-doc.cn

Load Balancer Type

The load balancer type is represented by loadBalancerType YAML property.spring-doc.cn

If you know what kind of load-balancing strategy has to be applied when calling this particular dependency, you can provide it in the YAML file, and it is automatically applied. You can choose one of the following load balancing strategies:spring-doc.cn

Content-Type Template and Version

The Content-Type template and version are represented by the contentTypeTemplate and version YAML properties.spring-doc.cn

If you version your API in the Content-Type header, you do not want to add this header to each of your requests. Also, if you want to call a new version of the API, you do not want to roam around your code to bump up the API version. That is why you can provide a contentTypeTemplate with a special $version placeholder. That placeholder will be filled by the value of the version YAML property. Consider the following example of a contentTypeTemplate:spring-doc.cn

application/vnd.newsletter.$version+json

Further consider the following version:spring-doc.cn

v1

The combination of contentTypeTemplate and version results in the creation of a Content-Type header for each request, as follows:spring-doc.cn

application/vnd.newsletter.v1+json

Default Headers

Default headers are represented by the headers map in YAML.spring-doc.cn

Sometimes, each call to a dependency requires setting up of some default headers. To not do that in code, you can set them up in the YAML file, as shown in the following example headers section:spring-doc.cn

headers:
    Accept:
        - text/html
        - application/xhtml+xml
    Cache-Control:
        - no-cache

That headers section results in adding the Accept and Cache-Control headers with appropriate list of values in your HTTP request.spring-doc.cn

Required Dependencies

Required dependencies are represented by required property in YAML.spring-doc.cn

If one of your dependencies is required to be up when your application boots, you can set the required: true property in the YAML file.spring-doc.cn

If your application cannot localize the required dependency during boot time, it throws an exception, and the Spring Context fails to set up. In other words, your application cannot start if the required dependency is not registered in Zookeeper.spring-doc.cn

You can read more about Spring Cloud Zookeeper Presence Checker later in this document.spring-doc.cn

Stubs

You can provide a colon-separated path to the JAR containing stubs of the dependency, as shown in the following example:spring-doc.cn

stubs: org.springframework:myApp:stubsspring-doc.cn

Because stubs is the default classifier, the preceding example is equal to the following example:spring-doc.cn

stubs: org.springframework:myAppspring-doc.cn

Configuring Spring Cloud Zookeeper Dependencies

You can set the following properties to enable or disable parts of Zookeeper Dependencies functionalities:spring-doc.cn

  • spring.cloud.zookeeper.dependencies: If you do not set this property, you cannot use Zookeeper Dependencies.spring-doc.cn

  • spring.cloud.zookeeper.dependency.loadbalancer.enabled (enabled by default): Turns on Zookeeper-specific custom load-balancing strategies, including ZookeeperServiceInstanceListSupplier and dependency-based load-balanced RestTemplate setup.spring-doc.cn

  • spring.cloud.zookeeper.dependency.headers.enabled (enabled by default): This property registers a FeignBlockingLoadBalancerClient that automatically appends appropriate headers and content types with their versions, as presented in the Dependency configuration. Without this setting, those two parameters do not work.spring-doc.cn

  • spring.cloud.zookeeper.dependency.resttemplate.enabled (enabled by default): When enabled, this property modifies the request headers of a @LoadBalanced-annotated RestTemplate such that it passes headers and content type with the version set in dependency configuration. Without this setting, those two parameters do not work.spring-doc.cn