Spring Cloud 2.2.2 源碼之四十四nacos客戶端服務發現任務解析一

Spring Cloud 2.2.2 源碼之四十四nacos客戶端服務發現任務解析一

服務發現任務圖

在這裏插入圖片描述

NacosWatch

這個就不多說了,SmartLifecycle這個已經很熟悉了吧,調用start,裏面開啓任務,30秒一次,發送HeartbeatEvent,不過貌似現在沒有監聽器監聽。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

BeatTask心跳任務

服務註冊默認是臨時結點,所以要有心跳。
在這裏插入圖片描述
在這裏插入圖片描述
其實就是發送心跳到/nacos/v1/ns/instance/beat,然後根據返回的信息設置屬性,如果是沒找到就註冊服務。默認第一次是非輕量級的心跳,會發body的,body就是一堆服務信息的url編碼的字符串,後面就不需要了,默認間隔5秒。

        @Override
        public void run() {
            if (beatInfo.isStopped()) {
                return;
            }
            long nextTime = beatInfo.getPeriod();
            try {
            	//直接http調用,如果是輕量級的心跳沒有body,否則會帶body
                JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
                long interval = result.getIntValue("clientBeatInterval");//心跳間隔
                boolean lightBeatEnabled = false;
                //根據服務端發開的是否要進行輕量級心跳進行改變
                if (result.containsKey(CommonParams.LIGHT_BEAT_ENABLED)) {
                    lightBeatEnabled = result.getBooleanValue(CommonParams.LIGHT_BEAT_ENABLED);
                }
                BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
                if (interval > 0) {
                    nextTime = interval;
                }
                int code = NamingResponseCode.OK;
                if (result.containsKey(CommonParams.CODE)) {
                    code = result.getIntValue(CommonParams.CODE);
                }
                //如果不存在,就註冊
                if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {
                    Instance instance = new Instance();
                    instance.setPort(beatInfo.getPort());
                    instance.setIp(beatInfo.getIp());
                    instance.setWeight(beatInfo.getWeight());
                    instance.setMetadata(beatInfo.getMetadata());
                    instance.setClusterName(beatInfo.getCluster());
                    instance.setServiceName(beatInfo.getServiceName());
                    instance.setInstanceId(instance.getInstanceId());
                    instance.setEphemeral(true);
                    try {
                        serverProxy.registerService(beatInfo.getServiceName(),
                            NamingUtils.getGroupName(beatInfo.getServiceName()), instance);
                    } catch (Exception ignore) {
                    }
                }
            } catch (NacosException ne) {
                NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
                    JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg());

            }
            //根據間隔進行下一次調用
            executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
        }
    

在這裏插入圖片描述

securityProxy.login(getServerList());安全驗證

這個以前說過,就是安全驗證的,沒用戶名密碼就直接返回了。

    public boolean login(List<String> servers) {

        try {//沒超時直接返回
            if ((System.currentTimeMillis() - lastRefreshTime) < TimeUnit.SECONDS.toMillis(tokenTtl - tokenRefreshWindow)) {
                return true;
            }

            for (String server : servers) {
                if (login(server)) {//登錄和計時
                    lastRefreshTime = System.currentTimeMillis();
                    return true;
                }
            }
        } catch (Throwable ignore) {
        }

        return false;
    }

還有其他任務下次講。

好了,今天就到這裏了,希望對學習理解有幫助,大神看見勿噴,僅爲自己的學習理解,能力有限,請多包涵。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章