本文主要研究一下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


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论