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_SERVICE。ZookeeperServiceInstanceListSupplier 会过滤掉所有非空且不等于 UP 的实例状态。如果实例状态字段为空,为了向后兼容,则将其视为 UP。要更改实例的状态,请使用 POST 请求将 OUT_OF_SERVICE 发送到 ServiceRegistry 实例状态 actuator 端点,如下例所示:
$ http POST https://:8081/serviceregistry status=OUT_OF_SERVICE
上例使用了 httpie.org 的 http 命令。 |