00.[dubbo源码解析]-[配置][辅助]URL-dubbo的统一数据模型

result: protocol://username:password@host:port/path?key=value&key=value

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

[源码]org.apache.dubbo.common.URL

 */
/**
* @description :所有配置最终都将转换为 URL 表示,并由服务提供方生成,经注册中心传递给消费方,各属性对应 URL 的参数,参见配置项一览表中的 "对应URL参数" 列。
 * @result: protocol://username:password@host:port/path?key=value&key=value
*/
public /**final**/ class URL implements Serializable {


    private static final long serialVersionUID = -1985165475234910535L;
    /**
     * 协议名
     */
    private final String protocol;
    /**
     * 用户名
     */
    private final String username;
    /**
     * 密码
     */
    private final String password;


    // by default, host to registry
    /**
     * by default, host to registry
     * 地址(默认为注册地址)
     */
    private final String host;


    // by default, port to registry
    /**
     * by default, port to registry
     * 端口(默认为注册端口)
     */
    private final int port;
    /**
     * 路径(服务名)
     */
    private final String path;
    /**
     * 参数集合
     */
    private final Map<String, String> parameters;


    // ==== cache ====
    /**
    * transient: 不会被序列化到
    */
    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;

  // 省略。。。。
      /**
    * 用StringBuilder来拼接字符串
    */
    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();
    }
    //省略。。。。
        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);
        }
        // Collections.unmodifiableMap()返回值不可被修改
        this.parameters = Collections.unmodifiableMap(parameters);
        
    }

你可能感兴趣的:(源码,dubbo)