71. 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);
}
71.1 实例状态
Netflix Eureka 支持在服务器上注册OUT_OF_SERVICE
的实例,而不是作为 active 服务实例返回。这对于 blue/green 部署等行为非常有用。策展人服务发现配方不支持此行为。利用灵活的有效载荷让 Spring Cloud Zookeeper 通过更新一些特定的元数据然后过滤 Ribbon ZookeeperServerList
中的元数据来实现OUT_OF_SERVICE
。 ZookeeperServerList
过滤掉所有不等于UP
的 non-null 实例状态。如果实例状态字段为空,则将其视为UP
以实现向后兼容性。要将实例 POST OUT_OF_SERVICE
的状态更改为ServiceRegistry
实例状态 actuator 端点。
----
$ echo -n OUT_OF_SERVICE | http POST http://localhost:8081/service-registry/instance-status
----
NOTE: The above example uses the `http` command from https://httpie.org