最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中的EurekaServer啟動詳解

 更新時間:2023年11月21日 10:00:41   作者:dalianpai  
這篇文章主要介紹了Spring中的EurekaServer啟動詳解,初始化eureka,包含eureka集群的同步和發(fā)布注冊,這個方法時重寫ServletContextListener#contextInitialized,是eureka啟動的入口了,需要的朋友可以參考下

EurekaServer啟動

看方法上的注釋,初始化eureka,包含eureka集群的同步和發(fā)布注冊,這個方法時重寫ServletContextListener#contextInitialized,是eureka啟動的入口了。

在 Servlet 容器( 例如 Tomcat、Jetty )啟動時,調用 #contextInitialized() 方法。

/**
     * Initializes Eureka, including syncing up with other Eureka peers and publishing the registry.
     *
     * @see
     * javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     */
    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
            //初始化eureka環(huán)境信息
            initEurekaEnvironment();
            //初始化eureka的上下文
            initEurekaServerContext();
 
            ServletContext sc = event.getServletContext();
            sc.setAttribute(EurekaServerContext.class.getName(), serverContext);
        } catch (Throwable e) {
            logger.error("Cannot bootstrap eureka server :", e);
            throw new RuntimeException("Cannot bootstrap eureka server :", e);
        }
    }

初始化eureka的環(huán)境

private static final String TEST = "test";
 
    private static final String ARCHAIUS_DEPLOYMENT_ENVIRONMENT = "archaius.deployment.environment";
 
    private static final String EUREKA_ENVIRONMENT = "eureka.environment";
 
    private static final String CLOUD = "cloud";
    private static final String DEFAULT = "default";
 
    private static final String ARCHAIUS_DEPLOYMENT_DATACENTER = "archaius.deployment.datacenter";
 
    private static final String EUREKA_DATACENTER = "eureka.datacenter";  
 
  protected void initEurekaEnvironment() throws Exception {
        logger.info("Setting the eureka configuration..");
 
        String dataCenter = ConfigurationManager.getConfigInstance().getString(EUREKA_DATACENTER);
        if (dataCenter == null) {
            logger.info("Eureka data center value eureka.datacenter is not set, defaulting to default");
            ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, DEFAULT);
        } else {
            ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, dataCenter);
        }
        String environment = ConfigurationManager.getConfigInstance().getString(EUREKA_ENVIRONMENT);
        if (environment == null) {
            ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_ENVIRONMENT, TEST);
            logger.info("Eureka environment value eureka.environment is not set, defaulting to test");
        }
    }

在ConfigurationManager.getConfigInstance()中,其實就是初始化ConfigurationManager的實例

(1)創(chuàng)建一個ConcurrentCompositeConfiguration實例,代表了所謂的配置,包括了eureka需要的所有的配置。在初始化這個實例的時候,調用了clear()方法,fireEvent()發(fā)布了一個事件(EVENT_CLEAR),fireEvent()這個方法其實是父類的方法,牽扯比較復雜的另外一個項目(ConfigurationManager本身不是屬于eureka的源碼,是屬于netflix config項目的源碼)。

/**
     * Creates an empty CompositeConfiguration object which can then
     * be added some other Configuration files
     */
    public ConcurrentCompositeConfiguration()
    {
        clear();
    }
 
    @Override
    public final void clear()
    {
        fireEvent(EVENT_CLEAR, null, null, true);
        configList.clear();
        namedConfigurations.clear();
        // recreate the in memory configuration
        containerConfiguration = new ConcurrentMapConfiguration();
        containerConfiguration.setThrowExceptionOnMissing(isThrowExceptionOnMissing());
        containerConfiguration.setListDelimiter(getListDelimiter());
        containerConfiguration.setDelimiterParsingDisabled(isDelimiterParsingDisabled());
        containerConfiguration.addConfigurationListener(eventPropagater);
        configList.add(containerConfiguration);
        
        overrideProperties = new ConcurrentMapConfiguration();
        overrideProperties.setThrowExceptionOnMissing(isThrowExceptionOnMissing());
        overrideProperties.setListDelimiter(getListDelimiter());
        overrideProperties.setDelimiterParsingDisabled(isDelimiterParsingDisabled());
        overrideProperties.addConfigurationListener(eventPropagater);
        
        fireEvent(EVENT_CLEAR, null, null, false);
        containerConfigurationChanged = false;
        invalidate();
    }

(2)就是往上面的那個ConcurrentCompositeConfiguration實例加入了一堆別的config,然后搞完了以后,就直接返回了這個實例,就是作為所謂的那個配置的單例

(3)初始化數據中心的配置,如果沒有配置的話,就是DEFAULT data center

(4)初始化eurueka運行的環(huán)境,如果你沒有配置的話,默認就給你設置為test環(huán)境

(5)initEurekaEnvironment的初始化環(huán)境的邏輯

初始化eureka的上下文

/**
     * init hook for server context. Override for custom logic.
     */
    protected void initEurekaServerContext() throws Exception {
        //加載eureka-server。properties文件的配置
        EurekaServerConfig eurekaServerConfig = new DefaultEurekaServerConfig();
 
        // For backward compatibility
        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,對應的其實是一個eureka-client。
        ApplicationInfoManager applicationInfoManager = null;
 
        //初始化eureka-server內部的一個eureka-client,用來和其他eureka-server節(jié)點用來注冊通信的
        if (eurekaClient == null) {
            //CommonConstants. 用來獲取client信息
            EurekaInstanceConfig instanceConfig = isCloud(ConfigurationManager.getDeploymentContext())
                    ? new CloudInstanceConfig()
                    : new MyDataCenterInstanceConfig();
            
            applicationInfoManager = new ApplicationInfoManager(
                    instanceConfig, new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get());
            
            EurekaClientConfig eurekaClientConfig = new DefaultEurekaClientConfig();
            eurekaClient = new DiscoveryClient(applicationInfoManager, eurekaClientConfig);
        } else {
            applicationInfoManager = eurekaClient.getApplicationInfoManager();
        }
 
        //處理注冊的相關事情
        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
            );
        }
 
        //處理peer節(jié)點
        PeerEurekaNodes peerEurekaNodes = getPeerEurekaNodes(
                registry,
                eurekaServerConfig,
                eurekaClient.getEurekaClientConfig(),
                serverCodecs,
                applicationInfoManager
        );
 
        //完成eureka-server上下文的構建以及初始化過程
        serverContext = new DefaultEurekaServerContext(
                eurekaServerConfig,
                serverCodecs,
                registry,
                peerEurekaNodes,
                applicationInfoManager
        );
 
        EurekaServerContextHolder.initialize(serverContext);
 
        serverContext.initialize();
        logger.info("Initialized server context");
 
        // 第六步,處理一點善后的事情,從相鄰的eureka節(jié)點拷貝注冊信息
        int registryCount = registry.syncUp();
        //這個方法就是打開注冊表,可以接收請求
        registry.openForTraffic(applicationInfoManager, registryCount);
 
        // 注冊所有的監(jiān)控
        EurekaMonitors.registerAllStats();
    }

配置文件讀取

public DefaultEurekaServerConfig() {
        init();
    }
 
    private void init() {
        String env = ConfigurationManager.getConfigInstance().getString(
                EUREKA_ENVIRONMENT, TEST);
        ConfigurationManager.getConfigInstance().setProperty(
                ARCHAIUS_DEPLOYMENT_ENVIRONMENT, env);
 
        String eurekaPropsFile = EUREKA_PROPS_FILE.get();
        try {
            // ConfigurationManager
            // .loadPropertiesFromResources(eurekaPropsFile);
            ConfigurationManager
                    .loadCascadedPropertiesFromResources(eurekaPropsFile);
        } catch (IOException e) {
            logger.warn(
                    "Cannot find the properties specified : {}. This may be okay if there are other environment "
                            + "specific properties or the configuration is installed with a different mechanism.",
                    eurekaPropsFile);
        }
    }

加載eureka-server.properties的過程

(1)創(chuàng)建了一個DefaultEurekaServerConfig對象

(2)創(chuàng)建DefaultEurekaServerConfig對象的時候,在里面會有一個init方法

(3)先是將eureka-server.properties中的配置加載到了一個Properties對象中,然后將Properties對象中的配置放到ConfigurationManager中去,此時ConfigurationManager中去就有了所有的配置了

(4)然后DefaultEurekaServerConfig提供的獲取配置項的各個方法,都是通過硬編碼的配置項名稱,從DynamicPropertyFactory中獲取配置項的值,DynamicPropertyFactory是從ConfigurationManager那兒來的,所以也包含了所有配置項的值

(5)在獲取配置項的時候,如果沒有配置,那么就會有默認的值,全部屬性都是有默認值的

創(chuàng)建 Eureka-Server 請求和響應編解碼器

logger.info("Initializing the eureka client...");
logger.info(eurekaServerConfig.getJsonCodecName());
ServerCodecs serverCodecs = new DefaultServerCodecs(eurekaServerConfig);

創(chuàng)建Eureka-Client

//初始化一個ApplicationInfoManager,對應的其實是一個eureka-client。        ApplicationInfoManager applicationInfoManager = null;        //初始化eureka-server內部的一個eureka-client,用來和其他eureka-server節(jié)點用來注冊通信的        if (eurekaClient == null) {            //CommonConstants. 用來獲取client信息            EurekaInstanceConfig instanceConfig = isCloud(ConfigurationManager.getDeploymentContext())                    ? new CloudInstanceConfig()                    : new MyDataCenterInstanceConfig();                        applicationInfoManager = new ApplicationInfoManager(                    instanceConfig, new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get());                        EurekaClientConfig eurekaClientConfig = new DefaultEurekaClientConfig();            eurekaClient = new DiscoveryClient(applicationInfoManager, eurekaClientConfig);        } else {            applicationInfoManager = eurekaClient.getApplicationInfoManager();        }

在new MyDataCenterInstanceConfig()這個無參構造中,最終會去讀取eureka-client.properties的配置,去提供一些默認值。

因為eureka-server本身也是一個eureka-client,因為當組成集群的時候,它自己也要向別的服務端進行注冊

//初始化一個ApplicationInfoManager,對應的其實是一個eureka-client。
        ApplicationInfoManager applicationInfoManager = null;
 
        //初始化eureka-server內部的一個eureka-client,用來和其他eureka-server節(jié)點用來注冊通信的
        if (eurekaClient == null) {
            //CommonConstants. 用來獲取client信息
            EurekaInstanceConfig instanceConfig = isCloud(ConfigurationManager.getDeploymentContext())
                    ? new CloudInstanceConfig()
                    : new MyDataCenterInstanceConfig();
            
            applicationInfoManager = new ApplicationInfoManager(
                    instanceConfig, new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get());
            
            EurekaClientConfig eurekaClientConfig = new DefaultEurekaClientConfig();
            eurekaClient = new DiscoveryClient(applicationInfoManager, eurekaClientConfig);
        } else {
            applicationInfoManager = eurekaClient.getApplicationInfoManager();
        }

初始化 EurekaServerContextHolder

EurekaServerContextHolder.initialize(serverContext);

初始化 Eureka-Server 上下文

@PostConstruct
    @Override
    public void initialize() {
        logger.info("Initializing ...");
        //啟動 Eureka-Server 集群節(jié)點集合(復制)
        peerEurekaNodes.start();
        try {
            // 初始化 應用實例信息的注冊表
            registry.init(peerEurekaNodes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        logger.info("Initialized");
    }

從其他 Eureka-Server 拉取注冊信息

// 第六步,處理一點善后的事情,從相鄰的eureka節(jié)點拷貝注冊信息
        int registryCount = registry.syncUp();
        //這個方法就是打開注冊表,可以接收請求
        registry.openForTraffic(applicationInfoManager, registryCount);

注冊監(jiān)控

EurekaMonitors.registerAllStats();

到此這篇關于Spring中的EurekaServer啟動詳解的文章就介紹到這了,更多相關EurekaServer啟動內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中String.split()用法小結

    Java中String.split()用法小結

    這篇文章主要介紹了Java中String.split()用法小結的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • Java中的隨機數Random

    Java中的隨機數Random

    這篇文章主要介紹了Java中的隨機數Random,關于隨機數的介紹不設置隨機數種子,你每次隨機抽樣得到的數據都是不一樣的。設置了隨機數種子,能夠確保每次抽樣的結果一樣,下面來了解具體的詳細內容介紹吧
    2022-03-03
  • java模擬hibernate一級緩存示例分享

    java模擬hibernate一級緩存示例分享

    這篇文章主要介紹了java模擬hibernate一級緩存示例,需要的朋友可以參考下
    2014-03-03
  • 解決struts2 攔截器修改request的parameters參數失敗的問題

    解決struts2 攔截器修改request的parameters參數失敗的問題

    這篇文章主要介紹了解決struts2 攔截器修改request的parameters參數失敗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring Boot應用的極速部署腳本示例代碼

    Spring Boot應用的極速部署腳本示例代碼

    最近在工作中遇到了一個問題,需要極速的部署Spring Boot應用,發(fā)現網上這方面的資料較少,所以自己來總結下,這篇文章主要給大家介紹了關于Spring Boot應用的極速部署腳本的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • Java解決浮點數計算不精確問題的方法詳解

    Java解決浮點數計算不精確問題的方法詳解

    在 Java 中,浮點數計算不精確問題指的是使用浮點數進行運算時,由于浮點數的內部表示方式和十進制數的表示方式存在差異,導致計算結果可能出現誤差,本文就給大家介紹一下Java如何解決浮點數計算不精確問題,需要的朋友可以參考下
    2023-09-09
  • Java反射及性能詳細

    Java反射及性能詳細

    這篇文章主要介紹了Java反射及性能,現如今的java工程中,反射的使用無處無在。無論是設計模式中的代理模式,還是紅透半邊天的Spring框架中的IOC,AOP等等,都存在大量反射的影子。下面我們就對該話題進行詳細介紹,感興趣的小伙伴可以參考一下
    2021-10-10
  • mapstruct中的@Mapper注解的基本用法

    mapstruct中的@Mapper注解的基本用法

    在MapStruct中,@Mapper注解是核心注解之一,用于標記一個接口或抽象類為MapStruct的映射器(Mapper),本文給大家介紹mapstruct中的@Mapper注解的相關知識,感興趣的朋友一起看看吧
    2025-06-06
  • 淺談servlet3異步原理與實踐

    淺談servlet3異步原理與實踐

    本篇文章主要介紹了servlet3異步原理與實踐,詳細的介紹了servlet和異步的流程使用,具有一定的參考價值,有興趣的可以了解一下
    2017-10-10
  • SpringBoot發(fā)送html郵箱驗證碼功能

    SpringBoot發(fā)送html郵箱驗證碼功能

    這篇文章主要介紹了SpringBoot發(fā)送html郵箱驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12

最新評論

大邑县| 常州市| 明水县| 金门县| 湘潭市| 东安县| 寿宁县| 阜康市| 竹溪县| 炎陵县| 巧家县| 六枝特区| 阿勒泰市| 东兰县| 茌平县| 康定县| 保德县| 建始县| 张家港市| 唐山市| 张家港市| 阿瓦提县| 安徽省| 边坝县| 石林| 蕲春县| 大新县| 建瓯市| 和田市| 昌宁县| 正阳县| 即墨市| 长岭县| 阳城县| 汤原县| 荔波县| 定陶县| 达日县| 汾西县| 开鲁县| 九寨沟县|