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 https://:8081/serviceregistry status=OUT_OF_SERVICE
前面的示例使用了來自 httpie.orghttp 命令。