77. 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);
}

77.1 实例状态

Netflix Eureka 支持将OUT_OF_SERVICE实例注册到服务器。这些实例不作为Active服务实例返回。这对于诸如蓝色/绿色部署之类的行为很有用。 (请注意,Curator Service Discovery 配方不支持此行为.)利用灵活的有效负载,Spring Cloud Zookeeper 可以通过更新一些特定的元数据,然后在功能区ZookeeperServerList中对该元数据进行过滤来实现OUT_OF_SERVICEZookeeperServerList过滤掉所有不等于UP的非空实例状态。如果实例状态字段为空,则为了向后兼容,它被认为是UP。要更改实例的状态,请在ServiceRegistry实例状态 Actuator 端点上创建一个POSTOUT_OF_SERVICE,如以下示例所示:

$ http POST http://localhost:8081/service-registry status=OUT_OF_SERVICE

Note

前面的示例使用了https://httpie.orghttp命令。