1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        【Spring Cloud】Eureka-Client 源碼解讀

        共 4475字,需瀏覽 9分鐘

         ·

        2021-06-02 09:12

        一、前言

        看源碼:抓大放小,先主流程,再細枝末節(jié)。

        用技巧連蒙帶猜:

        1. 看方法名(英文名)

        2. 看注釋

        搭建環(huán)境:

        // 定位:eureka-examples 模塊下
        // 修改 ExampleEurekaClient.java

        // 1. 增加一個方法,用于初始化環(huán)境變量,方便調(diào)試
        private static void injectEurekaConfiguration() throws UnknownHostException {
        String myHostName = InetAddress.getLocalHost().getHostName();
        String myServiceUrl = "http://" + myHostName + ":8080/v2/";

        System.setProperty("eureka.region", "default");
        System.setProperty("eureka.name", "eureka");
        System.setProperty("eureka.vipAddress", "eureka.mydomain.net");
        System.setProperty("eureka.port", "8080");
        System.setProperty("eureka.preferSameZone", "false");
        System.setProperty("eureka.shouldUseDns", "false");
        System.setProperty("eureka.shouldFetchRegistry", "false");
        System.setProperty("eureka.serviceUrl.defaultZone", myServiceUrl);
        System.setProperty("eureka.serviceUrl.default.defaultZone", myServiceUrl);
        System.setProperty("eureka.awsAccessId", "fake_aws_access_id");
        System.setProperty("eureka.awsSecretKey", "fake_aws_secret_key");
        System.setProperty("eureka.numberRegistrySyncRetries", "0");
        }


        // 2. 在 main 方法添加
        public static void main(String[] args) throws UnknownHostException {

        // 添加如下這行
        injectEurekaConfiguration();
        ... ...
        }
        復(fù)制代碼



        二、從源碼中學(xué)到了什么



        三、直接懟源碼

        (1)eureka-client 如何啟動(初始化)?

        代碼如下:

        // 定位:com.netflix.eureka.ExampleEurekaClient.java
        public static void main(String[] args) throws UnknownHostException {
        // 1. 初始化 eureka 環(huán)境變量
        injectEurekaConfiguration();
        // 2. 創(chuàng)建 eureka 服務(wù)
        ExampleEurekaClient sampleClient = new ExampleEurekaClient();
        // 3. 創(chuàng)建服務(wù)實例管理器
        ApplicationInfoManager applicationInfoManager =
        initializeApplicationInfoManager(new MyDataCenterInstanceConfig());
        // 4. 創(chuàng)建 eureka-client
        EurekaClient client =
        initializeEurekaClient(applicationInfoManager, new DefaultEurekaClientConfig());

        ... ...
        }
        復(fù)制代碼

        過程如下:

        1. 初始化 eureka 環(huán)境變量

        2. 創(chuàng)建 eureka 服務(wù),會有一個 eureka-client

        3. 創(chuàng)建服務(wù)實例管理器

          ApplicationInfoManager applicationInfoManager = initializeApplicationInfoManager(new MyDataCenterInstanceConfig());
          復(fù)制代碼
          1. 構(gòu)建服務(wù)實例(InstanceInfo

          2. 構(gòu)建服務(wù)實例管理器(ApplicationInfoManager

        4. 創(chuàng)建 eureka-client(通過構(gòu)造 DiscoveryClient

          • 處理配置

          • 服務(wù)的注冊和注冊表的抓取(初始化網(wǎng)絡(luò)通信組件)

          • 創(chuàng)建幾個線程池,啟動調(diào)度任務(wù)

          • 注冊監(jiān)控項



        (2)eureka-client 如何服務(wù)注冊的?

        針對注冊,提出問題:

        1. 什么時候進行服務(wù)注冊?初始化 eureka-client 時候

        eureka-client 的服務(wù)注冊,是在 InstanceInfoReplicator 中完成的。

        1. 服務(wù)注冊做哪些操作?主要發(fā)送 HTTP 請求

        針對這個兩個問題,來看下源碼。雖然這部分的源碼寫的不好,但也可以學(xué)習了解下他人的思路。

        這部分源碼比較難找,實際是在創(chuàng)建 DiscoveryClientinitScheduledTasks()(初始化調(diào)度任務(wù))

        // 定位:com.netflix.discovery.DiscoveryClient.java
        private void initScheduledTasks() {
        // InstanceRegisterManager:實例注冊管理器,專門來管理實例注冊
        // 傳參:默認 40秒
        instanceInfoReplicator.start(...);
        }
        復(fù)制代碼
        1. 啟動實例注冊管理器(InstanceRegisterManager

        // 定位:com.netflix.discovery.InstanceInfoReplicator.java
        public void start(int initialDelayMs) {
        // 原子操作
        if (started.compareAndSet(false, true)) {
        // 1. 先設(shè)置 isDirty = true
        instanceInfo.setIsDirty();
        // 2. 調(diào)度器執(zhí)行,將自身傳入,并且調(diào)度時間默認為 40 秒
        // 所以會執(zhí)行 InstanceInfoReplicator.run() 方法
        Future next = scheduler.schedule(this, initialDelayMs, TimeUnit.SECONDS);
        scheduledPeriodicRef.set(next);
        }
        }
        復(fù)制代碼
        1. 執(zhí)行 InstanceInfoReplicator.run()

        // 定位:com.netflix.discovery.InstanceInfoReplicator.java
        // 會發(fā)現(xiàn):class InstanceInfoReplicator implements Runnable,是可創(chuàng)建線程的。
        public void run() {
        try {
        // 1. 刷新了服務(wù)實例的信息,拿到服務(wù)的狀態(tài)
        discoveryClient.refreshInstanceInfo();
        Long dirtyTimestamp = instanceInfo.isDirtyWithTime();
        if (dirtyTimestamp != null) {
        // 2. 注冊:因為之前已經(jīng)設(shè)置 isDirty = true,所以下面直接注冊
        discoveryClient.register();
        // 3. 設(shè)置 isDirty = false
        instanceInfo.unsetIsDirty(dirtyTimestamp);
        }
        } catch (Throwable t) {
        logger.warn("There was a problem with the instance info replicator", t);
        } finally {
        // 4. 再次把自己丟進調(diào)度線程中
        Future next =
        scheduler.schedule(this, replicationIntervalSeconds, TimeUnit.SECONDS);
        scheduledPeriodicRef.set(next);
        }
        }
        復(fù)制代碼
        1. 重要:注冊 discoveryClient.register();

        // 定位:com.netflix.discovery.DiscoveryClient.java
        boolean register() throws Throwable {
        EurekaHttpResponse<Void> httpResponse;
        try {
        // 發(fā)送 HTTP 請求
        httpResponse = eurekaTransport.registrationClient.register(instanceInfo);
        ... ...
        }
        ... ...
        return httpResponse.getStatusCode() == 204;
        }


        作者:卷卷啊
        鏈接:https://juejin.cn/post/6968080788868825118
        來源:掘金
        著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。


        瀏覽 38
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            爆操美女视频 | 大香蕉人人网 | 女人的天堂av | 日本xxxxxxxxx8泡妞 | 啊~嗯~ 我下水好多水网站 | 波多野结衣操逼视频 | 男人天堂一区 | 五月丁香成人 | 美女张开腿让男人捅视频 | 成人特级片 |