当前位置:   article > 正文

SpringCloud Nacos 【客户端】自动注册源码解析_springboot nacos 自动注册 源码

springboot nacos 自动注册 源码

1 项目demo

在这里插入图片描述

1.1 pom

 <dependencies>
        <!--spring cloud alibaba-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.2 ProviderController

@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Value("${server.port}")
    private Integer port;


    @GetMapping("/hello")
    public String hello() {

        return "server port: " + port + ", say: Hello World";
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.3 ProviderApplication

@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(ProviderApplication.class);

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.4 启动

在这里插入图片描述

2 静态源码解读

2.1 spring.factories

描述: springboot 自动装配原理,查看nacos包下面的 spring.factories文件。找到NacosServiceRegistryAutoConfiguration类,该类作用是nacos服务自动装配。
在这里插入图片描述

2.2 NacosServiceRegistryAutoConfiguration

在这里插入图片描述

2.3 NacosAutoServiceRegistration

描述: 查看当前类图。发现当前类实现了ApplicationListener接口,及有监听事件。
在这里插入图片描述
描述: 查看监听事件。监听事件在其父类AbstractAutoServiceRegistration里。
在这里插入图片描述

描述: 跟踪bind方法。
在这里插入图片描述
描述: 进入start方法。
在这里插入图片描述
描述: 进入NacosAutoServiceRegistration类 register 方法。
在这里插入图片描述
描述: 进入AbstractAutoServiceRegistration 类register方法。
在这里插入图片描述
描述: 既然怒NacosServiceRegistry类register方法。

@Override
	public void register(Registration registration) {

		if (StringUtils.isEmpty(registration.getServiceId())) {
			log.warn("No service to register for nacos client...");
			return;
		}
        //获取namingService,serviceId、组id
		NamingService namingService = namingService();
		String serviceId = registration.getServiceId();
		String group = nacosDiscoveryProperties.getGroup();
		//封装注册实例
		Instance instance = getNacosInstanceFromRegistration(registration);

		try {
		    //注册
			namingService.registerInstance(serviceId, group, instance);
			log.info("nacos registry, {} {} {}:{} register finished", group, serviceId,
					instance.getIp(), instance.getPort());
		}
		catch (Exception e) {
			log.error("nacos registry, {} register failed...{},", serviceId,
					registration.toString(), e);
			// rethrow a RuntimeException if the registration is failed.
			// issue : https://github.com/alibaba/spring-cloud-alibaba/issues/1132
			rethrowRuntimeException(e);
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

在这里插入图片描述
描述: NacosNamingService 调用registerInstance 进行当前服务的注册。
在这里插入图片描述

3 Debug验证

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/315377
推荐阅读
相关标签
  

闽ICP备14008679号