75. Spring Cloud Zookeeper 和 Service Registry
Spring Cloud Zookeeper 实现ServiceRegistry
接口,让开发人员以编程方式注册任意服务。
ServiceInstanceRegistration
class 提供builder()
方法来创建ServiceRegistry
可以使用的Registration
object,如下面的示例所示:
@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);
}
75.1 实例状态
Netflix Eureka 支持在服务器上注册OUT_OF_SERVICE
的实例。这些实例不会作为 active 服务实例返回。这对于 blue/green 部署等行为很有用。 (请注意,Curator Service Discovery 配方不支持此 behavior.)利用灵活的有效负载,让 Spring Cloud Zookeeper 通过更新某些特定元数据然后在 Ribbon ZookeeperServerList
中过滤该元数据来实现OUT_OF_SERVICE
。ZookeeperServerList
过滤掉所有 non-null 实例状态不等于UP
。如果实例状态字段为空,则认为它是UP
以便向后兼容。要更改实例的状态,请将POST
带OUT_OF_SERVICE
到ServiceRegistry
实例状态 actuator 端点,如下所示例:
$ http POST http://localhost:8081/service-registry status=OUT_OF_SERVICE
前面的 example 使用https://httpie.org中的
http
命令。