关于nacos:Nacos-启动

看了Eureka系列、Ribbon系列、Feign系列、Zuul系列,我置信大家应该晓得怎么找到看源码的入口了,一个是须要用的注解,一个是spring.factories。咱们还是从注解先来。

@EnableDiscoveryClient

这个注解做了两件事,一个是import了EnableDiscoveryClientImportSelector,另外一个就是默认autoRegister为true,开启主动注册。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public @interface EnableDiscoveryClient {
    boolean autoRegister() default true;
}

EnableDiscoveryClientImportSelector的selectImports办法,通过下面的autoRegister来判断,如果为true,则import了AutoServiceRegistrationConfiguration,如果为false,pring.cloud.service-registry.auto-registration.enabled设置为false。这个参数决定了是否引入主动注册。

@Override
public String[] selectImports(AnnotationMetadata metadata) {
    // 其余略
    boolean autoRegister = attributes.getBoolean("autoRegister");

    if (autoRegister) {
        List<String> importsList = new ArrayList<>(Arrays.asList(imports));
        importsList.add(
                "org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration");
        imports = importsList.toArray(new String[0]);
    }
    else {
        // 其余略
        // spring.cloud.service-registry.auto-registration.enabled设置为false
        map.put("spring.cloud.service-registry.auto-registration.enabled", false);
        // 其余略

    }
    return imports;
}

NacosServiceAutoConfiguration

注解的局部看完了,咱们开始看spring.factories。首先是NacosServiceAutoConfiguration。
这里只加载了NacosServiceManager,是service外围治理类,NamingService就是他治理的。

NacosDiscoveryAutoConfiguration

这个是用于服务发现的,他加载了两个类,NacosDiscoveryProperties和NacosServiceDiscovery。
NacosDiscoveryProperties是配置类,咱们配置注册核心的信息就是在这里配置。
NacosServiceDiscovery次要是用于服务发现。

NacosServiceRegistryAutoConfiguration

NacosServiceRegistryAutoConfiguration,这个是用于主动注册的。
咱们看到他下面的注解信息,@AutoConfigureAfter确保AutoServiceRegistrationConfiguration先加载,而后判断spring.cloud.service-registry.auto-registration.enabled是否为true,为true才能够加载。所以下面@EnableDiscoveryClient的autoRegister如果设置为false,则不加载。
因为spring.cloud.service-registry.auto-registration.enabled默认是true的,所以@EnableDiscoveryClient其实不引入,也是能够加载NacosServiceRegistryAutoConfiguration。

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
        matchIfMissing = true)
@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class,
        AutoServiceRegistrationAutoConfiguration.class,
        NacosDiscoveryAutoConfiguration.class })
public class NacosServiceRegistryAutoConfiguration

这个类会加载NacosServiceRegistry、NacosRegistration、NacosAutoServiceRegistration。NacosServiceRegistry、NacosRegistration会注入到NacosAutoServiceRegistration。
NacosServiceRegistry次要用于注册、勾销注册。
NacosRegistration是以后实例的信息,比方实例名称、实例id、端口、ip等。
NacosAutoServiceRegistration继承了NacosAutoServiceRegistration,NacosAutoServiceRegistration又继承了AbstractAutoServiceRegistration。AbstractAutoServiceRegistration实现了ApplicationListener<WebServerInitializedEvent>接口,通过WebServerInitializedEvent就晓得了,当tomcat启动胜利后,会调用onApplicationEvent(WebServerInitializedEvent event)办法。这个办法会实现注册操作,前面再来解说。

NacosDiscoveryClientConfiguration

这里加载了两个类,DiscoveryClient和NacosWatch。
DiscoveryClient次要用于示例获取和服务获取。
NacosWatch实现了SmartLifecycle接口,所以会调用start和stop办法。
start办法次要是退出NamingEvent监听、获取NamingService、注册监听、公布HeartbeatEvent事件。

@Override
public void start() {
    // 启动的时候,退出NamingEvent监听
    if (this.running.compareAndSet(false, true)) {
        EventListener eventListener = listenerMap.computeIfAbsent(buildKey(),
                event -> new EventListener() {
                    @Override
                    public void onEvent(Event event) {
                        if (event instanceof NamingEvent) {
                            List<Instance> instances = ((NamingEvent) event)
                                    .getInstances();
                            Optional<Instance> instanceOptional = selectCurrentInstance(
                                    instances);
                            instanceOptional.ifPresent(currentInstance -> {
                                resetIfNeeded(currentInstance);
                            });
                        }
                    }
                });
        // 获取NamingService
        NamingService namingService = nacosServiceManager
                .getNamingService(properties.getNacosProperties());
        try {
            // 注册监听
            namingService.subscribe(properties.getService(), properties.getGroup(),
                    Arrays.asList(properties.getClusterName()), eventListener);
        }
        catch (Exception e) {
            log.error("namingService subscribe failed, properties:{}", properties, e);
        }
        //公布HeartbeatEvent事件
        this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(
                this::nacosServicesWatch, this.properties.getWatchDelay());
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理