??URL相信大家都非常熟悉,一般在HTTP協(xié)議中,使用URL請求服務(wù)器資源,形如:http://192.168.0.112:8080/user/login.do?name=xxx&id=xxx,這個(gè)URL可以簡化成:protocol://host:port/path?param1=value1¶m1=value1&,dubbo也使用類似的URL,用于在擴(kuò)展點(diǎn)之間傳遞數(shù)據(jù),組成此URL對象的具體參數(shù)如下:
?protocol:一般是dubbo中的各種協(xié)議 如:dubbo thrift http zk 等等
?username password:用戶名 密碼
?host port:主機(jī) 端口
?path:接口名稱
?parameters:參數(shù) key value鍵值對
最終形成的URL對象如下:
dubbo://192.168.1.7:9090/com.alibaba.service1?param1=value1¶m2=value2
下面是此URL對象的構(gòu)造參數(shù):
com.alibaba.dubbo.common.URL
public URL(String protocol, String username, String password, String host, int port, String path, Map<String, String> parameters) {
if ((username == null || username.length() == 0)
&& password != null && password.length() > 0) {
throw new IllegalArgumentException("Invalid url, password without username!");
}
this.protocol = protocol;
this.username = username;
this.password = password;
this.host = host;
this.port = (port < 0 ? 0 : port);
// trim the beginning "/"
while(path != null && path.startsWith("/")) {
path = path.substring(1);
}
this.path = path;
if (parameters == null) {
parameters = new HashMap<String, String>();
} else {
parameters = new HashMap<String, String>(parameters);
}
this.parameters = Collections.unmodifiableMap(parameters);
}
??dubbo擴(kuò)展點(diǎn)的實(shí)現(xiàn)方法都包含URL對象,這樣擴(kuò)展點(diǎn)可以從URL拿到配置信息,所有的擴(kuò)展點(diǎn)自己定好配置的Key后,配置信息從URL上從最外層傳入。URL在配置傳遞上即是一條總線。URL中定義各種方法用于獲取配置信息,如獲取參數(shù)的getParameter(String key, long defaultValue)、將字符串轉(zhuǎn)成URL對象的valueOf(String url)、設(shè)置參數(shù)的setxxx()方法,在擴(kuò)展點(diǎn)適配類中需要根據(jù)接口對象方法上的@Adpative注解中的value值,將其作為key從URL對象中獲取具體的值,從文件中加載具體的擴(kuò)展點(diǎn)實(shí)現(xiàn)。