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 服務發現配方不支援此行為。)利用靈活的負載,Spring Cloud Zookeeper 透過更新一些特定的元資料,然後在 Spring Cloud LoadBalancer 的 ZookeeperServiceInstanceListSupplier 中根據該元資料進行過濾來實現 OUT_OF_SERVICEZookeeperServiceInstanceListSupplier 會過濾掉所有非空且不等於 UP 的例項狀態。如果例項狀態欄位為空,為了向後相容,則將其視為 UP。要更改例項的狀態,請使用 POST 請求將 OUT_OF_SERVICE 傳送到 ServiceRegistry 例項狀態 actuator 端點,如下例所示:

$ http POST https://:8081/serviceregistry status=OUT_OF_SERVICE
上例使用了 httpie.orghttp 命令。
© . This site is unofficial and not affiliated with VMware.