5. Spring Cloud Zookeeper 与服务注册

Spring Cloud Zookeeper 实现了 ServiceRegistry 接口,让开发者 以编程方式注册任意服务。spring-doc.cadn.net.cn

The ServiceInstanceRegistration class offers a builder() method to create a Registration object that can be used by the ServiceRegistry, as shown in the following example:spring-doc.cadn.net.cn

@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);
}

5.1. 实例状态

Netflix Eureka 支持实例以 OUT_OF_SERVICE 的状态注册到服务器。 这些实例不会作为活动服务实例返回。 这在蓝/绿部署等行为中很有用。 (注意,Curator 服务发现配方不支持此行为。)借助灵活的负载使 Spring Cloud Zookeeper 实现 OUT_OF_SERVICE,通过更新某些特定的元数据并在 Spring Cloud LoadBalancer ZookeeperServiceInstanceListSupplier 上进行过滤。 ZookeeperServiceInstanceListSupplier 过滤掉所有不等于 UP 的非空实例状态。 如果实例状态字段为空,则视为 UP,以保持向后兼容。 要更改实例状态,向 ServiceRegistry 实例状态 actuator 端点发送 POST OUT_OF_SERVICE,如下例所示:spring-doc.cadn.net.cn

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