Spring Cloud Commons 普通抽象 谁借莪1个温暖的怀抱¢ 2022-05-12 03:40 86阅读 0赞 诸如服务发现,负载平衡和断路器之类的模式适用于所有Spring Cloud客户端可以独立于实现(例如通过Eureka或Consul发现)的消耗的共同抽象层。 ### @EnableDiscoveryClient ### Commons提供`@EnableDiscoveryClient`注释。这通过`META-INF/spring.factories`查找`DiscoveryClient`接口的实现。Discovery Client的实现将在`org.springframework.cloud.client.discovery.EnableDiscoveryClient`键下的`spring.factories`中添加一个配置类。`DiscoveryClient`实现的示例是[Spring Cloud Netflix Eureka][],[Spring Cloud Consul发现][Spring Cloud Consul]和[Spring Cloud Zookeeper发现][Spring Cloud Zookeeper]。 默认情况下,`DiscoveryClient`的实现将使用远程发现服务器自动注册本地Spring Boot服务器。可以通过在`@EnableDiscoveryClient`中设置`autoRegister=false`来禁用此功能。 ### ServiceRegistry ### Commons现在提供了一个`ServiceRegistry`接口,它提供了诸如`register(Registration)`和`deregister(Registration)`之类的方法,允许您提供定制的注册服务。`Registration`是一个标记界面。 @Configuration @EnableDiscoveryClient(autoRegister=false) public class MyConfiguration { private ServiceRegistry registry; public MyConfiguration(ServiceRegistry registry) { this.registry = registry; } // called via some external process, such as an event or a custom actuator endpoint public void register() { Registration registration = constructRegistration(); this.registry.register(registration); } } 每个`ServiceRegistry`实现都有自己的`Registry`实现。 服务部门自动注册。欢迎大家一起学习研究相关技术愿意了解源码的朋友直接求求交流分享技术二一四七七七五六三三 默认情况下,`ServiceRegistry`实现将自动注册正在运行的服务。要禁用该行为,有两种方法。您可以设置`@EnableDiscoveryClient(autoRegister=false)`永久禁用自动注册。您还可以设置`spring.cloud.service-registry.auto-registration.enabled=false`以通过配置禁用该行为。 服务注册执行器端点 Commons提供`/service-registry`致动器端点。该端点依赖于Spring应用程序上下文中的`Registration` bean。通过GET调用`/service-registry/instance-status`将返回`Registration`的状态。具有`String`主体的同一端点的POST将将当前`Registration`的状态更改为新值。请参阅您正在使用的`ServiceRegistry`实现的文档,以获取更新状态的允许值和为状态获取的值。 [Spring Cloud Netflix Eureka]: http://cloud.spring.io/spring-cloud-netflix/ [Spring Cloud Consul]: http://cloud.spring.io/spring-cloud-consul/ [Spring Cloud Zookeeper]: http://cloud.spring.io/spring-cloud-zookeeper/
还没有评论,来说两句吧...