簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

1:消費者端dubbo的yml配置

dubbo: consumer: timeout: 300000 protocol: name: dubbo port: -1 cloud: subscribed-services: order-server# subscribed-services: hello-server,account-server,storage-server,order-server

簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

2:按住ctrl + 滑鼠左擊subscribed-services如下圖:

簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

3:這裡是對應的setter方法,上面找到定義地方:

/** * All services of Dubbo。 */ public static final String ALL_DUBBO_SERVICES = “*”; /** * The subscribed services, the default value is “*”。 The multiple value will use * comma(“,”) as the separator。 * * @see #ALL_DUBBO_SERVICES */ private String subscribedServices = ALL_DUBBO_SERVICES;

讀一讀就知道答案了。預設為*,多個值時候用

隔開。

接下來,走走最表面的流程,就是看看怎麼處理我們輸入的資料的,至於是怎麼找到提供者的,先不管。

4:游標點到類DubboCloudProperties上面,這裡ctrl+單擊是點不進去的,但是你點一下有提醒:

No usages found in Project Files Press Ctrl+Alt+F7 again to search in ‘Project and Libraries

5:跟著提醒,Ctrl+Alt+F7,如果提醒關閉了,就雙擊F7,出來下圖

簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

6:直接回車,就是高亮的這一行。回去複製ubscribedServices,不要開頭的s,不管大寫還是小寫,Ctrl+F搜出來看看。

7:透過點擊向上向下的箭頭,或者F3(下一個)/Shift+F3(上一個)來讀一讀原始碼。這裡我們看到他在237行時候進行了初始化。

簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

8:老樣子,ctrl+單擊initSubscribedServices()方法。

簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

讀一下,如果

ALL_DUBBO_SERVICES

等於我們輸入的提供者,就是輸出巴拉巴拉。

9: 那麼ALL_DUBBO_SERVICES是啥,ctrl點,發現又跳回第一個檔案了:

@ConfigurationProperties(prefix = CONFIG_PROPERTY_PREFIX)public class DubboCloudProperties { /** * All services of Dubbo。 */ public static final String ALL_DUBBO_SERVICES = “*”; // 這裡是預設為*,如果set方法沒有執行,那麼get時候獲得的就是* private String subscribedServices = ALL_DUBBO_SERVICES; /* 。。。 */ }:10現在測試一下輸出那個東西,啟動一個提供者,然後消費者訂閱的提供者寫成*或者註釋掉。啟動消費者。

10:現在測試一下輸出那個東西,啟動一個提供者,然後消費者訂閱的提供者寫成*或者註釋掉。啟動消費者。

檢視日誌:

2021-05-27 16:28:24。950 WARN 6564 ——- [client。listener] a。c。d。m。r。DubboServiceMetadataRepository : Current application will subscribe all services(size:20) in registry, a lot of memory and CPU cycles may be used, thus it’s strongly recommend you using the externalized property ‘dubbo。cloud。subscribed-services’ to specify the services

與步驟8中一致。

11:接著讀else,如果我們填入內容了,就subscribedServices()後加入到那個集合中

newSubscribedServices。addAll(dubboCloudProperties。subscribedServices());

ctrl點

subscribedServices()

,然後發現又轉到第一個檔案了:

簡單讀讀原始碼 - dubbo多提供者(provider)配置方法

再繼續讀一下:使用

commaDelimitedListToStringArray

將我們輸入的東西轉化了字串陣列,處理返回。

12:我們來看看commaDelimitedListToStringArray怎麼處理的,找到匯入它的地方:

import static org。springframework。util。StringUtils。commaDelimitedListToStringArray;

然後ctrl點:

/** * Convert a comma delimited list (e。g。, a row from a CSV file) into an * array of strings。 * @param str the input {@code String} (potentially {@code null} or empty) * @return an array of strings, or the empty array in case of empty input */ public static String[] commaDelimitedListToStringArray(@Nullable String str) { return delimitedListToStringArray(str, “,”); }

這裡與標題3。中

The multiple value will use comma(“,”) as the separator。

對應。

結論:

不寫或者*會訂閱所有的。

寫多就使用

隔開。

原文連結:https://www。cnblogs。com/Ddlm2wxm/p/14818979。html