08、Eureka教程-initEurekaServerContext流程大体观察

initEurekaServerContext流程大体观察

这里用原生的Eureka来讲解,毕竟Spring-Cloud的分散的到处都是,不好观察。

步骤就写在代码里面了,注意观察哟。

protected void initEurekaServerContext() throws Exception {

    // 第一步:读取EurekaServer的配置文件
    EurekaServerConfig eurekaServerConfig = new DefaultEurekaServerConfig();

    // 第二步,创建json和xml的数据流转换器
    JsonXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH);
    XmlXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH);

    logger.info("Initializing the eureka client...");
    logger.info(eurekaServerConfig.getJsonCodecName());
    // 第三步:创建数据流的编码解码器
    ServerCodecs serverCodecs = new DefaultServerCodecs(eurekaServerConfig);

    ApplicationInfoManager applicationInfoManager = null;

    if (eurekaClient == null) {

        EurekaInstanceConfig instanceConfig = isCloud(ConfigurationManager.getDeploymentContext())
                ? new CloudInstanceConfig()
                : new MyDataCenterInstanceConfig();

        // 第四步,创建一个服务信息管理器
        applicationInfoManager = new ApplicationInfoManager(
                instanceConfig, new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get());

        // 第五步,创建EurekaClient配置文件
        EurekaClientConfig eurekaClientConfig = new DefaultEurekaClientConfig();
        // 第六步,创建一个客户端连接
        eurekaClient = new DiscoveryClient(applicationInfoManager, eurekaClientConfig);
    } else {

        applicationInfoManager = eurekaClient.getApplicationInfoManager();
    }

    // 第七步,创建一个Eureka集群注册器
    PeerAwareInstanceRegistry registry;
    if (isAws(applicationInfoManager.getInfo())) {

        registry = new AwsInstanceRegistry(
                eurekaServerConfig,
                eurekaClient.getEurekaClientConfig(),
                serverCodecs,
                eurekaClient
        );
        awsBinder = new AwsBinderDelegate(eurekaServerConfig, eurekaClient.getEurekaClientConfig(), registry, applicationInfoManager);
        awsBinder.start();
    } else {

        registry = new PeerAwareInstanceRegistryImpl(
                eurekaServerConfig,
                eurekaClient.getEurekaClientConfig(),
                serverCodecs,
                eurekaClient
        );
    }

    // 第八步,创建一个集群节点
    PeerEurekaNodes peerEurekaNodes = getPeerEurekaNodes(
            registry,
            eurekaServerConfig,
            eurekaClient.getEurekaClientConfig(),
            serverCodecs,
            applicationInfoManager
    );

    // 第九步,创建一个EurekaServer上下文
    serverContext = new DefaultEurekaServerContext(
            eurekaServerConfig,
            serverCodecs,
            registry,
            peerEurekaNodes,
            applicationInfoManager
    );

    EurekaServerContextHolder.initialize(serverContext);

    serverContext.initialize();
    logger.info("Initialized server context");

    // 第十步,从集群上复制某些信息
    int registryCount = registry.syncUp();
    registry.openForTraffic(applicationInfoManager, registryCount);

    // 第十一步,注册一些监控信息。
    EurekaMonitors.registerAllStats();
}

从上面的源码上来看,步骤还是挺多的。下一篇开始具体每个步骤分析