对于最新的稳定版本,请使用 Spring Cloud Zookeeper 4.3.0! |
Spring Cloud Zookeeper 和服务注册表
Spring Cloud Zookeeper 实现了ServiceRegistry
界面,让开发者
以编程方式注册任意服务。
这ServiceInstanceRegistration
类提供builder()
创建Registration
对象,该对象可供ServiceRegistry
,如下所示
例:
@Autowired
private ZookeeperServiceRegistry serviceRegistry;
public void registerThings() {
ZookeeperRegistration registration = ServiceInstanceRegistration.builder()
.defaultUriSpec()
.address("anyUrl")
.port(10)
.name("/a/b/c/d/anotherservice")
.build();
this.serviceRegistry.register(registration);
}
实例状态
Netflix Eureka 支持拥有OUT_OF_SERVICE
在服务器中注册。
这些实例不会作为活动服务实例返回。
这对于蓝/绿部署等行为很有用。
(请注意,Curator Service Discovery 配方不支持此行为。利用灵活的有效负载,让 Spring Cloud Zookeeper 实现OUT_OF_SERVICE
通过更新一些特定的元数据,然后在 Spring Cloud LoadBalancer 中过滤该元数据ZookeeperServiceInstanceListSupplier
.
这ZookeeperServiceInstanceListSupplier
过滤掉所有不等于的非空实例状态UP
.
如果实例状态字段为空,则认为它是UP
用于向后兼容性。
要更改实例的状态,请将POST
跟OUT_OF_SERVICE
到ServiceRegistry
实例状态执行器端点,如以下示例所示:
$ http POST http://localhost:8081/serviceregistry status=OUT_OF_SERVICE
前面的示例使用http 命令来自 httpie.org。 |