Spring Cloud Zookeeper 和服务注册

Spring Cloud Zookeeper 实现了 ServiceRegistry 接口,使开发人员能够以编程方式注册任意服务。

ServiceInstanceRegistration 类提供了一个 builder() 方法来创建一个可以被 ServiceRegistry 使用的 Registration 对象,如下例所示

@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 recipe 不支持此行为。) 利用灵活的有效负载,Spring Cloud Zookeeper 实现了 OUT_OF_SERVICE,方法是更新一些特定的元数据,然后在 Spring Cloud LoadBalancer ZookeeperServiceInstanceListSupplier 中过滤该元数据。 ZookeeperServiceInstanceListSupplier 过滤掉所有不等于 UP 的非空实例状态。 如果实例状态字段为空,则为了向后兼容,它被认为是 UP。 要更改实例的状态,请使用 OUT_OF_SERVICEServiceRegistry 实例状态执行器端点发出 POST 请求,如下例所示

$ http POST http://localhost:8081/serviceregistry status=OUT_OF_SERVICE
前面的示例使用了来自 httpie.orghttp 命令。