Zookeeper Dependencies
以下主题介绍如何处理 Spring Cloud Zookeeper 依赖项:
Using the Zookeeper Dependencies
Spring Cloud Zookeeper 让您可以将应用程序的依赖项作为属性提供。您理解的依赖项可能是注册在 Zookeeper 中,您想通过 OpenFeign(一个 REST 客户端构建器)、RestTemplate
和 WebClient
通过 Spring Cloud Loadbalancer 来调用的其他应用程序。
您还可以使用 Zookeeper Dependency Watchers 功能来控制和监视依赖项的状态。
Activating Zookeeper Dependencies
包含对 org.springframework.cloud:spring-cloud-starter-zookeeper-discovery
的依赖项将启用自动配置,该配置将设置 Spring Cloud Zookeeper 依赖项。即使您在属性中提供了依赖项,也可以关闭依赖项。要执行此操作,请将 spring.cloud.zookeeper.dependency.enabled
属性设置为 false(默认为 true
)。
Setting up Zookeeper Dependencies
考虑以下依赖关系表示的示例:
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
。
Aliases
在 root 属性下方,你必须表示每个依赖项为一个别名。这是由于 Spring Cloud LoadBalancer 的限制,它要求应用程序标识符放入 URL。因此,你不能传递任何复杂路径,例如 /myApp/myRoute/name
。该别名是你在 DiscoveryClient
、Feign
或 RestTemplate
中用于替代 serviceId
的名称。
在前面的示例中,别名是 newsletter
和 mailing
。下面的示例展示了带有 newsletter
别名的 Feign 用法:
@FeignClient("newsletter")
public interface NewsletterService {
@RequestMapping(method = RequestMethod.GET, value = "/newsletter")
String getNewsletters();
}
Path
路径由 path
YAML 属性表示,这是依赖项在 Zookeeper 下注册的路径。如 previous section 中所述,Spring Cloud LoadBalancer 在 URL 上运行。因此,此路径不符合其要求。这就是 Spring Cloud Zookeeper 将别名映射到适当路径的原因。
Load Balancer Type
负载均衡器类型由 loadBalancerType
YAML 属性表示。
如果你了解在调用此特定依赖项时应该应用哪种负载均衡策略,则可以在 YAML 文件中提供它,并且它会自动应用。你可以选择以下负载均衡策略之一:
-
STICKY:一旦选择,将始终调用该实例。
-
RANDOM:随机选择一个实例。
-
ROUND_ROBIN:反复在实例之间切换。
Content-Type
Template and Version
Content-Type
模板和版本通过 contentTypeTemplate
和 version
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
Default Headers
默认头通过 YAML 中的 headers
映射表示。
有时,对依赖项的每一次调用都需要设置一些默认头。为不在代码中执行此操作,你可以在 YAML 文件中设置它们,如下例所示 headers
部分:
headers:
Accept:
- text/html
- application/xhtml+xml
Cache-Control:
- no-cache
该 headers
部分导致在 HTTP 请求中添加 Accept
和 Cache-Control
头部以及适当的值列表。
Required Dependencies
必需的依赖项由 YAML 中的 required
属性表示。
如果在应用程序引导时需要你的一个依赖项启动,则可以在 YAML 文件中设置 required: true
属性。
如果应用程序在引导期间找不到必需的依赖项,则会抛出一个异常,并且 Spring Context 将无法设置。换句话说,如果必需的依赖项未在 Zookeeper 中注册,则应用程序无法启动。
您可以在 later in this document 中阅读有关 Spring Cloud Zookeeper Presence Checker 的更多信息。
Configuring Spring Cloud Zookeeper Dependencies
您可以设置以下属性启用或禁用 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
标注的请求标头修改为传递标头和内容类型(依赖关系配置中设置的版本)。如果不设置此属性,这两个参数将不起作用。