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);
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值