nacos服务注册之SpringCloud 集成nacos

简介: nacos服务注册之SpringCloud 集成nacos

nacos服务注册之SpringCloud 集成nacos

服务注册的功能主要体现在:

  • 服务实例在启动时注册到服务注册表,并在关闭时注销。
  • 服务消费者查询服务注册表,获得可用实例。
  • 服务注册中心需要调用服务实例的健康检查API来验证 是否能够处理请求

ServiceRegistry是Spring Cloud提供的服务注册的标准。集成到Spring Cloud中实现服务注册的组件,都会实现该接口。

public interface ServiceRegistry<R extends Registration> {

   /**
    * Registers the registration. A registration typically has information about an
    * instance, such as its hostname and port.
    * @param registration registration meta data
    */
   void register(R registration);

   /**
    * Deregisters the registration.
    * @param registration registration meta data
    */
   void deregister(R registration);

   /**
    * Closes the ServiceRegistry. This is a lifecycle method.
    */
   void close();

   /**
    * Sets the status of the registration. The status values are determined by the
    * individual implementations.
    * @param registration The registration to update.
    * @param status The status to set.
    * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
    */
   void setStatus(R registration, String status);

   /**
    * Gets the status of a particular registration.
    * @param registration The registration to query.
    * @param <T> The type of the status.
    * @return The status of the registration.
    * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
    */
   <T> T getStatus(R registration);

}

NacosServiceRegistry实现该接口

SpringCloud 集成nacos

spring-cloud-commons包的META-INF/spring.factories自动装配:

# AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.client.CommonsClientAutoConfiguration,\
org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancerAutoConfiguration,\
org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,\
org.springframework.cloud.commons.httpclient.HttpClientConfiguration,\
org.springframework.cloud.commons.util.UtilAutoConfiguration,\
org.springframework.cloud.configuration.CompatibilityVerifierAutoConfiguration,\
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.client.HostInfoEnvironmentPostProcessor
# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.cloud.configuration.CompatibilityNotMetFailureAnalyzer

AutoServiceRegistrationAutoConfiguration是服务注册相关的配置类

@Configuration(proxyBeanMethods = false)
@Import(AutoServiceRegistrationConfiguration.class)
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
      matchIfMissing = true)
public class AutoServiceRegistrationAutoConfiguration {

   @Autowired(required = false)
   private AutoServiceRegistration autoServiceRegistration;

   @Autowired
   private AutoServiceRegistrationProperties properties;

   @PostConstruct
   protected void init() {
      if (this.autoServiceRegistration == null && this.properties.isFailFast()) {
         throw new IllegalStateException("Auto Service Registration has "
               + "been requested, but there is no AutoServiceRegistration bean");
      }
   }

}

AutoServiceRegistrationAutoConfiguration注入了AutoServiceRegistration,AutoServiceRegistration是个接口,AbstractAutoServiceRegistration实现了这个接口,而NacosAutoServiceRegistration继承了AbstractAutoServiceRegistration

AbstractAutoServiceRegistration同时实现了ApplicationListener接口:

ApplicationListener接口:

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

   /**
    * Handle an application event.
    * @param event the event to respond to
    */
   void onApplicationEvent(E event);

}

接口中方法的作用是监听某个特定事件,

AbstractAutoServiceRegistration实现了这个接口:

@Override
@SuppressWarnings("deprecation")
public void onApplicationEvent(WebServerInitializedEvent event) {
   bind(event);
}

@Deprecated
public void bind(WebServerInitializedEvent event) {
   ApplicationContext context = event.getApplicationContext();
   if (context instanceof ConfigurableWebServerApplicationContext) {
      if ("management".equals(((ConfigurableWebServerApplicationContext) context)
            .getServerNamespace())) {
         return;
      }
   }
   this.port.compareAndSet(0, event.getWebServer().getPort());
   this.start();
}

AbstractAutoServiceRegistration监听WebServerInitializedEvent事件,调用bind方法 ,最终调用NacosServiceRegistry的register方法

总结一下:
服务注册的功能主要体现在:

  • 服务实例在启动时注册到服务注册表,并在关闭时注销。
  • 服务消费者查询服务注册表,获得可用实例。
  • 服务注册中心需要调用服务实例的健康检查API来验证 是否能够处理请求

这就是SpringCloud集成nacos的部分,ServiceRegistry是Spring Cloud提供的服务注册的标准。如果你有什么不懂的地方,或者我写错的地方,欢迎给我留言评论,我们一起学习一起进步,一起成就更好的自己,提升技术,服务业务,服务工作。我们一起努力,共同进步,我们下篇文章见。

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
负载均衡 算法 Java
【SpringCloud(4)】OpenFeign客户端:OpenFeign服务绑定;调用服务接口;Feign和OpenFeign
Feign是一个WebService客户端。使用Feign能让编写WebService客户端更加简单。 它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,十七支持了SpringMVC标准注解和HttpMessageConverters。 Feign可用于Eureka和Ribbon组合使用以支持负载均衡
1186 139
|
人工智能 Java API
Nacos 3.1.0 正式发布,支持 A2A 注册中心与 MCP 注册协议增强
3.1.0 发布核心全新功能-Agent 注册中心,助力构建基于 A2A 协议的多 Agent 协作的AI应用,同时 MCP 注册中心适配最新 MCP 官方注册中心协议及升级优化多项核心功能。
2904 91
|
JSON Java Nacos
SpringCloud 应用 Nacos 配置中心注解
在 Spring Cloud 应用中可以非常低成本地集成 Nacos 实现配置动态刷新,在应用程序代码中通过 Spring 官方的注解 @Value 和 @ConfigurationProperties,引用 Spring enviroment 上下文中的属性值,这种用法的最大优点是无代码层面侵入性,但也存在诸多限制,为了解决问题,提升应用接入 Nacos 配置中心的易用性,Spring Cloud Alibaba 发布一套全新的 Nacos 配置中心的注解。
2252 164
|
存储 Java Nacos
Spring Cloud+Nacos+KMS 动态配置最佳实践
本文讲述了 Spring Cloud 应用中结合 Nacos 实现了运行期配置动态更新的功能,以及在此基础上结合 KMS 在不改动代码的情况下对应用使用的敏感配置进行保护,解决将配置迁移到 Nacos 中可能存在的数据安全顾虑,并对其底层工作原理做了简单介绍。
2128 182
|
人工智能 API 开发工具
GitHub官方开源MCP服务!GitHub MCP Server:无缝集成GitHub API,实现Git流程完全自动化
GitHub MCP Server是基于Model Context Protocol的服务器工具,提供与GitHub API的无缝集成,支持自动化处理问题、Pull Request和仓库管理等功能。
4063 2
GitHub官方开源MCP服务!GitHub MCP Server:无缝集成GitHub API,实现Git流程完全自动化
|
Cloud Native Java Nacos
springcloud/springboot集成NACOS 做注册和配置中心以及nacos源码分析
通过本文,我们详细介绍了如何在 Spring Cloud 和 Spring Boot 中集成 Nacos 进行服务注册和配置管理,并对 Nacos 的源码进行了初步分析。Nacos 作为一个强大的服务注册和配置管理平台,为微服务架构提供
5342 14
|
机器学习/深度学习 人工智能 自然语言处理
Voice-Pro:开源AI音频处理工具,集成转录、翻译、TTS等一站式服务
Voice-Pro是一款开源的多功能音频处理工具,集成了语音转文字、文本转语音、实时翻译、YouTube视频下载和人声分离等多种功能。它支持超过100种语言,适用于教育、娱乐和商业等多个领域,为用户提供一站式的音频处理解决方案,极大地提高工作效率和音频处理的便捷性。
2483 10
Voice-Pro:开源AI音频处理工具,集成转录、翻译、TTS等一站式服务
|
Java 网络安全 Nacos
Nacos作为流行的微服务注册与配置中心,其稳定性与易用性广受好评
Nacos作为流行的微服务注册与配置中心,其稳定性与易用性广受好评。然而,“客户端不发送心跳检测”是使用中常见的问题之一。本文详细探讨了该问题的原因及解决方法,包括检查客户端配置、网络连接、日志、版本兼容性、心跳检测策略、服务实例注册状态、重启应用及环境变量等步骤,旨在帮助开发者快速定位并解决问题,确保服务正常运行。
464 5
|
Dubbo Cloud Native 应用服务中间件
阿里云的 Dubbo 和 Nacos 深度整合,提供了高效的服务注册与发现、配置管理等关键功能,简化了微服务治理,提升了系统的灵活性和可靠性。
在云原生时代,微服务架构成为主流。阿里云的 Dubbo 和 Nacos 深度整合,提供了高效的服务注册与发现、配置管理等关键功能,简化了微服务治理,提升了系统的灵活性和可靠性。示例代码展示了如何在项目中实现两者的整合,通过 Nacos 动态调整服务状态和配置,适应多变的业务需求。
842 2
|
JSON Java 测试技术
SpringCloud2023实战之接口服务测试工具SpringBootTest
SpringBootTest同时集成了JUnit Jupiter、AssertJ、Hamcrest测试辅助库,使得更容易编写但愿测试代码。
1065 3