Zookeeper 依赖

使用 Zookeeper 依赖

Spring Cloud Zookeeper 使您能够将应用程序的依赖作为属性提供。作为依赖,您可以理解为在 Zookeeper 中注册的其他应用程序,您希望通过 OpenFeign(一个 REST 客户端构建器)、RestTemplateWebClient 经由 Spring Cloud Loadbalancer 调用这些应用程序。

您还可以使用 Zookeeper 依赖监视器功能来控制和监控依赖的状态。

激活 Zookeeper 依赖

包含对 org.springframework.cloud:spring-cloud-starter-zookeeper-discovery 的依赖项会启用自动配置,该配置会设置 Spring Cloud Zookeeper 依赖项。即使您在属性中提供了依赖项,您也可以关闭这些依赖项。要做到这一点,请将 spring.cloud.zookeeper.dependency.enabled 属性设置为 false(它默认为 true)。

设置 Zookeeper 依赖

考虑以下依赖表示示例

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

接下来的几节将逐一介绍依赖的每个部分。根属性名为 spring.cloud.zookeeper.dependencies

别名

在根属性下面,您必须将每个依赖表示为别名。这是由于 Spring Cloud LoadBalancer 的限制,它要求将应用程序 ID 放在 URL 中。因此,您不能传递任何复杂的路径,例如 /myApp/myRoute/name)。别名是您代替 serviceId 用于 DiscoveryClientFeignRestTemplate 的名称。

在前面的示例中,别名是 newslettermailing。以下示例展示了使用 newsletter 别名的 Feign 用法

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

路径

路径由 path YAML 属性表示,是依赖项在 Zookeeper 下注册的路径。如上一节所述,Spring Cloud LoadBalancer 操作 URL。因此,此路径不符合其要求。这就是 Spring Cloud Zookeeper 将别名映射到正确路径的原因。

负载均衡器类型

负载均衡器类型由 loadBalancerType YAML 属性表示。

如果您知道在调用此特定依赖项时必须应用哪种负载平衡策略,您可以在 YAML 文件中提供它,它会自动应用。您可以选择以下负载平衡策略之一

  • STICKY:一旦选择,实例将始终被调用。

  • RANDOM:随机选择一个实例。

  • ROUND_ROBIN:循环迭代实例。

Content-Type 模板和版本

Content-Type 模板和版本由 contentTypeTemplateversion YAML 属性表示。

如果您在 Content-Type 标头中对 API 进行版本控制,您不希望将此标头添加到每个请求中。此外,如果您想调用新版本的 API,您也不希望在代码中四处寻找以提升 API 版本。这就是您可以提供一个带有特殊 $version 占位符的 contentTypeTemplate 的原因。该占位符将由 version YAML 属性的值填充。考虑以下 contentTypeTemplate 示例

application/vnd.newsletter.$version+json

进一步考虑以下 version

v1

contentTypeTemplate 和版本的组合导致为每个请求创建 Content-Type 标头,如下所示

application/vnd.newsletter.v1+json

默认标头

默认标头在 YAML 中由 headers 映射表示。

有时,对依赖项的每次调用都需要设置一些默认标头。为了不在代码中执行此操作,您可以在 YAML 文件中设置它们,如以下 headers 部分示例所示

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

headers 部分将导致在您的 HTTP 请求中添加带有适当值列表的 AcceptCache-Control 标头。

必需的依赖项

所需的依赖项在 YAML 中由 required 属性表示。

如果您的应用程序启动时需要其中一个依赖项正常运行,您可以在 YAML 文件中设置 required: true 属性。

如果您的应用程序在启动时无法定位所需的依赖项,它会抛出异常,并且 Spring Context 无法设置。换句话说,如果所需的依赖项未在 Zookeeper 中注册,您的应用程序将无法启动。

您可以在本文档后面阅读更多关于 Spring Cloud Zookeeper 存在检查器的信息。

存根

您可以提供一个冒号分隔的路径,指向包含依赖项存根的 JAR 包,如以下示例所示

存根: org.springframework:myApp:stubs

其中:

  • org.springframeworkgroupId

  • myAppartifactId

  • stubs 是分类器。(注意 stubs 是默认值。)

因为 stubs 是默认分类器,所以前面的示例等同于以下示例

存根: org.springframework:myApp

配置 Spring Cloud Zookeeper 依赖

您可以设置以下属性来启用或禁用 Zookeeper 依赖项功能的某些部分

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

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

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

  • spring.cloud.zookeeper.dependency.resttemplate.enabled(默认启用):启用后,此属性会修改 @LoadBalanced 注解的 RestTemplate 的请求头,使其传递依赖配置中设置的带有版本的头和内容类型。如果没有此设置,这两个参数将不起作用。

© . This site is unofficial and not affiliated with VMware.