6. Zookeeper 依赖项

以下主题介绍如何使用 Spring Cloud Zookeeper 依赖项:spring-doc.cadn.net.cn

6.1. 使用 Zookeeper 依赖项

Spring Cloud Zookeeper 使您能够提供应用程序的依赖项 作为属性。作为依赖项,您可以了解已注册的其他应用程序 在 Zookeeper 中,并且您希望通过 Feign(REST 客户端构建器)调用,SpringRestTemplateSpring WebFlux 的 Web Flux 进行更新。spring-doc.cadn.net.cn

您还可以使用 Zookeeper Dependency Watchers 功能来控制和监控 依赖项的状态。spring-doc.cadn.net.cn

6.2. 激活 Zookeeper 依赖项

包括 对org.springframework.cloud:spring-cloud-starter-zookeeper-discovery使 auto配置来设置 Spring Cloud Zookeeper 依赖项。即使您提供 dependencies 中,您可以关闭依赖项。为此,请将spring.cloud.zookeeper.dependency.enabled属性设置为 false(默认为true).spring-doc.cadn.net.cn

6.3. 设置 Zookeeper 依赖项

请考虑以下依赖项表示示例:spring-doc.cadn.net.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

接下来的几节将逐一介绍依赖项的每个部分。root 属性 name 为spring.cloud.zookeeper.dependencies.spring-doc.cadn.net.cn

6.3.1. 别名

在 root 属性下,您必须将每个依赖项表示为别名。 这是由于 Spring Cloud LoadBalancer 的约束,它要求将应用程序 ID 放在 URL 中。 因此,您不能传递任何复杂路径,例如/myApp/myRoute/name). 别名是您使用的名称,而不是serviceIdDiscoveryClient,FeignRestTemplate.spring-doc.cadn.net.cn

在前面的示例中,别名是newslettermailing. 以下示例显示了newsletter别名:spring-doc.cadn.net.cn

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

6.3.2. 路径

该路径由pathYAML 属性,并且是在 Zookeeper 下注册依赖项的路径。 如上一节所述,Spring Cloud LoadBalancer 对 URL 进行作。 因此,此路径不符合其要求。 这就是 Spring Cloud Zookeeper 将别名映射到正确路径的原因。spring-doc.cadn.net.cn

6.3.3. 负载均衡器类型

负载均衡器类型由loadBalancerTypeYAML 属性。spring-doc.cadn.net.cn

如果您知道在调用此特定依赖项时必须应用哪种负载平衡策略,则可以在 YAML 文件中提供该策略,并且它会自动应用。 您可以选择以下负载均衡策略之一:spring-doc.cadn.net.cn

6.3.4.Content-Type模板和版本

Content-Typetemplate 和 version 由contentTypeTemplateversionYAML 属性。spring-doc.cadn.net.cn

如果您在 API 的Content-Typeheader,则您不想添加此 header 添加到您的每个请求中。此外,如果要调用新版本的 API,则不要 想要在代码中漫游以提高 API 版本。这就是为什么您可以提供contentTypeTemplate带有特殊的$version占 位 符。该占位符将由versionYAML 属性。请考虑以下示例中的contentTypeTemplate:spring-doc.cadn.net.cn

application/vnd.newsletter.$version+json

进一步考虑以下内容version:spring-doc.cadn.net.cn

v1

的组合contentTypeTemplate和 version 会导致创建一个Content-Type标头,如下所示:spring-doc.cadn.net.cn

application/vnd.newsletter.v1+json

6.3.5. 默认 Headers

默认标头由headersmap 中。spring-doc.cadn.net.cn

有时,每次调用依赖项都需要设置一些默认标头。要不 在代码中执行此作,您可以在 YAML 文件中设置它们,如以下示例所示headers部分:spring-doc.cadn.net.cn

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

headers部分会导致添加AcceptCache-Control标头替换为 HTTP 请求中的相应值列表。spring-doc.cadn.net.cn

6.3.6. 必需的依赖项

必需的依赖项由required属性。spring-doc.cadn.net.cn

如果在应用程序启动时需要启动其中一个依赖项,则可以设置 这required: true属性。spring-doc.cadn.net.cn

如果您的应用程序在引导期间无法本地化所需的依赖项,则会抛出 异常,并且 Spring Context 设置失败。换句话说,您的应用程序不能 如果所需的依赖项未在 Zookeeper 中注册,则启动。spring-doc.cadn.net.cn

您可以在本文档后面阅读有关 Spring Cloud Zookeeper Presence Checker 的更多信息。spring-doc.cadn.net.cn

6.3.7. 存根

您可以提供包含依赖项存根的 JAR 的冒号分隔路径,如 如以下示例所示:spring-doc.cadn.net.cn

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

因为stubs是默认分类器,则前面的示例等于以下内容 例:spring-doc.cadn.net.cn

stubs: org.springframework:myAppspring-doc.cadn.net.cn

6.4. 配置 Spring Cloud Zookeeper 依赖项

您可以设置以下属性来启用或禁用 Zookeeper 依赖项功能的某些部分:spring-doc.cadn.net.cn

  • spring.cloud.zookeeper.dependencies:如果不设置此属性,则无法使用 Zookeeper 依赖项。spring-doc.cadn.net.cn

  • spring.cloud.zookeeper.dependency.loadbalancer.enabled(默认启用):开启特定于 Zookeeper 的自定义负载均衡策略,包括ZookeeperServiceInstanceListSupplier和基于依赖关系的负载均衡RestTemplate设置。spring-doc.cadn.net.cn

  • spring.cloud.zookeeper.dependency.headers.enabled(默认启用):此属性注册一个FeignBlockingLoadBalancerClient它会自动将适当的标头和内容类型附加到其版本,如 Dependency 配置中所示。 如果没有此设置,这两个参数将不起作用。spring-doc.cadn.net.cn

  • spring.cloud.zookeeper.dependency.resttemplate.enabled(默认启用):启用后,此属性会修改@LoadBalanced-注释RestTemplate这样,它就可以传递 Headers 和 Content Type 以及 Dependency Configuration 中设置的 version。 如果没有此设置,这两个参数将不起作用。spring-doc.cadn.net.cn