Spring Cloud 2.2.2 源碼之二十六nacos客戶端獲取配置原理一

NacosConfigService大致的結構

在這裏插入圖片描述

NacosConfigManager

上篇說了,BootstrapApplicationListener監聽方法裏會進行NacosConfigBootstrapConfiguration的註冊,然後裏面有個比較重要的注入對象NacosConfigManager。我們來看看他的構造方法做了什麼。
在這裏插入圖片描述

createConfigService創建配置服務

因爲是第一次,所以會創建,這裏你會發現他給NacosConfigManager類對象上鎖了,防止多線程多次創建NacosConfigManager而重複創建配置服務,不多囉嗦繼續。
在這裏插入圖片描述

NacosConfigProperties的assembleConfigServiceProperties一些初始化配置

在這裏插入圖片描述

NacosFactory的createConfigService

其實就是反射出NacosConfigService,然後獲取有參構造方法,反射創建實例。

    public static ConfigService createConfigService(Properties properties) throws NacosException {
        return ConfigFactory.createConfigService(properties);
    }

    public static ConfigService createConfigService(Properties properties) throws NacosException {
        try {
            Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.config.NacosConfigService");
            Constructor constructor = driverImplClass.getConstructor(Properties.class);
            ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties);
            return vendorImpl;
        } catch (Throwable e) {
            throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
        }
    }

NacosConfigService構造方法

裏面有幾個組件,ServerHttpAgenthttp請求的代理,MetricsHttpAgent包裝了ServerHttpAgent,加了計時的功能,ClientWorker做配置文件檢查。

    public NacosConfigService(Properties properties) throws NacosException {
        String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
        if (StringUtils.isBlank(encodeTmp)) {
            encode = Constants.ENCODE;//默認設置utf-8
        } else {
            encode = encodeTmp.trim();
        }
        initNamespace(properties);
        agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
        agent.start();
        worker = new ClientWorker(agent, configFilterChainManager, properties);
    }
ServerHttpAgent

ServerListManager用來管理註冊中心集羣列表,SecurityProxy用來安全驗證的,默認沒有用戶名就沒啓用,登錄驗證直接返回true。另外這裏會啓一個調度任務,每5秒執行登錄驗證,但是內部還有個令牌時間判斷的,超時了纔會去驗證。

    public ServerHttpAgent(Properties properties) throws NacosException {
        serverListMgr = new ServerListManager(properties);
        securityProxy = new SecurityProxy(properties);
        namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE);
        init(properties);//設置編碼,密碼,最大重試次數
        securityProxy.login(serverListMgr.getServerUrls());//代理登錄
        //單線程調度執行器
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.config.security.updater");
                t.setDaemon(true);
                return t;
            }
        });
        //無延遲開始調度,每5秒一次
        executorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                securityProxy.login(serverListMgr.getServerUrls());
            }
        }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS);
    }

包裝的MetricsHttpAgent
在這裏插入圖片描述

ClientWorker

http代理就是MetricsHttpAgent對象,有個過濾器管理器ConfigFilterChainManager,默認裏面沒有過濾器,可以addFilter自己加。這裏也開啓了一個單線程的執行器,執行checkConfigInfo檢查配置任務,每10毫秒一次,去檢查當前的配置數量,如果超過一個輪詢任務的限制數量,默認3000個,就開啓一個新的任務去做。

    public ClientWorker(final HttpAgent agent, final ConfigFilterChainManager configFilterChainManager, final Properties properties) {
        this.agent = agent;
        this.configFilterChainManager = configFilterChainManager;

     
        init(properties);
		
        executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker." + agent.getName());
                t.setDaemon(true);
                return t;
            }
        });
		//有cpu核數的線程,用來做長輪詢的,每次檢查配置,如果LongPollingRunnable任務的配置緩存超過一定數量,默認3000個,就要去開啓一個新任務去檢查配置
        executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker.longPolling." + agent.getName());
                t.setDaemon(true);
                return t;
            }
        });
		//配置檢查
        executor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    checkConfigInfo();
                } catch (Throwable e) {
                    LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", e);
                }
            }
        }, 1L, 10L, TimeUnit.MILLISECONDS);
    }

先介紹下這個,其他的後面說。

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

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