(window.webpackJsonp=window.webpackJsonp||[]).push([[82],{507:function(e,t,o){"use strict";o.r(t);var n=o(56),r=Object(n.a)({},(function(){var e=this,t=e.$createElement,o=e._self._c||t;return o("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[o("h1",{attrs:{id:"spring-cloud-zookeeper"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#spring-cloud-zookeeper"}},[e._v("#")]),e._v(" "),o("a",{attrs:{href:"#_spring_cloud_zookeeper"}},[e._v("Spring Cloud Zookeeper")])]),e._v(" "),o("p",[e._v("This project provides Zookeeper integrations for Spring Boot applications through\nautoconfiguration and binding to the Spring Environment and other Spring programming model\nidioms. With a few annotations, you can quickly enable and configure the common patterns\ninside your application and build large distributed systems with Zookeeper based\ncomponents. The provided patterns include Service Discovery and Configuration. The project\nalso provides client-side load-balancing via integration with Spring Cloud LoadBalancer.")]),e._v(" "),o("h2",{attrs:{id:"_1-quick-start"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_1-quick-start"}},[e._v("#")]),e._v(" 1. Quick Start")]),e._v(" "),o("p",[e._v("This quick start walks through using Spring Cloud Zookeeper for Service Discovery and Distributed Configuration.")]),e._v(" "),o("p",[e._v("First, run Zookeeper on your machine. Then you can access it and use it as a Service Registry and Configuration source with Spring Cloud Zookeeper.")]),e._v(" "),o("h3",{attrs:{id:"_1-1-discovery-client-usage"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_1-1-discovery-client-usage"}},[e._v("#")]),e._v(" 1.1. Discovery Client Usage")]),e._v(" "),o("p",[e._v("To use these features in an application, you can build it as a Spring Boot application that depends on "),o("code",[e._v("spring-cloud-zookeeper-core")]),e._v(" and "),o("code",[e._v("spring-cloud-zookeeper-discovery")]),e._v(".\nThe most convenient way to add the dependency is with a Spring Boot starter: "),o("code",[e._v("org.springframework.cloud:spring-cloud-starter-zookeeper-discovery")]),e._v(".\nWe recommend using dependency management and "),o("code",[e._v("spring-boot-starter-parent")]),e._v(".\nThe following example shows a typical Maven configuration:")]),e._v(" "),o("p",[e._v("pom.xml")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("\n\n org.springframework.boot\n spring-boot-starter-parent\n {spring-boot-version}\n \x3c!-- lookup parent from repository --\x3e\n \n\n \n \n org.springframework.cloud\n spring-cloud-starter-zookeeper-discovery\n \n \n org.springframework.boot\n spring-boot-starter-test\n test\n \n \n \n \n \n org.springframework.cloud\n spring-cloud-dependencies\n ${spring-cloud.version}\n pom\n import\n \n \n \n \n \n \n org.springframework.boot\n spring-boot-maven-plugin\n \n \n \n\n")])])]),o("p",[e._v("The following example shows a typical Gradle setup:")]),e._v(" "),o("p",[e._v("build.gradle")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("plugins {\n id 'org.springframework.boot' version ${spring-boot-version}\n id 'io.spring.dependency-management' version ${spring-dependency-management-version}\n id 'java'\n}\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'\n testImplementation 'org.springframework.boot:spring-boot-starter-test'\n}\ndependencyManagement {\n imports {\n mavenBom \"org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}\"\n }\n}\n")])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("Depending on the version you are using, you might need to adjust Apache Zookeeper version used in your project."),o("br"),e._v("You can read more about it in the "),o("a",{attrs:{href:"#spring-cloud-zookeeper-install"}},[e._v("Install Zookeeper section")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("Now you can create a standard Spring Boot application, such as the following HTTP server:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@SpringBootApplication\n@RestController\npublic class Application {\n\n @GetMapping("/")\n public String home() {\n return "Hello World!";\n }\n\n public static void main(String[] args) {\n SpringApplication.run(Application.class, args);\n }\n\n}\n')])])]),o("p",[e._v("When this HTTP server runs, it connects to Zookeeper, which runs on the default local port (2181).\nTo modify the startup behavior, you can change the location of Zookeeper by using "),o("code",[e._v("application.properties")]),e._v(", as shown in the following example:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring:\n cloud:\n zookeeper:\n connect-string: localhost:2181\n")])])]),o("p",[e._v("You can now use "),o("code",[e._v("DiscoveryClient")]),e._v(", "),o("code",[e._v("@LoadBalanced RestTemplate")]),e._v(", or "),o("code",[e._v("@LoadBalanced WebClient.Builder")]),e._v(" to retrieve services and instances data from Zookeeper, as shown in the following example:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@Autowired\nprivate DiscoveryClient discoveryClient;\n\npublic String serviceUrl() {\n List list = discoveryClient.getInstances("STORES");\n if (list != null && list.size() > 0 ) {\n return list.get(0).getUri().toString();\n }\n return null;\n}\n')])])]),o("h3",{attrs:{id:"_1-2-distributed-configuration-usage"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_1-2-distributed-configuration-usage"}},[e._v("#")]),e._v(" 1.2. Distributed Configuration Usage")]),e._v(" "),o("p",[e._v("To use these features in an application, you can build it as a Spring Boot application that depends on "),o("code",[e._v("spring-cloud-zookeeper-core")]),e._v(" and "),o("code",[e._v("spring-cloud-zookeeper-config")]),e._v(".\nThe most convenient way to add the dependency is with a Spring Boot starter: "),o("code",[e._v("org.springframework.cloud:spring-cloud-starter-zookeeper-config")]),e._v(".\nWe recommend using dependency management and "),o("code",[e._v("spring-boot-starter-parent")]),e._v(".\nThe following example shows a typical Maven configuration:")]),e._v(" "),o("p",[e._v("pom.xml")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("\n\n org.springframework.boot\n spring-boot-starter-parent\n {spring-boot-version}\n \x3c!-- lookup parent from repository --\x3e\n \n\n \n \n org.springframework.cloud\n spring-cloud-starter-zookeeper-config\n \n \n org.springframework.boot\n spring-boot-starter-test\n test\n \n \n \n \n \n org.springframework.cloud\n spring-cloud-dependencies\n ${spring-cloud.version}\n pom\n import\n \n \n \n \n \n \n org.springframework.boot\n spring-boot-maven-plugin\n \n \n \n\n")])])]),o("p",[e._v("The following example shows a typical Gradle setup:")]),e._v(" "),o("p",[e._v("build.gradle")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("plugins {\n id 'org.springframework.boot' version ${spring-boot-version}\n id 'io.spring.dependency-management' version ${spring-dependency-management-version}\n id 'java'\n}\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-config'\n testImplementation 'org.springframework.boot:spring-boot-starter-test'\n}\ndependencyManagement {\n imports {\n mavenBom \"org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}\"\n }\n}\n")])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("Depending on the version you are using, you might need to adjust Apache Zookeeper version used in your project."),o("br"),e._v("You can read more about it in the "),o("a",{attrs:{href:"#spring-cloud-zookeeper-install"}},[e._v("Install Zookeeper section")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("Now you can create a standard Spring Boot application, such as the following HTTP server:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@SpringBootApplication\n@RestController\npublic class Application {\n\n @GetMapping("/")\n public String home() {\n return "Hello World!";\n }\n\n public static void main(String[] args) {\n SpringApplication.run(Application.class, args);\n }\n\n}\n')])])]),o("p",[e._v("The application retrieves configuration data from Zookeeper.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("If you use Spring Cloud Zookeeper Config, you need to set the "),o("code",[e._v("spring.config.import")]),e._v(" property in order to bind to Zookeeper."),o("br"),e._v("You can read more about it in the "),o("a",{attrs:{href:"#config-data-import"}},[e._v("Spring Boot Config Data Import section")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h2",{attrs:{id:"_2-install-zookeeper"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_2-install-zookeeper"}},[e._v("#")]),e._v(" 2. Install Zookeeper")]),e._v(" "),o("p",[e._v("See the "),o("a",{attrs:{href:"https://zookeeper.apache.org/doc/current/zookeeperStarted.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("installation\ndocumentation"),o("OutboundLink")],1),e._v(" for instructions on how to install Zookeeper.")]),e._v(" "),o("p",[e._v('Spring Cloud Zookeeper uses Apache Curator behind the scenes.\nWhile Zookeeper 3.5.x is still considered "beta" by the Zookeeper development team,\nthe reality is that it is used in production by many users.\nHowever, Zookeeper 3.4.x is also used in production.\nPrior to Apache Curator 4.0, both versions of Zookeeper were supported via two versions of Apache Curator.\nStarting with Curator 4.0 both versions of Zookeeper are supported via the same Curator libraries.')]),e._v(" "),o("p",[e._v("In case you are integrating with version 3.4 you need to change the Zookeeper dependency\nthat comes shipped with "),o("code",[e._v("curator")]),e._v(", and thus "),o("code",[e._v("spring-cloud-zookeeper")]),e._v(".\nTo do so simply exclude that dependency and add the 3.4.x version like shown below.")]),e._v(" "),o("p",[e._v("maven")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("\n org.springframework.cloud\n spring-cloud-starter-zookeeper-all\n \n \n org.apache.zookeeper\n zookeeper\n \n \n\n\n org.apache.zookeeper\n zookeeper\n 3.4.12\n \n \n org.slf4j\n slf4j-log4j12\n \n \n\n")])])]),o("p",[e._v("gradle")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("compile('org.springframework.cloud:spring-cloud-starter-zookeeper-all') {\n exclude group: 'org.apache.zookeeper', module: 'zookeeper'\n}\ncompile('org.apache.zookeeper:zookeeper:3.4.12') {\n exclude group: 'org.slf4j', module: 'slf4j-log4j12'\n}\n")])])]),o("h2",{attrs:{id:"_3-service-discovery-with-zookeeper"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_3-service-discovery-with-zookeeper"}},[e._v("#")]),e._v(" 3. Service Discovery with Zookeeper")]),e._v(" "),o("p",[e._v("Service Discovery is one of the key tenets of a microservice based architecture. Trying to\nhand-configure each client or some form of convention can be difficult to do and can be\nbrittle. "),o("a",{attrs:{href:"https://curator.apache.org",target:"_blank",rel:"noopener noreferrer"}},[e._v("Curator"),o("OutboundLink")],1),e._v("(A Java library for Zookeeper) provides Service\nDiscovery through a "),o("a",{attrs:{href:"https://curator.apache.org/curator-x-discovery/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Service Discovery\nExtension"),o("OutboundLink")],1),e._v(". Spring Cloud Zookeeper uses this extension for service registration and\ndiscovery.")]),e._v(" "),o("h3",{attrs:{id:"_3-1-activating"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_3-1-activating"}},[e._v("#")]),e._v(" 3.1. Activating")]),e._v(" "),o("p",[e._v("Including a dependency on"),o("code",[e._v("org.springframework.cloud:spring-cloud-starter-zookeeper-discovery")]),e._v(" enables\nautoconfiguration that sets up Spring Cloud Zookeeper Discovery.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("For web functionality, you still need to include"),o("code",[e._v("org.springframework.boot:spring-boot-starter-web")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("When working with version 3.4 of Zookeeper you need to change"),o("br"),e._v("the way you include the dependency as described "),o("a",{attrs:{href:"#spring-cloud-zookeeper-install"}},[e._v("here")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h3",{attrs:{id:"_3-2-registering-with-zookeeper"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-registering-with-zookeeper"}},[e._v("#")]),e._v(" 3.2. Registering with Zookeeper")]),e._v(" "),o("p",[e._v("When a client registers with Zookeeper, it provides metadata (such as host and port, ID,\nand name) about itself.")]),e._v(" "),o("p",[e._v("The following example shows a Zookeeper client:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@SpringBootApplication\n@RestController\npublic class Application {\n\n @RequestMapping("/")\n public String home() {\n return "Hello world";\n }\n\n public static void main(String[] args) {\n new SpringApplicationBuilder(Application.class).web(true).run(args);\n }\n\n}\n')])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("The preceding example is a normal Spring Boot application.")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("If Zookeeper is located somewhere other than "),o("code",[e._v("localhost:2181")]),e._v(", the configuration must\nprovide the location of the server, as shown in the following example:")]),e._v(" "),o("p",[e._v("application.yml")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring:\n cloud:\n zookeeper:\n connect-string: localhost:2181\n")])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("If you use "),o("a",{attrs:{href:"#spring-cloud-zookeeper-config"}},[e._v("Spring Cloud Zookeeper Config")]),e._v(", the"),o("br"),e._v("values shown in the preceding example need to be in "),o("code",[e._v("bootstrap.yml")]),e._v(" instead of"),o("code",[e._v("application.yml")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("p",[e._v("The default service name, instance ID, and port (taken from the "),o("code",[e._v("Environment")]),e._v(") are"),o("code",[e._v("${spring.application.name}")]),e._v(", the Spring Context ID, and "),o("code",[e._v("${server.port}")]),e._v(", respectively.")]),e._v(" "),o("p",[e._v("Having "),o("code",[e._v("spring-cloud-starter-zookeeper-discovery")]),e._v(" on the classpath makes the app into both\na Zookeeper “service” (that is, it registers itself) and a “client” (that is, it can\nquery Zookeeper to locate other services).")]),e._v(" "),o("p",[e._v("If you would like to disable the Zookeeper Discovery Client, you can set"),o("code",[e._v("spring.cloud.zookeeper.discovery.enabled")]),e._v(" to "),o("code",[e._v("false")]),e._v(".")]),e._v(" "),o("h3",{attrs:{id:"_3-3-using-the-discoveryclient"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_3-3-using-the-discoveryclient"}},[e._v("#")]),e._v(" 3.3. Using the DiscoveryClient")]),e._v(" "),o("p",[e._v("Spring Cloud has support for"),o("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#spring-cloud-feign",target:"_blank",rel:"noopener noreferrer"}},[e._v("Feign"),o("OutboundLink")],1),e._v("(a REST client builder),"),o("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/ascii",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring"),o("code",[e._v("RestTemplate")]),o("OutboundLink")],1),e._v(" and"),o("a",{attrs:{href:"https://cloud.spring.io/spring-cloud-commons/reference/html/#loadbalanced-webclient",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring WebFlux"),o("OutboundLink")],1),e._v(", using logical service names instead of physical URLs.")]),e._v(" "),o("p",[e._v("You can also use the "),o("code",[e._v("org.springframework.cloud.client.discovery.DiscoveryClient")]),e._v(", which\nprovides a simple API for discovery clients that is not specific to Netflix, as shown in\nthe following example:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@Autowired\nprivate DiscoveryClient discoveryClient;\n\npublic String serviceUrl() {\n List list = discoveryClient.getInstances("STORES");\n if (list != null && list.size() > 0 ) {\n return list.get(0).getUri().toString();\n }\n return null;\n}\n')])])]),o("h2",{attrs:{id:"_4-using-spring-cloud-zookeeper-with-spring-cloud-components"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_4-using-spring-cloud-zookeeper-with-spring-cloud-components"}},[e._v("#")]),e._v(" 4. Using Spring Cloud Zookeeper with Spring Cloud Components")]),e._v(" "),o("p",[e._v("Feign, Spring Cloud Gateway and Spring Cloud LoadBalancer all work with Spring Cloud Zookeeper.")]),e._v(" "),o("h3",{attrs:{id:"_4-1-spring-cloud-loadbalancer-with-zookeeper"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_4-1-spring-cloud-loadbalancer-with-zookeeper"}},[e._v("#")]),e._v(" 4.1. Spring Cloud LoadBalancer with Zookeeper")]),e._v(" "),o("p",[e._v("Spring Cloud Zookeeper provides an implementation of Spring Cloud LoadBalancer "),o("code",[e._v("ServiceInstanceListSupplier")]),e._v(".\nWhen you use the "),o("code",[e._v("spring-cloud-starter-zookeeper-discovery")]),e._v(", Spring Cloud LoadBalancer is autoconfigured to use the"),o("code",[e._v("ZookeeperServiceInstanceListSupplier")]),e._v(" by default.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("If you were previously using the StickyRule in Zookeeper, its replacement in the current stack"),o("br"),e._v("is the "),o("code",[e._v("SameInstancePreferenceServiceInstanceListSupplier")]),e._v(" in SC LoadBalancer. You can read on how to set it up in the "),o("a",{attrs:{href:"https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#spring-cloud-loadbalancer",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Cloud Commons documentation"),o("OutboundLink")],1),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h2",{attrs:{id:"_5-spring-cloud-zookeeper-and-service-registry"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_5-spring-cloud-zookeeper-and-service-registry"}},[e._v("#")]),e._v(" 5. Spring Cloud Zookeeper and Service Registry")]),e._v(" "),o("p",[e._v("Spring Cloud Zookeeper implements the "),o("code",[e._v("ServiceRegistry")]),e._v(" interface, letting developers\nregister arbitrary services in a programmatic way.")]),e._v(" "),o("p",[e._v("The "),o("code",[e._v("ServiceInstanceRegistration")]),e._v(" class offers a "),o("code",[e._v("builder()")]),e._v(" method to create a"),o("code",[e._v("Registration")]),e._v(" object that can be used by the "),o("code",[e._v("ServiceRegistry")]),e._v(", as shown in the following\nexample:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@Autowired\nprivate ZookeeperServiceRegistry serviceRegistry;\n\npublic void registerThings() {\n ZookeeperRegistration registration = ServiceInstanceRegistration.builder()\n .defaultUriSpec()\n .address("anyUrl")\n .port(10)\n .name("/a/b/c/d/anotherservice")\n .build();\n this.serviceRegistry.register(registration);\n}\n')])])]),o("h3",{attrs:{id:"_5-1-instance-status"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_5-1-instance-status"}},[e._v("#")]),e._v(" 5.1. Instance Status")]),e._v(" "),o("p",[e._v("Netflix Eureka supports having instances that are "),o("code",[e._v("OUT_OF_SERVICE")]),e._v(" registered with the server.\nThese instances are not returned as active service instances.\nThis is useful for behaviors such as blue/green deployments.\n(Note that the Curator Service Discovery recipe does not support this behavior.) Taking advantage of the flexible payload has let Spring Cloud Zookeeper implement "),o("code",[e._v("OUT_OF_SERVICE")]),e._v(" by updating some specific metadata and then filtering on that metadata in the Spring Cloud LoadBalancer "),o("code",[e._v("ZookeeperServiceInstanceListSupplier")]),e._v(".\nThe "),o("code",[e._v("ZookeeperServiceInstanceListSupplier")]),e._v(" filters out all non-null instance statuses that do not equal "),o("code",[e._v("UP")]),e._v(".\nIf the instance status field is empty, it is considered to be "),o("code",[e._v("UP")]),e._v(" for backwards compatibility.\nTo change the status of an instance, make a "),o("code",[e._v("POST")]),e._v(" with "),o("code",[e._v("OUT_OF_SERVICE")]),e._v(" to the "),o("code",[e._v("ServiceRegistry")]),e._v("instance status actuator endpoint, as shown in the following example:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("$ http POST http://localhost:8081/service-registry status=OUT_OF_SERVICE\n")])])]),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("The preceding example uses the "),o("code",[e._v("http")]),e._v(" command from "),o("a",{attrs:{href:"https://httpie.org",target:"_blank",rel:"noopener noreferrer"}},[e._v("httpie.org"),o("OutboundLink")],1),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h2",{attrs:{id:"_6-zookeeper-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-zookeeper-dependencies"}},[e._v("#")]),e._v(" 6. Zookeeper Dependencies")]),e._v(" "),o("p",[e._v("The following topics cover how to work with Spring Cloud Zookeeper dependencies:")]),e._v(" "),o("ul",[o("li",[o("p",[o("a",{attrs:{href:"#spring-cloud-zookeeper-dependencies-using"}},[e._v("Using the Zookeeper Dependencies")])])]),e._v(" "),o("li",[o("p",[o("a",{attrs:{href:"#spring-cloud-zookeeper-dependencies-activating"}},[e._v("Activating Zookeeper Dependencies")])])]),e._v(" "),o("li",[o("p",[o("a",{attrs:{href:"#spring-cloud-zookeeper-dependencies-setting-up"}},[e._v("Setting up Zookeeper Dependencies")])])]),e._v(" "),o("li",[o("p",[o("a",{attrs:{href:"#spring-cloud-zookeeper-dependencies-configuring"}},[e._v("Configuring Spring Cloud Zookeeper Dependencies")])])])]),e._v(" "),o("h3",{attrs:{id:"_6-1-using-the-zookeeper-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-1-using-the-zookeeper-dependencies"}},[e._v("#")]),e._v(" 6.1. Using the Zookeeper Dependencies")]),e._v(" "),o("p",[e._v("Spring Cloud Zookeeper gives you a possibility to provide dependencies of your application\nas properties. As dependencies, you can understand other applications that are registered\nin Zookeeper and which you would like to call through"),o("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#spring-cloud-feign",target:"_blank",rel:"noopener noreferrer"}},[e._v("Feign"),o("OutboundLink")],1),e._v("(a REST client builder),"),o("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/ascii",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring"),o("code",[e._v("RestTemplate")]),o("OutboundLink")],1),e._v(" and"),o("a",{attrs:{href:"https://cloud.spring.io/spring-cloud-commons/reference/html/#loadbalanced-webclient",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring WebFlux"),o("OutboundLink")],1),e._v(".")]),e._v(" "),o("p",[e._v("You can also use the Zookeeper Dependency Watchers functionality to control and monitor\nthe state of your dependencies.")]),e._v(" "),o("h3",{attrs:{id:"_6-2-activating-zookeeper-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-2-activating-zookeeper-dependencies"}},[e._v("#")]),e._v(" 6.2. Activating Zookeeper Dependencies")]),e._v(" "),o("p",[e._v("Including a dependency on"),o("code",[e._v("org.springframework.cloud:spring-cloud-starter-zookeeper-discovery")]),e._v(" enables\nautoconfiguration that sets up Spring Cloud Zookeeper Dependencies. Even if you provide\nthe dependencies in your properties, you can turn off the dependencies. To do so, set the"),o("code",[e._v("spring.cloud.zookeeper.dependency.enabled")]),e._v(" property to false (it defaults to "),o("code",[e._v("true")]),e._v(").")]),e._v(" "),o("h3",{attrs:{id:"_6-3-setting-up-zookeeper-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-setting-up-zookeeper-dependencies"}},[e._v("#")]),e._v(" 6.3. Setting up Zookeeper Dependencies")]),e._v(" "),o("p",[e._v("Consider the following example of dependency representation:")]),e._v(" "),o("p",[e._v("application.yml")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring.application.name: yourServiceName\nspring.cloud.zookeeper:\n dependencies:\n newsletter:\n path: /path/where/newsletter/has/registered/in/zookeeper\n loadBalancerType: ROUND_ROBIN\n contentTypeTemplate: application/vnd.newsletter.$version+json\n version: v1\n headers:\n header1:\n - value1\n header2:\n - value2\n required: false\n stubs: org.springframework:foo:stubs\n mailing:\n path: /path/where/mailing/has/registered/in/zookeeper\n loadBalancerType: ROUND_ROBIN\n contentTypeTemplate: application/vnd.mailing.$version+json\n version: v1\n required: true\n")])])]),o("p",[e._v("The next few sections go through each part of the dependency one by one. The root property\nname is "),o("code",[e._v("spring.cloud.zookeeper.dependencies")]),e._v(".")]),e._v(" "),o("h4",{attrs:{id:"_6-3-1-aliases"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-1-aliases"}},[e._v("#")]),e._v(" 6.3.1. Aliases")]),e._v(" "),o("p",[e._v("Below the root property you have to represent each dependency as an alias.\nThis is due to the constraints of Spring Cloud LoadBalancer, which requires that the application ID be placed in the URL.\nConsequently, you cannot pass any complex path, suchas "),o("code",[e._v("/myApp/myRoute/name")]),e._v(").\nThe alias is the name you use instead of the "),o("code",[e._v("serviceId")]),e._v(" for "),o("code",[e._v("DiscoveryClient")]),e._v(", "),o("code",[e._v("Feign")]),e._v(", or"),o("code",[e._v("RestTemplate")]),e._v(".")]),e._v(" "),o("p",[e._v("In the previous examples, the aliases are "),o("code",[e._v("newsletter")]),e._v(" and "),o("code",[e._v("mailing")]),e._v(".\nThe following example shows Feign usage with a "),o("code",[e._v("newsletter")]),e._v(" alias:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@FeignClient("newsletter")\npublic interface NewsletterService {\n @RequestMapping(method = RequestMethod.GET, value = "/newsletter")\n String getNewsletters();\n}\n')])])]),o("h4",{attrs:{id:"_6-3-2-path"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-2-path"}},[e._v("#")]),e._v(" 6.3.2. Path")]),e._v(" "),o("p",[e._v("The path is represented by the "),o("code",[e._v("path")]),e._v(" YAML property and is the path under which the dependency is registered under Zookeeper.\nAs described in the"),o("a",{attrs:{href:"#spring-cloud-zookeeper-dependencies-setting-up-aliases"}},[e._v("previous section")]),e._v(", Spring Cloud LoadBalancer operates on URLs.\nAs a result, this path is not compliant with its requirement.\nThat is why Spring Cloud Zookeeper maps the alias to the proper path.")]),e._v(" "),o("h4",{attrs:{id:"_6-3-3-load-balancer-type"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-3-load-balancer-type"}},[e._v("#")]),e._v(" 6.3.3. Load Balancer Type")]),e._v(" "),o("p",[e._v("The load balancer type is represented by "),o("code",[e._v("loadBalancerType")]),e._v(" YAML property.")]),e._v(" "),o("p",[e._v("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.\nYou can choose one of the following load balancing strategies:")]),e._v(" "),o("ul",[o("li",[o("p",[e._v("STICKY: Once chosen, the instance is always called.")])]),e._v(" "),o("li",[o("p",[e._v("RANDOM: Picks an instance randomly.")])]),e._v(" "),o("li",[o("p",[e._v("ROUND_ROBIN: Iterates over instances over and over again.")])])]),e._v(" "),o("h4",{attrs:{id:"_6-3-4-content-type-template-and-version"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-4-content-type-template-and-version"}},[e._v("#")]),e._v(" 6.3.4. "),o("code",[e._v("Content-Type")]),e._v(" Template and Version")]),e._v(" "),o("p",[e._v("The "),o("code",[e._v("Content-Type")]),e._v(" template and version are represented by the "),o("code",[e._v("contentTypeTemplate")]),e._v(" and"),o("code",[e._v("version")]),e._v(" YAML properties.")]),e._v(" "),o("p",[e._v("If you version your API in the "),o("code",[e._v("Content-Type")]),e._v(" header, you do not want to add this header\nto each of your requests. Also, if you want to call a new version of the API, you do not\nwant to roam around your code to bump up the API version. That is why you can provide a"),o("code",[e._v("contentTypeTemplate")]),e._v(" with a special "),o("code",[e._v("$version")]),e._v(" placeholder. That placeholder will be filled by the value of the"),o("code",[e._v("version")]),e._v(" YAML property. Consider the following example of a "),o("code",[e._v("contentTypeTemplate")]),e._v(":")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("application/vnd.newsletter.$version+json\n")])])]),o("p",[e._v("Further consider the following "),o("code",[e._v("version")]),e._v(":")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("v1\n")])])]),o("p",[e._v("The combination of "),o("code",[e._v("contentTypeTemplate")]),e._v(" and version results in the creation of a"),o("code",[e._v("Content-Type")]),e._v(" header for each request, as follows:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("application/vnd.newsletter.v1+json\n")])])]),o("h4",{attrs:{id:"_6-3-5-default-headers"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-5-default-headers"}},[e._v("#")]),e._v(" 6.3.5. Default Headers")]),e._v(" "),o("p",[e._v("Default headers are represented by the "),o("code",[e._v("headers")]),e._v(" map in YAML.")]),e._v(" "),o("p",[e._v("Sometimes, each call to a dependency requires setting up of some default headers. To not\ndo that in code, you can set them up in the YAML file, as shown in the following example"),o("code",[e._v("headers")]),e._v(" section:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("headers:\n Accept:\n - text/html\n - application/xhtml+xml\n Cache-Control:\n - no-cache\n")])])]),o("p",[e._v("That "),o("code",[e._v("headers")]),e._v(" section results in adding the "),o("code",[e._v("Accept")]),e._v(" and "),o("code",[e._v("Cache-Control")]),e._v(" headers with\nappropriate list of values in your HTTP request.")]),e._v(" "),o("h4",{attrs:{id:"_6-3-6-required-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-6-required-dependencies"}},[e._v("#")]),e._v(" 6.3.6. Required Dependencies")]),e._v(" "),o("p",[e._v("Required dependencies are represented by "),o("code",[e._v("required")]),e._v(" property in YAML.")]),e._v(" "),o("p",[e._v("If one of your dependencies is required to be up when your application boots, you can set\nthe "),o("code",[e._v("required: true")]),e._v(" property in the YAML file.")]),e._v(" "),o("p",[e._v("If your application cannot localize the required dependency during boot time, it throws an\nexception, and the Spring Context fails to set up. In other words, your application cannot\nstart if the required dependency is not registered in Zookeeper.")]),e._v(" "),o("p",[e._v("You can read more about Spring Cloud Zookeeper Presence Checker"),o("a",{attrs:{href:"#spring-cloud-zookeeper-dependency-watcher-presence-checker"}},[e._v("later in this document")]),e._v(".")]),e._v(" "),o("h4",{attrs:{id:"_6-3-7-stubs"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-3-7-stubs"}},[e._v("#")]),e._v(" 6.3.7. Stubs")]),e._v(" "),o("p",[e._v("You can provide a colon-separated path to the JAR containing stubs of the dependency, as\nshown in the following example:")]),e._v(" "),o("p",[o("code",[e._v("stubs: org.springframework:myApp:stubs")])]),e._v(" "),o("p",[e._v("where:")]),e._v(" "),o("ul",[o("li",[o("p",[o("code",[e._v("org.springframework")]),e._v(" is the "),o("code",[e._v("groupId")]),e._v(".")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("myApp")]),e._v(" is the "),o("code",[e._v("artifactId")]),e._v(".")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("stubs")]),e._v(" is the classifier. (Note that "),o("code",[e._v("stubs")]),e._v(" is the default value.)")])])]),e._v(" "),o("p",[e._v("Because "),o("code",[e._v("stubs")]),e._v(" is the default classifier, the preceding example is equal to the following\nexample:")]),e._v(" "),o("p",[o("code",[e._v("stubs: org.springframework:myApp")])]),e._v(" "),o("h3",{attrs:{id:"_6-4-configuring-spring-cloud-zookeeper-dependencies"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_6-4-configuring-spring-cloud-zookeeper-dependencies"}},[e._v("#")]),e._v(" 6.4. Configuring Spring Cloud Zookeeper Dependencies")]),e._v(" "),o("p",[e._v("You can set the following properties to enable or disable parts of Zookeeper Dependencies functionalities:")]),e._v(" "),o("ul",[o("li",[o("p",[o("code",[e._v("spring.cloud.zookeeper.dependencies")]),e._v(": If you do not set this property, you cannot use Zookeeper Dependencies.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("spring.cloud.zookeeper.dependency.loadbalancer.enabled")]),e._v(" (enabled by default): Turns on Zookeeper-specific custom load-balancing strategies, including "),o("code",[e._v("ZookeeperServiceInstanceListSupplier")]),e._v(" and dependency-based load-balanced "),o("code",[e._v("RestTemplate")]),e._v(" setup.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("spring.cloud.zookeeper.dependency.headers.enabled")]),e._v(" (enabled by default): This property registers a "),o("code",[e._v("FeignBlockingLoadBalancerClient")]),e._v(" that automatically appends appropriate headers and content types with their versions, as presented in the Dependency configuration.\nWithout this setting, those two parameters do not work.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("spring.cloud.zookeeper.dependency.resttemplate.enabled")]),e._v(" (enabled by default): When enabled, this property modifies the request headers of a "),o("code",[e._v("@LoadBalanced")]),e._v("-annotated"),o("code",[e._v("RestTemplate")]),e._v(" such that it passes headers and content type with the version set in dependency configuration.\nWithout this setting, those two parameters do not work.")])])]),e._v(" "),o("h2",{attrs:{id:"_7-spring-cloud-zookeeper-dependency-watcher"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_7-spring-cloud-zookeeper-dependency-watcher"}},[e._v("#")]),e._v(" 7. Spring Cloud Zookeeper Dependency Watcher")]),e._v(" "),o("p",[e._v("The Dependency Watcher mechanism lets you register listeners to your dependencies. The\nfunctionality is, in fact, an implementation of the "),o("code",[e._v("Observator")]),e._v(" pattern. When a\ndependency changes, its state (to either UP or DOWN), some custom logic can be applied.")]),e._v(" "),o("h3",{attrs:{id:"_7-1-activating"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_7-1-activating"}},[e._v("#")]),e._v(" 7.1. Activating")]),e._v(" "),o("p",[e._v("Spring Cloud Zookeeper Dependencies functionality needs to be enabled for you to use the\nDependency Watcher mechanism.")]),e._v(" "),o("h3",{attrs:{id:"_7-2-registering-a-listener"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_7-2-registering-a-listener"}},[e._v("#")]),e._v(" 7.2. Registering a Listener")]),e._v(" "),o("p",[e._v("To register a listener, you must implement an interface called"),o("code",[e._v("org.springframework.cloud.zookeeper.discovery.watcher.DependencyWatcherListener")]),e._v(" and\nregister it as a bean. The interface gives you one method:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("void stateChanged(String dependencyName, DependencyState newState);\n")])])]),o("p",[e._v("If you want to register a listener for a particular dependency, the "),o("code",[e._v("dependencyName")]),e._v(" would\nbe the discriminator for your concrete implementation. "),o("code",[e._v("newState")]),e._v(" provides you with\ninformation about whether your dependency has changed to "),o("code",[e._v("CONNECTED")]),e._v(" or "),o("code",[e._v("DISCONNECTED")]),e._v(".")]),e._v(" "),o("h3",{attrs:{id:"_7-3-using-the-presence-checker"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_7-3-using-the-presence-checker"}},[e._v("#")]),e._v(" 7.3. Using the Presence Checker")]),e._v(" "),o("p",[e._v("Bound with the Dependency Watcher is the functionality called Presence Checker. It lets\nyou provide custom behavior when your application boots, to react according to the state\nof your dependencies.")]),e._v(" "),o("p",[e._v("The default implementation of the abstract"),o("code",[e._v("org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier")]),e._v("class is the"),o("code",[e._v("org.springframework.cloud.zookeeper.discovery.watcher.presence.DefaultDependencyPresenceOnStartupVerifier")]),e._v(",\nwhich works in the following way.")]),e._v(" "),o("ol",[o("li",[o("p",[e._v("If the dependency is marked us "),o("code",[e._v("required")]),e._v(" and is not in Zookeeper, when your application\nboots, it throws an exception and shuts down.")])]),e._v(" "),o("li",[o("p",[e._v("If the dependency is not "),o("code",[e._v("required")]),e._v(", the"),o("code",[e._v("org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker")]),e._v("logs that the dependency is missing at the "),o("code",[e._v("WARN")]),e._v(" level.")])])]),e._v(" "),o("p",[e._v("Because the "),o("code",[e._v("DefaultDependencyPresenceOnStartupVerifier")]),e._v(" is registered only when there is\nno bean of type "),o("code",[e._v("DependencyPresenceOnStartupVerifier")]),e._v(", this functionality can be\noverridden.")]),e._v(" "),o("h2",{attrs:{id:"_8-distributed-configuration-with-zookeeper"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_8-distributed-configuration-with-zookeeper"}},[e._v("#")]),e._v(" 8. Distributed Configuration with Zookeeper")]),e._v(" "),o("p",[e._v("Zookeeper provides a"),o("a",{attrs:{href:"https://zookeeper.apache.org/doc/current/zookeeperOver.html#sc_dataModelNameSpace",target:"_blank",rel:"noopener noreferrer"}},[e._v("hierarchical namespace"),o("OutboundLink")],1),e._v("that lets clients store arbitrary data, such as configuration data. Spring Cloud Zookeeper\nConfig is an alternative to the"),o("a",{attrs:{href:"https://github.com/spring-cloud/spring-cloud-config",target:"_blank",rel:"noopener noreferrer"}},[e._v("Config Server and Client"),o("OutboundLink")],1),e._v(".\nConfiguration is loaded into the Spring Environment during the special “bootstrap”\nphase. Configuration is stored in the "),o("code",[e._v("/config")]),e._v(" namespace by default. Multiple"),o("code",[e._v("PropertySource")]),e._v(" instances are created, based on the application’s name and the active\nprofiles, to mimic the Spring Cloud Config order of resolving properties. For example, an\napplication with a name of "),o("code",[e._v("testApp")]),e._v(" and with the "),o("code",[e._v("dev")]),e._v(" profile has the following property\nsources created for it:")]),e._v(" "),o("ul",[o("li",[o("p",[o("code",[e._v("config/testApp,dev")])])]),e._v(" "),o("li",[o("p",[o("code",[e._v("config/testApp")])])]),e._v(" "),o("li",[o("p",[o("code",[e._v("config/application,dev")])])]),e._v(" "),o("li",[o("p",[o("code",[e._v("config/application")])])])]),e._v(" "),o("p",[e._v("The most specific property source is at the top, with the least specific at the bottom.\nProperties in the "),o("code",[e._v("config/application")]),e._v(" namespace apply to all applications that use\nzookeeper for configuration. Properties in the "),o("code",[e._v("config/testApp")]),e._v(" namespace are available\nonly to the instances of the service named "),o("code",[e._v("testApp")]),e._v(".")]),e._v(" "),o("p",[e._v("Configuration is currently read on startup of the application. Sending a HTTP "),o("code",[e._v("POST")]),e._v("request to "),o("code",[e._v("/refresh")]),e._v(" causes the configuration to be reloaded. Watching the configuration\nnamespace (which Zookeeper supports) is not currently implemented.")]),e._v(" "),o("h3",{attrs:{id:"_8-1-activating"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_8-1-activating"}},[e._v("#")]),e._v(" 8.1. Activating")]),e._v(" "),o("p",[e._v("Including a dependency on"),o("code",[e._v("org.springframework.cloud:spring-cloud-starter-zookeeper-config")]),e._v(" enables\nautoconfiguration that sets up Spring Cloud Zookeeper Config.")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("When working with version 3.4 of Zookeeper you need to change"),o("br"),e._v("the way you include the dependency as described "),o("a",{attrs:{href:"#spring-cloud-zookeeper-install"}},[e._v("here")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h3",{attrs:{id:"_8-2-spring-boot-config-data-import"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_8-2-spring-boot-config-data-import"}},[e._v("#")]),e._v(" 8.2. Spring Boot Config Data Import")]),e._v(" "),o("p",[e._v("Spring Boot 2.4 introduced a new way to import configuration data via the "),o("code",[e._v("spring.config.import")]),e._v(" property. This is now the default way to get configuration from Zookeeper.")]),e._v(" "),o("p",[e._v("To optionally connect to Zookeeper for configuration set the following in application.properties:")]),e._v(" "),o("p",[e._v("application.properties")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring.config.import=optional:zookeeper:\n")])])]),o("p",[e._v('This will connect to Zookeeper at the default location of "localhost:2181". Removing the '),o("code",[e._v("optional:")]),e._v(" prefix will cause Zookeeper Config to fail if it is unable to connect to Zookeeper. To change the connection properties of Zookeeper Config either set "),o("code",[e._v("spring.cloud.zookeeper.connect-string")]),e._v(" or add the connect string to the "),o("code",[e._v("spring.config.import")]),e._v(" statement such as, "),o("code",[e._v("spring.config.import=optional:zookeeper:myhost:2818")]),e._v(". The location in the import property has precedence over the "),o("code",[e._v("connect-string")]),e._v(" property.")]),e._v(" "),o("p",[e._v("Zookeeper Config will try to load values from four automatic contexts based on "),o("code",[e._v("spring.cloud.zookeeper.config.name")]),e._v(" (which defaults to the value of the "),o("code",[e._v("spring.application.name")]),e._v(" property) and "),o("code",[e._v("spring.cloud.zookeeper.config.default-context")]),e._v(" (which defaults to "),o("code",[e._v("application")]),e._v("). If you want to specify the contexts rather than using the computed ones, you can add that information to the "),o("code",[e._v("spring.config.import")]),e._v(" statement.")]),e._v(" "),o("p",[e._v("application.properties")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring.config.import=optional:zookeeper:myhost:2181/contextone;/context/two\n")])])]),o("p",[e._v("This will optionally load configuration only from "),o("code",[e._v("/contextone")]),e._v(" and "),o("code",[e._v("/context/two")]),e._v(".")]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("A "),o("code",[e._v("bootstrap")]),e._v(" file (properties or yaml) is "),o("strong",[e._v("not")]),e._v(" needed for the Spring Boot Config Data method of import via "),o("code",[e._v("spring.config.import")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h3",{attrs:{id:"_8-3-customizing"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_8-3-customizing"}},[e._v("#")]),e._v(" 8.3. Customizing")]),e._v(" "),o("p",[e._v("Zookeeper Config may be customized by setting the following properties:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("spring:\n cloud:\n zookeeper:\n config:\n enabled: true\n root: configuration\n defaultContext: apps\n profileSeparator: '::'\n")])])]),o("ul",[o("li",[o("p",[o("code",[e._v("enabled")]),e._v(": Setting this value to "),o("code",[e._v("false")]),e._v(" disables Zookeeper Config.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("root")]),e._v(": Sets the base namespace for configuration values.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("defaultContext")]),e._v(": Sets the name used by all applications.")])]),e._v(" "),o("li",[o("p",[o("code",[e._v("profileSeparator")]),e._v(": Sets the value of the separator used to separate the profile name in\nproperty sources with profiles.")])])]),e._v(" "),o("table",[o("thead",[o("tr",[o("th"),e._v(" "),o("th",[e._v("If you have set "),o("code",[e._v("spring.cloud.bootstrap.enabled=true")]),e._v(" or "),o("code",[e._v("spring.config.use-legacy-processing=true")]),e._v(", or included "),o("code",[e._v("spring-cloud-starter-bootstrap")]),e._v(", then the above values will need to be placed in "),o("code",[e._v("bootstrap.yml")]),e._v(" instead of "),o("code",[e._v("application.yml")]),e._v(".")])])]),e._v(" "),o("tbody")]),e._v(" "),o("h3",{attrs:{id:"_8-4-access-control-lists-acls"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#_8-4-access-control-lists-acls"}},[e._v("#")]),e._v(" 8.4. Access Control Lists (ACLs)")]),e._v(" "),o("p",[e._v("You can add authentication information for Zookeeper ACLs by calling the "),o("code",[e._v("addAuthInfo")]),e._v("method of a "),o("code",[e._v("CuratorFramework")]),e._v(" bean. One way to accomplish this is to provide your own"),o("code",[e._v("CuratorFramework")]),e._v(" bean, as shown in the following example:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@BoostrapConfiguration\npublic class CustomCuratorFrameworkConfig {\n\n @Bean\n public CuratorFramework curatorFramework() {\n CuratorFramework curator = new CuratorFramework();\n curator.addAuthInfo("digest", "user:password".getBytes());\n return curator;\n }\n\n}\n')])])]),o("p",[e._v("Consult"),o("a",{attrs:{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:"_blank",rel:"noopener noreferrer"}},[e._v("the ZookeeperAutoConfiguration class"),o("OutboundLink")],1),e._v("to see how the "),o("code",[e._v("CuratorFramework")]),e._v(" bean’s default configuration.")]),e._v(" "),o("p",[e._v("Alternatively, you can add your credentials from a class that depends on the existing"),o("code",[e._v("CuratorFramework")]),e._v(" bean, as shown in the following example:")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('@BoostrapConfiguration\npublic class DefaultCuratorFrameworkConfig {\n\n public ZookeeperConfig(CuratorFramework curator) {\n curator.addAuthInfo("digest", "user:password".getBytes());\n }\n\n}\n')])])]),o("p",[e._v("The creation of this bean must occur during the boostrapping phase. You can register\nconfiguration classes to run during this phase by annotating them with"),o("code",[e._v("@BootstrapConfiguration")]),e._v(" and including them in a comma-separated list that you set as the\nvalue of the "),o("code",[e._v("org.springframework.cloud.bootstrap.BootstrapConfiguration")]),e._v(" property in the"),o("code",[e._v("resources/META-INF/spring.factories")]),e._v(" file, as shown in the following example:")]),e._v(" "),o("p",[e._v("resources/META-INF/spring.factories")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("org.springframework.cloud.bootstrap.BootstrapConfiguration=\\\nmy.project.CustomCuratorFrameworkConfig,\\\nmy.project.DefaultCuratorFrameworkConfig\n")])])])])}),[],!1,null,null,null);t.default=r.exports}}]);