Nacos源码 (7) Nacos与Spring

SpringCloud工程可以使用Nacos作为注册中心和配置中心,配置和使用非常简单,本文将简单介绍使用方式,并分析其实现方式。

SpringCloud工程集成Nacos

SpringCloud工程使用Nacos非常简单,只需要引入依赖、编写配置参数、在启动类上添加注解即可。

引入依赖

<dependencyManagement>
  <dependencies>
    <!-- Spring Cloud -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Hoxton.SR9</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <!-- Spring Cloud Alibaba -->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>2.2.5.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <!-- Nacos -->
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  </dependency>
</dependencies>

bootstrap.yml配置文件

spring:
  application:
    name: xx-service
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        prefix: ${spring.application.name}
        file-extension: yml
        group: DEFAULT_GROUP
        server-addr: localhost:8848
        extension-configs:
          - data-id: base-config-${spring.profiles.active}.yml
            group: DEFAULT_GROUP
            refresh: true
          - data-id: resource-config-${spring.profiles.active}.yml
            group: DEFAULT_GROUP
            refresh: true

配置中心源码分析

配置中心集成的源码在spring-cloud-starter-alibaba-nacos-config工程中。

spring.factories文件

# Configuration配置类
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.cloud.nacos.NacosConfigAutoConfiguration,\
com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration

org.springframework.boot.diagnostics.FailureAnalyzer=\
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer

# 这里是Nacos扩展的支持json和xml的PropertySourceLoader
org.springframework.boot.env.PropertySourceLoader=\
com.alibaba.cloud.nacos.parser.NacosJsonPropertySourceLoader,\
com.alibaba.cloud.nacos.parser.NacosXmlPropertySourceLoader

NacosConfigBootstrapConfiguration

装配NacosConfigProperties、NacosConfigManager、NacosPropertySourceLocator等组件:

  • NacosConfigProperties - 封装spring.cloud.nacos.config配置参数
  • NacosConfigManager - 用于获取ConfigService对象
  • NacosPropertySourceLocator - 用于加载配置封装PropertySource

注册中心源码分析

注册中心集成的源码在spring-cloud-starter-alibaba-nacos-discovery工程中。

spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,\
  com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\
  com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
  com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\
  com.alibaba.cloud.nacos.NacosServiceAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
  com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration

NacosDiscoveryClientConfigServiceBootstrapConfiguration

import了几个其他的配置类:

  • NacosDiscoveryAutoConfiguration.class
  • NacosServiceAutoConfiguration.class
  • NacosDiscoveryClientConfiguration.class
  • NacosReactiveDiscoveryClientConfiguration.class

NacosDiscoveryAutoConfiguration

装配NacosDiscoveryProperties、NacosServiceDiscovery组件。

NacosServiceDiscovery类:用于获取注册中心的服务和实例。

RibbonNacosAutoConfiguration

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnBean(SpringClientFactory.class)
@ConditionalOnRibbonNacos
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureAfter(RibbonAutoConfiguration.class)
// 使用NacosRibbonClientConfiguration装配NacosServerList
// NacosServerList用于获取注册到Nacos上的指定service的服务实例列表
@RibbonClients(defaultConfiguration = NacosRibbonClientConfiguration.class)
public class RibbonNacosAutoConfiguration {

}

RibbonClients注解导入RibbonClientConfigurationRegistrar类,支持组合多个RibbonClient注解。

RibbonClientConfigurationRegistrar类会解析RibbonClients和RibbonClient注解,将相关配置类注入到registry中。

NacosDiscoveryEndpointAutoConfiguration

装配NacosDiscoveryEndpoint和NacosDiscoveryHealthIndicator组件:

  • NacosDiscoveryEndpoint - 用于获取Nacos配置参数和订阅的服务
  • NacosDiscoveryHealthIndicator - 健康检查

NacosServiceRegistryAutoConfiguration

装配以下组件:

  • NacosServiceRegistry

    实现了ServiceRegistry接口,用于服务注册、服务下线

  • NacosRegistration

    封装服务实例信息:instanceId、serviceId、host、port和metadata等信息

  • NacosAutoServiceRegistration

    1. 将服务注册到Nacos注册中心,内部维护NacosServiceRegistry和NacosRegistration组件
    2. 继承了AbstractAutoServiceRegistration抽象类,其register用于注册服务
    3. AbstractAutoServiceRegistration实现了ApplicationListener接口,监听WebServerInitializedEvent事件,在事件发生时做服务注册

NacosDiscoveryClientConfiguration

装配NacosDiscoveryClient和NacosWatch组件。

热门相关:一等狂妃:邪王,请接招!   紫府仙缘   名门盛婚:首席,别来无恙!   楚氏赘婿   我是仙凡