6. Zookeeper 依赖项
以下主题涵盖了如何使用 Spring Cloud Zookeeper 依赖项:
6.1. 使用Zookeeper依赖项
Spring Cloud Zookeeper 给你的应用程序提供了通过属性提供依赖项的可能。作为依赖项,你可以理解在 Zookeeper 注册并希望通过
Feign
(一个 REST 客户端构建器),
Spring
RestTemplate 和
Spring WebFlux
调用的其他应用程序。
您也可以使用Zookeeper依赖监视器功能来控制和监控 依赖项的状态。
6.2. 激活Zookeeper依赖
包括对
org.springframework.cloud:spring-cloud-starter-zookeeper-discovery 的依赖将启用自动配置,以设置 Spring Cloud Zookeeper 依赖。即使你在属性中提供了依赖,也可以通过将其设置为 false 来关闭(它默认为
true)。
6.3. 配置Zookeeper依赖
考虑以下依赖关系表示的示例:
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。
6.3.1. 别名
在根属性下,你需要将每个依赖表示为一个别名。
这是由于 Spring Cloud LoadBalancer 的限制,该限制要求将应用程序ID放在URL中。
因此,你不能传递任何复杂的路径,例如 /myApp/myRoute/name)。
该别名是你在 DiscoveryClient、Feign 或 RestTemplate 处代替 serviceId 的名称。
在之前的示例中,别名是 newsletter 和 mailing。
以下示例展示了使用 Feign 时的 newsletter 别名:
@FeignClient("newsletter")
public interface NewsletterService {
@RequestMapping(method = RequestMethod.GET, value = "/newsletter")
String getNewsletters();
}
6.3.2. 路径
路径由 YAML 中的 path 属性表示,是该依赖在 Zookeeper 下注册的路径。
正如在
上一节
中所述,Spring Cloud LoadBalancer 操作在 URLs 上。
因此,该路径不满足其要求。
这就是为什么 Spring Cloud Zookeeper 会将别名映射到正确的路径。
6.3.3. 负载均衡器类型
负载均衡器类型由 YAML 中的 loadBalancerType 属性表示。
如果你知道在调用此特定依赖时应应用哪种负载均衡策略,你可以在YAML文件中提供它,系统会自动应用。 你可以选择以下负载均衡策略之一:
-
STICKY: 一旦选择,实例将始终被调用。
-
随机选择: 随机选择一个实例。
-
循环取样: 依次遍历实例,循环往复。
6.3.4. 模板和版本 Content-Type
The Content-Type 模板和版本由 contentTypeTemplate 和
version YAML 属性表示。
如果你在Content-Type头中对API进行版本化,你就不需要在每个请求中都添加这个头。此外,当你想要调用API的新版本时,也不需要在整个代码中到处查找并提升API版本。这就是你可以提供一个contentTypeTemplate并使用一个特殊的$version占位符的原因。该占位符将由versionYAML属性的值填充。考虑以下contentTypeTemplate示例:
application/vnd.newsletter.$version+json
进一步考虑以下 version:
v1
将 contentTypeTemplate 与版本组合将为每个请求创建一个 Content-Type 头部,如下所示:
application/vnd.newsletter.v1+json
6.3.5. 默认头信息
默认的头信息在 YAML 中由 headers 映射表示。
有时,每次调用依赖项都需要设置一些默认的头部。为了避免在代码中进行设置,您可以将它们在YAML文件中进行配置,如下例中的headers部分所示:
headers:
Accept:
- text/html
- application/xhtml+xml
Cache-Control:
- no-cache
那个headers部分会导致在您的HTTP请求中添加Accept和Cache-Control头信息以及相应的值列表。
6.3.6. 必需依赖项
必需的依赖项在 YAML 中由 required 属性表示。
如果其中一个依赖项在您的应用程序启动时必须可用,您可以设置YAML文件中的required: true属性。
如果您的应用程序在启动时无法本地化所需的依赖项,则会抛出异常,Spring 上下文将无法设置。换句话说,如果所需依赖项未在Zookeeper中注册,您的应用程序将无法启动。
You can read more about Spring Cloud Zookeeper Presence Checker 的相关内容在本文档的后面部分。
6.3.7. 伪对象
您可以提供一个用冒号分隔的JAR文件路径,该JAR文件包含依赖项的存根,如 下例所示:
stubs: org.springframework:myApp:stubs
其中:
-
org.springframework是groupId。 -
myApp是artifactId。 -
stubs是分类器。 (注意stubs是默认值。)
因为 stubs 是默认分类器,上述示例等同于以下示例:
stubs: org.springframework:myApp
6.4. 配置 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的请求头,使其携带由依赖配置中设置的版本的头信息和内容类型。 如果没有启用此设置,这两个参数将无法工作。