您的位置:首页 > 运维架构 > 网站架构

dubbo_URL分析

2015-10-21 11:03 513 查看

    看dubbo源码是我第一次研究开源框架的源码,也不知道从何处下手,限于自身水平也就一个类一个类的先看着找找感觉吧。希望自己能够坚持下去,在这里先给自己加个油吧!

    dubbo-common包是公共逻辑模块,包括Util类和通用模型。

    DUBBO中所有配置最终都将转换为URL表示,并由服务提供方生成,经注册中心传递给消费方,各属性对应URL的参数,参见配置项一览表中的"对应URL参数"列。

    URL格式: protocol://username:password@host:port/path?key=value&key=value

      Dubbo框架是以URL为总线的模式,即运行过程中所有的状态数据信息都可以通过URL来获取,比如当前系统采用什么序列化,采用什么通信,采用什么负载均衡等信息,都是通过URL的参数来呈现的,所以在框架运行过程中,运行到某个阶段需要相应的数据,都可以通过对应的Key从URL的参数列表中获取,比如在cluster模块,到服务调用触发到该模块,则会从URL中获取当前调用服务的负载均衡策略,以及mock信息等。

    那我首先就先看看URL类中都有什么东西吧。

1、变量:

private final String protocol;

private final String username;

private final String password;

private final String host;

private final int port;

private final String path;

private final Map<String, String> parameters;

// ==== cache ====

private volatile transient Map<String, Number> numbers;

private volatile transient Map<String, URL> urls;

private volatile transient String ip;

private volatile transient String full;

private volatile transient String identity;

private volatile transient String parameter;

private volatile transient String string;

整体来看包含final类型的URL基本信息变量,还有用于缓存用的volatile transient类型变量。

2、方法:

此类方法很多,但并不复杂,大部分是一些构造方法和一些参数的get/set方法,以及一些其它方法.

其中我认为重要一点的方法如下:

(1)构造方法

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);
this.path = path;
// trim the beginning "/"
while(path != null && path.startsWith("/")) {
path = path.substring(1);
}
if (parameters == null) {
parameters = new HashMap<String, String>();
} else {
parameters = new HashMap<String, String>(parameters);
}
this.parameters = Collections.unmodifiableMap(parameters);
}

其中Collections.unmodifiableMap(parameters);是为了返回一个只读访问的map类型,保证安全。

(2)valueOf(String url)用来将url字符串转为URL对象,方法实现很简单,就是字符串的拆分。

public static URL valueOf(String url) {
if (url == null || (url = url.trim()).length() == 0) {
throw new IllegalArgumentException("url == null");
}
String protocol = null;
String username = null;
String password = null;
String host = null;
int port = 0;
String path = null;
Map<String, String> parameters = null;
int i = url.indexOf("?"); // seperator between body and parameters

b5e8
if (i >= 0) {
String[] parts = url.substring(i + 1).split("\\&");
parameters = new HashMap<String, String>();
for (String part : parts) {
part = part.trim();
if (part.length() > 0) {
int j = part.indexOf('=');
if (j >= 0) {
parameters.put(part.substring(0, j), part.substring(j + 1));
} else {
parameters.put(part, part);
}
}
}
url = url.substring(0, i);
}
i = url.indexOf("://");
if (i >= 0) {
if(i == 0) throw new IllegalStateException("url missing protocol: \"" + url + "\"");
protocol = url.substring(0, i);
url = url.substring(i + 3);
}
else {
// case: file:/path/to/file.txt
i = url.indexOf(":/");
if(i>=0) {
if(i == 0) throw new IllegalStateException("url missing protocol: \"" + url + "\"");
protocol = url.substring(0, i);
url = url.substring(i + 1);
}
}

i = url.indexOf("/");
if (i >= 0) {
path = url.substring(i + 1);
url = url.substring(0, i);
}
i = url.indexOf("@");
if (i >= 0) {
username = url.substring(0, i);
int j = username.indexOf(":");
if (j >= 0) {
password = username.substring(j + 1);
username = username.substring(0, j);
}
url = url.substring(i + 1);
}
i = url.indexOf(":");
if (i >= 0 && i < url.length() - 1) {
port = Integer.parseInt(url.substring(i + 1));
url = url.substring(0, i);
}
if(url.length() > 0) host = url;
return new URL(protocol, username, password, host, port, path, parameters);
}

(3)buildString和buildParameters方法,是将URL对象中的变量,通过StringBuilder对象转换成URL字符串格式。

private String buildString(boolean appendUser, boolean appendParameter, boolean useIP, boolean useService, String... parameters) {
StringBuilder buf = new StringBuilder();
if (protocol != null && protocol.length() > 0) {
buf.append(protocol);
buf.append("://");
}
if (appendUser && username != null && username.length() > 0) {
buf.append(username);
if (password != null && password.length() > 0) {
buf.append(":");
buf.append(password);
}
buf.append("@");
}
String host;
if (useIP) {
host = getIp();
} else {
host = getHost();
}
if(host != null && host.length() > 0) {
buf.append(host);
if (port > 0) {
buf.append(":");
buf.append(port);
}
}
String path;
if (useService) {
path = getServiceKey();
} else {
path = getPath();
}
if (path != null && path.length() > 0) {
buf.append("/");
buf.append(path);
}
if (appendParameter) {
buildParameters(buf, true, parameters);
}
return buf.toString();
}
private void buildParameters(StringBuilder buf, boolean concat, String[] parameters) {
if (getParameters() !=null && getParameters().size() > 0) {
List<String> includes = (parameters == null || parameters.length == 0 ? null : Arrays.asList(parameters));
boolean first = true;
for (Map.Entry<String, String> entry : new TreeMap<String, String>(getParameters()).entrySet()) {
if (entry.getKey() != null && entry.getKey().length() > 0
&& (includes == null || includes.contains(entry.getKey()))) {
if (first) {
if (concat) {
buf.append("?");
}
first = false;
} else {
buf.append("&");
}
buf.append(entry.getKey());
buf.append("=");
buf.append(entry.getValue() == null ? "" : entry.getValue().trim());
}
}
}
}


本类的内容我主要看了这些,第一次看代码,大部分都看懂了,算是给自己的一份信心吧。加油。这里只是当自己的一些笔记进行了记录,没有太详细的分析,希望在以后随着自己理解的深入在慢慢补充。




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dubbo 架构 url