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 可以通过更新某些特定元数据,然后在 Spring Cloud LoadBalancer 的 ZookeeperServiceInstanceListSupplier 中根据该元数据进行筛选,从而实现 OUT_OF_SERVICE。ZookeeperServiceInstanceListSupplier 会过滤掉所有非空且不等于 UP 的实例状态。如果实例状态字段为空,则为了向后兼容,它将被视为 UP。要更改实例的状态,请向 ServiceRegistry 实例状态 actuator 端点发送一个带有 OUT_OF_SERVICE 的 POST 请求,如下所示:
$ http POST http://localhost:8081/serviceregistry status=OUT_OF_SERVICE
备注
前面的示例使用了来自 httpie.org 的 http 命令。