2023-09-15
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105610838

初始化基本流程图

202309152310121941.png

EurekaServerInitializerConfiguration的start

这个startSmartLifecycle的,在容器finishRefresh的时候调用,也就是说该初始化和实例化基本都完成了,其实还是依靠EurekaServerBootstrap

202309152310145482.png

EurekaServerBootstrap的contextInitialized

初始化环境和上下文,好像没什么东西,里面很多哦。

202309152310161273.png

EurekaServerBootstrap的initEurekaServerContext

环境就不说了,我们来看上下文怎么做的。

    protected void initEurekaServerContext() throws Exception {
    		// 数据转换器
    		JsonXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(),
    				XStream.PRIORITY_VERY_HIGH);
    		XmlXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(),
    				XStream.PRIORITY_VERY_HIGH);
    		...
    		log.info("Initialized server context");
    
    		// Copy registry from neighboring eureka node 进行结点注册表同步
    		int registryCount = this.registry.syncUp();
    		this.registry.openForTraffic(this.applicationInfoManager, registryCount);
    
    		...
    	}

PeerAwareInstanceRegistryImpl的syncUp同步

将其他结点的信息同步到本地,然后注册到本地,不过貌似重试次数是0,初始化的时候没执行。

       @Override
        public int syncUp() {
            // Copy entire entry from neighboring DS node
            int count = 0;
    		//重试多少次
            for (int i = 0; ((i < serverConfig.getRegistrySyncRetries()) && (count == 0)); i++) {
                if (i > 0) {
                    try {
                        Thread.sleep(serverConfig.getRegistrySyncRetryWaitMs());
                    } catch (InterruptedException e) {
                        logger.warn("Interrupted during registry transfer..");
                        break;
                    }
                }
                Applications apps = eurekaClient.getApplications();
                for (Application app : apps.getRegisteredApplications()) {
                    for (InstanceInfo instance : app.getInstances()) {
                        try {
                            if (isRegisterable(instance)) {
                                register(instance, instance.getLeaseInfo().getDurationInSecs(), true);
                                count++;
                            }
                        } catch (Throwable t) {
                            logger.error("During DS init copy", t);
                        }
                    }
                }
            }
            return count;
        }

openForTraffic

更新续约数,开启剔除定时任务。

        @Override
        public void openForTraffic(ApplicationInfoManager applicationInfoManager, int count) {
            // Renewals happen every 30 seconds and for a minute it should be a factor of 2.
            this.expectedNumberOfClientsSendingRenews = count;
            updateRenewsPerMinThreshold();//更新续约数
            logger.info("Got {} instances from neighboring DS node", count);
            logger.info("Renew threshold is: {}", numberOfRenewsPerMinThreshold);
            this.startupTime = System.currentTimeMillis();
            if (count > 0) {
                this.peerInstancesTransferEmptyOnStartup = false;
            }
           ...
            logger.info("Changing status to UP");
            applicationInfoManager.setInstanceStatus(InstanceStatus.UP);//设置状态
            super.postInit();//开启定时剔除
        }

AbstractInstanceRegistry的postInit

开启定时剔除任务EvictionTask,每60秒一次。

        protected void postInit() {
            renewsLastMin.start();
            if (evictionTaskRef.get() != null) {
                evictionTaskRef.get().cancel();
            }
            evictionTaskRef.set(new EvictionTask());
            evictionTimer.schedule(evictionTaskRef.get(),
                    serverConfig.getEvictionIntervalTimerInMs(),
                    serverConfig.getEvictionIntervalTimerInMs());
        }

服务端初始化大致流程就这些,当然还有一些细节没讲,还是要自己去琢磨。因为很多定时任务,多线程,不太好调试,不是很方便调试,有些只能光看源码了,但是知道服务端初始化的时候干了什么,主要是EurekaServerContextEurekaServerInitializerConfiguration的初始化,比如开启了集群信心更新的定时器,开启了服务剔除的定时器,开启了续约阈值更新的定时器等。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

阅读全文