聊聊nacos的NacosDiscoveryAutoConfiguration

本文主要研究一下nacos的NacosDiscoveryAutoConfiguration

NacosDiscoveryAutoConfiguration

nacos-spring-boot-project/nacos-discovery-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/discovery/autoconfigure/NacosDiscoveryAutoConfiguration.java

@ConditionalOnProperty(name = NacosDiscoveryConstants.ENABLED, matchIfMissing = true)
@ConditionalOnMissingBean(name = DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME)
@EnableNacosDiscovery
@EnableConfigurationProperties(value = NacosDiscoveryProperties.class)
@ConditionalOnClass(name = "org.springframework.boot.context.properties.bind.Binder")
public class NacosDiscoveryAutoConfiguration {
​
    @Bean
    public NacosDiscoveryAutoRegister discoveryAutoRegister() {
        return new NacosDiscoveryAutoRegister();
    }
​
}
  • NacosDiscoveryAutoConfiguration注册了NacosDiscoveryAutoRegister

NacosDiscoveryAutoRegister

nacos-spring-boot-project/nacos-discovery-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/discovery/autoconfigure/NacosDiscoveryAutoRegister.java

@Component
public class NacosDiscoveryAutoRegister implements ApplicationListener<WebServerInitializedEvent> {
​
    private static final Logger logger = LoggerFactory.getLogger(NacosDiscoveryAutoRegister.class);
​
    @NacosInjected
    private NamingService namingService;
​
    @Autowired
    private NacosDiscoveryProperties discoveryProperties;
​
    @Value("${spring.application.name:spring.application.name}")
    private String applicationName;
​
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
​
        if (!discoveryProperties.isAutoRegister()) {
            return;
        }
​
        Register register = discoveryProperties.getRegister();
​
        if (StringUtils.isEmpty(register.getIp())) {
            register.setIp(NetUtils.localIP());
        }
​
        if (register.getPort() == 0) {
            register.setPort(event.getWebServer().getPort());
        }
​
        register.getMetadata().put("preserved.register.source", "SPRING_BOOT");
​
        register.setInstanceId("");
        String serviceName = StringUtils.isEmpty(register.getServiceName()) ? applicationName : register.getServiceName();
​
        try {
            namingService.registerInstance(serviceName, register.getGroupName(), register);
            logger.info("Finished auto register service : {}, ip : {}, port : {}", register.getServiceName(), register.getIp(), register.getPort());
        } catch (NacosException e) {
            throw new AutoRegisterException(e);
        }
    }
​
}
  • NacosDiscoveryAutoRegister实现了ApplicationListener接口,它会在接收到WebServerInitializedEvent时通过namingService.registerInstance(serviceName, register.getGroupName(), register)来注册实例,若出现NacosException则会被转义为AutoRegisterException

NacosDiscoveryAutoDeregister

nacos-spring-boot-project/nacos-discovery-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/discovery/autoconfigure/NacosDiscoveryAutoDeregister.java

@Component
public class NacosDiscoveryAutoDeregister implements ApplicationListener<ContextClosedEvent> {
​
    private static final Logger logger = LoggerFactory.getLogger(NacosDiscoveryAutoRegister.class);
​
    @NacosInjected
    private NamingService namingService;
​
    private final NacosDiscoveryProperties discoveryProperties;
    private final WebServer webServer;
​
    @Value("${spring.application.name:spring.application.name}")
    private String applicationName;
​
    public NacosDiscoveryAutoDeregister(NacosDiscoveryProperties discoveryProperties, WebServer webServer) {
        this.discoveryProperties = discoveryProperties;
        this.webServer = webServer;
    }
​
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        if (!discoveryProperties.isAutoRegister()) {
            return;
        }
​
        Register register = discoveryProperties.getRegister();
​
        if (StringUtils.isEmpty(register.getIp())) {
            register.setIp(NetUtils.localIP());
        }
​
        if (register.getPort() == 0) {
            register.setPort(webServer.getPort());
        }
​
        String serviceName = StringUtils.isEmpty(register.getServiceName()) ? applicationName : register.getServiceName();
​
        try {
            namingService.deregisterInstance(serviceName, register.getGroupName(), register);
            logger.info("Finished auto deregister service : {}, ip : {}, port : {}", register.getServiceName(), register.getIp(), register.getPort());
        } catch (NacosException e) {
            throw new AutoDeregisterException(e);
        }
    }
}
  • NacosDiscoveryAutoDeregister实现了ApplicationListener接口,它会在接收到ContextClosedEvent时通过namingService.deregisterInstance(serviceName, register.getGroupName(), register)来注销实例,若出现NacosException则会被转义为AutoDeregisterException

小结

  • NacosDiscoveryAutoConfiguration注册了NacosDiscoveryAutoRegister
  • NacosDiscoveryAutoRegister实现了ApplicationListener接口,它会在接收到WebServerInitializedEvent时通过namingService.registerInstance(serviceName, register.getGroupName(), register)来注册实例,若出现NacosException则会被转义为AutoRegisterException
  • NacosDiscoveryAutoDeregister实现了ApplicationListener接口,它会在接收到ContextClosedEvent时通过namingService.deregisterInstance(serviceName, register.getGroupName(), register)来注销实例,若出现NacosException则会被转义为AutoDeregisterException

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏陶士涵的菜地

[android] android下junit测试框架配置

我们的业务代码一般是放在一个新的包下面,这个业务类不能够通过右键run as java application,因为android项目只能运行在手机上的dalv...

9430
来自专栏陶士涵的菜地

[android] smartimageview&常见的开源代码

github上搜索开源框架android-smarty-imageview,下载压缩包,拷贝我们之前写的网络图片查看器布局。

10640
来自专栏FunTester

python用递归筛选法求N以内的孪生质数(孪生素数)

本人最近读完一本书《质数的孤独》,里面讲到孪生质数,就想查一下孪生质数的分布情况。其中主要用到了计算质数(素数)的方法,搜了一下,排名前几的都是用for循环来做...

63520
来自专栏陶士涵的菜地

[android] 采用服务录制电话&服务的生命周期

调用MediaRecorder对象的setAudioSource()方法,设置音频源,

10750
来自专栏陶士涵的菜地

[android] 采用断点调试的方式观察pull解析的流程

当程序出现错误的时候,界面出不来,这个时候就需要调试技巧,描述这个程序在哪个地方出现的问题。在你认为可能出错的代码部分,左侧的行号栏点击打断点,在项目目录右键 ...

5920
来自专栏数据猿

Java字节码增强探秘

大家好,美美今天给大家推荐一篇Java字节码增强技术的文章,在实际工作中有很多应用场景。

16120
来自专栏陶士涵的菜地

[android] 常用数据适配器SimpleAdapter

当我们的列表包含图片和文字信息时,ArrayAdapter就无法实现了,使用SimpleAdapter,activity中findViewById()获取到Li...

7660
来自专栏Java架构沉思录

讲真,下次打死我也不敢随便改serialVersionUID了

序列化是一种对象持久化的手段。普遍应用在网络传输、RMI等场景中。类通过实现 java.io.Serializable 接口以启用其序列化功能。

14710
来自专栏FunTester

基于java的直线型接口测试框架初探

在使用java语言作为接口测试的过程中,发现java语言的简洁性的确不如脚本语言,如python,很多功能python一行代码几个方法就能搞定,java需要几行...

9430
来自专栏陶士涵的菜地

[android] androidPN开源项目介绍

找到server的代码,开启服务,双击 bin/run.bat ,服务启动后监听127.0.0.1:7070端口

13120

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励