Spring Cloud Zookeeper and Service Registry
Spring Cloud Zookeeper 实现了 ServiceRegistry
接口,这允许开发人员以编程方式注册任意服务。
Spring Cloud Zookeeper implements the ServiceRegistry
interface, letting developers
register arbitrary services in a programmatic way.
ServiceInstanceRegistration
类提供了一个 builder()
方法,该方法可以创建一个`Registration` 对象,该对象可由 ServiceRegistry
使用,如下例所示:
The ServiceInstanceRegistration
class offers a builder()
method to create a
Registration
object that can be used by the ServiceRegistry
, as shown in the following
example:
@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);
}
Instance Status
Netflix Eureka 支持将实例`OUT_OF_SERVICE` 注册在服务器上。这些实例不会作为活动服务实例返回。这对蓝/绿部署等行为很有用。(请注意,Curator Service Discovery 方法不支持此行为。)Spring Cloud Zookeeper 利用灵活的有效负载实现了`OUT_OF_SERVICE`,方法是更新一些特定元数据,然后在 Spring Cloud LoadBalancer ZookeeperServiceInstanceListSupplier
中根据该元数据进行筛选。ZookeeperServiceInstanceListSupplier
会筛选出所有不等于 UP
的非空实例状态。如果实例状态字段为空,则为了向后兼容,它会被视为 UP
。要更改实例的状态,请使用 OUT_OF_SERVICE
向 ServiceRegistry
实例状态执行器端点发出 POST
,如下例所示:
Netflix Eureka supports having instances that are OUT_OF_SERVICE
registered with the server.
These instances are not returned as active service instances.
This is useful for behaviors such as blue/green deployments.
(Note that the Curator Service Discovery recipe does not support this behavior.) Taking advantage of the flexible payload has let Spring Cloud Zookeeper implement OUT_OF_SERVICE
by updating some specific metadata and then filtering on that metadata in the Spring Cloud LoadBalancer ZookeeperServiceInstanceListSupplier
.
The ZookeeperServiceInstanceListSupplier
filters out all non-null instance statuses that do not equal UP
.
If the instance status field is empty, it is considered to be UP
for backwards compatibility.
To change the status of an instance, make a POST
with OUT_OF_SERVICE
to the ServiceRegistry
instance status actuator endpoint, as shown in the following example:
$ http POST http://localhost:8081/serviceregistry status=OUT_OF_SERVICE
前一个示例使用 [role="bare"][role="bare"]https://httpie.org 中的 |
The preceding example uses the |