美文网首页
Dubbo源码分析(一) ReferenceConfig

Dubbo源码分析(一) ReferenceConfig

作者: skyguard | 来源:发表于2018-11-12 10:31 被阅读0次

现在我们来分析一下Dubbo的源码。我们知道,目前RPC框架用的最多的就是Dubbo,所以在看Dubbo源码之前我们需要先了解一下Dubbo的节点都有哪些。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点dubbo里面抽象出了服务提供方(Provider)和服务消费方(Consumer)两个角色。而连接Provider和Consumer的就是注册中心(Registry),Provider把服务暴露在Registry上,而Consumer通过Registry来调用暴露在Registry上面的服务。
节点角色说明:
Provider: 暴露服务的服务提供方
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
我们先来看一下Dubbo的配置。这里我们先介绍一个类AbstractConfig。这个类包括了基本的Dubbo服务的配置。主要是用来读取配置参数和properties配置到配置对象中。provier的配置有ProviderConfig,consumer的配置有ConsumerConfig,暴露服务的配置有ServiceConfig,监控的配置有MonitorConfig。而服务引用关系的就是ReferenceConfig。现在我们来说一下服务间引用关系的配置类ReferenceConfig。
先来看一下init方法

private void init() {
    // 已经初始化,直接返回
    if (initialized) {
        return;
    }
    initialized = true;
    // 校验接口名非空
    if (interfaceName == null || interfaceName.length() == 0) {
        throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
    }
    // 拼接属性配置(环境变量 + properties 属性)到 ConsumerConfig 对象
    // get consumer's global configuration
    checkDefault();
    // 拼接属性配置(环境变量 + properties 属性)到 ReferenceConfig 对象
    appendProperties(this);
    // 若未设置 `generic` 属性,使用 `ConsumerConfig.generic` 属性。
    if (getGeneric() == null && getConsumer() != null) {
        setGeneric(getConsumer().getGeneric());
    }
    if (ProtocolUtils.isGeneric(getGeneric())) {
        interfaceClass = GenericService.class;
    } else {
        try {
            interfaceClass = Class.forName(interfaceName, true, Thread.currentThread().getContextClassLoader());
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
        // 校验接口和方法
        checkInterfaceAndMethods(interfaceClass, methods);
    }
    // 直连提供者,
    // 【直连提供者】第一优先级,通过 -D 参数指定 ,例如 java -Dcom.alibaba.xxx.XxxService=dubbo://localhost:20890
    String resolve = System.getProperty(interfaceName);
    String resolveFile = null;
    // 【直连提供者】第二优先级,通过文件映射,例如 com.alibaba.xxx.XxxService=dubbo://localhost:20890
    if (resolve == null || resolve.length() == 0) {
        // 默认先加载,`${user.home}/dubbo-resolve.properties` 文件 ,无需配置
        resolveFile = System.getProperty("dubbo.resolve.file");
        if (resolveFile == null || resolveFile.length() == 0) {
            File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties");
            if (userResolveFile.exists()) {
                resolveFile = userResolveFile.getAbsolutePath();
            }
        }
        // 存在 resolveFile ,则进行文件读取加载。
        if (resolveFile != null && resolveFile.length() > 0) {
            Properties properties = new Properties();
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(new File(resolveFile));
                properties.load(fis);
            } catch (IOException e) {
                throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e);
            } finally {
                try {
                    if (null != fis) fis.close();
                } catch (IOException e) {
                    logger.warn(e.getMessage(), e);
                }
            }
            resolve = properties.getProperty(interfaceName);
        }
    }
    // 设置直连提供者的 url
    if (resolve != null && resolve.length() > 0) {
        url = resolve;
        if (logger.isWarnEnabled()) {
            if (resolveFile != null && resolveFile.length() > 0) {
                logger.warn("Using default dubbo resolve file " + resolveFile + " replace " + interfaceName + "" + resolve + " to p2p invoke remote service.");
            } else {
                logger.warn("Using -D" + interfaceName + "=" + resolve + " to p2p invoke remote service.");
            }
        }
    }
    // 从 ConsumerConfig 对象中,读取 application、module、registries、monitor 配置对象。
    if (consumer != null) {
        if (application == null) {
            application = consumer.getApplication();
        }
        if (module == null) {
            module = consumer.getModule();
        }
        if (registries == null) {
            registries = consumer.getRegistries();
        }
        if (monitor == null) {
            monitor = consumer.getMonitor();
        }
    }
    // 从 ModuleConfig 对象中,读取 registries、monitor 配置对象。
    if (module != null) {
        if (registries == null) {
            registries = module.getRegistries();
        }
        if (monitor == null) {
            monitor = module.getMonitor();
        }
    }
    // 从 ApplicationConfig 对象中,读取 registries、monitor 配置对象。
    if (application != null) {
        if (registries == null) {
            registries = application.getRegistries();
        }
        if (monitor == null) {
            monitor = application.getMonitor();
        }
    }
    // 校验 ApplicationConfig 配置。
    checkApplication();
    // 校验 Stub 和 Mock 相关的配置
    checkStubAndMock(interfaceClass);
    // 将 `side`,`dubbo`,`timestamp`,`pid` 参数,添加到 `map` 集合中。
    Map<String, String> map = new HashMap<String, String>();
    Map<Object, Object> attributes = new HashMap<Object, Object>();
    map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE);
    map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion());
    map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
    if (ConfigUtils.getPid() > 0) {
        map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
    }
    // methods、revision、interface
    if (!isGeneric()) {
        String revision = Version.getVersion(interfaceClass, version);
        if (revision != null && revision.length() > 0) {
            map.put("revision", revision);
        }

        String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames(); // 获得方法数组
        if (methods.length == 0) {
            logger.warn("NO method found in service interface " + interfaceClass.getName());
            map.put("methods", Constants.ANY_VALUE);
        } else {
            map.put("methods", StringUtils.join(new HashSet<String>(Arrays.asList(methods)), ","));
        }
    }
    map.put(Constants.INTERFACE_KEY, interfaceName);
    // 将各种配置对象,添加到 `map` 集合中。
    appendParameters(map, application);
    appendParameters(map, module);
    appendParameters(map, consumer, Constants.DEFAULT_KEY);
    appendParameters(map, this);
    // 获得服务键,作为前缀
    String prefix = StringUtils.getServiceKey(map);
    // 将 MethodConfig 对象数组,添加到 `map` 集合中。
    if (methods != null && !methods.isEmpty()) {
        for (MethodConfig method : methods) {
            // 将 MethodConfig 对象,添加到 `map` 集合中。
            appendParameters(map, method, method.getName());
            // 当 配置了 `MethodConfig.retry = false` 时,强制禁用重试
            String retryKey = method.getName() + ".retry";
            if (map.containsKey(retryKey)) {
                String retryValue = map.remove(retryKey);
                if ("false".equals(retryValue)) {
                    map.put(method.getName() + ".retries", "0");
                }
            }
            // 将带有 @Parameter(attribute = true) 配置对象的属性
            appendAttributes(attributes, method, prefix + "." + method.getName());
            // 检查属性集合中的事件通知方法是否正确。若正确,进行转换。
            checkAndConvertImplicitConfig(method, map, attributes);
        }
    }

    // 以系统环境变量( DUBBO_IP_TO_REGISTRY ) 作为服务注册地址
    String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY);
    if (hostToRegistry == null || hostToRegistry.length() == 0) {
        hostToRegistry = NetUtils.getLocalHost();
    } else if (isInvalidLocalHost(hostToRegistry)) {
        throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
    }
    map.put(Constants.REGISTER_IP_KEY, hostToRegistry);

    // 添加到 StaticContext 进行缓存
    //attributes are stored by system context.
    StaticContext.getSystemContext().putAll(attributes);

    // 创建 Service 代理对象
    ref = createProxy(map);


    ConsumerModel consumerModel = new ConsumerModel(getUniqueServiceName(), this, ref, interfaceClass.getMethods());
    ApplicationModel.initConsumerModel(getUniqueServiceName(), consumerModel);
}

简单来说,就是把配置文件中的配置信息放到ReferenceConfig对象中。
再来看看是怎么创建service代理对象的

private T createProxy(Map<String, String> map) {
    URL tmpUrl = new URL("temp", "localhost", 0, map);
    // 是否本地引用
    final boolean isJvmRefer;
    // injvm 属性为空,不通过该属性判断
    if (isInjvm() == null) {
        // 直连服务提供者
        if (url != null && url.length() > 0) { // if a url is specified, don't do local reference
            isJvmRefer = false;
        // 通过 `tmpUrl` 判断,是否需要本地引用
        } else if (InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl)) {
            // by default, reference local service if there is
            isJvmRefer = true;
        // 默认不是
        } else {
            isJvmRefer = false;
        }
    // 通过 injvm 属性。
    } else {
        isJvmRefer = isInjvm();
    }

    // 本地引用
    if (isJvmRefer) {
        // 创建本地服务引用 URL 对象。
        URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map);
        // 引用服务,返回 Invoker 对象
        invoker = refprotocol.refer(interfaceClass, url);
        if (logger.isInfoEnabled()) {
            logger.info("Using injvm service " + interfaceClass.getName());
        }
    // 正常流程,一般为远程引用
    } else {
        // 定义直连地址,可以是服务提供者的地址,也可以是注册中心的地址
        if (url != null && url.length() > 0) { // user specified URL, could be peer-to-peer address, or register center's address.
            // 拆分地址成数组,使用 ";" 分隔。
            String[] us = Constants.SEMICOLON_SPLIT_PATTERN.split(url);
            // 循环数组,添加到 `url` 中。
            if (us != null && us.length > 0) {
                for (String u : us) {
                    // 创建 URL 对象
                    URL url = URL.valueOf(u);
                    // 设置默认路径
                    if (url.getPath() == null || url.getPath().length() == 0) {
                        url = url.setPath(interfaceName);
                    }
                    // 注册中心的地址,带上服务引用的配置参数
                    if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
                        urls.add(url.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
                    // 服务提供者的地址
                    } else {
                        urls.add(ClusterUtils.mergeUrl(url, map));
                    }
                }
            }
        // 注册中心
        } else { // assemble URL from register center's configuration
            // 加载注册中心 URL 数组
            List<URL> us = loadRegistries(false);
            // 循环数组,添加到 `url` 中。
            if (us != null && !us.isEmpty()) {
                for (URL u : us) {
                    // 加载监控中心 URL
                    URL monitorUrl = loadMonitor(u);
                    // 服务引用配置对象 `map`,带上监控中心的 URL
                    if (monitorUrl != null) {
                        map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
                    }
                    // 注册中心的地址,带上服务引用的配置参数
                    urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map))); // 注册中心,带上服务引用的配置参数
                }
            }
            if (urls.isEmpty()) {
                throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
            }
        }

        // 单 `urls` 时,引用服务,返回 Invoker 对象
        if (urls.size() == 1) {
            // 引用服务
            invoker = refprotocol.refer(interfaceClass, urls.get(0));
        } else {
            // 循环 `urls` ,引用服务,返回 Invoker 对象
            List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
            URL registryURL = null;
            for (URL url : urls) {
                // 引用服务
                invokers.add(refprotocol.refer(interfaceClass, url));
                // 使用最后一个注册中心的 URL
                if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
                    registryURL = url; // use last registry url
                }
            }
            // 有注册中心
            if (registryURL != null) { // registry url is available
                // 对有注册中心的 Cluster 只用 AvailableCluster
                // use AvailableCluster only when register's cluster is available
                URL u = registryURL.addParameter(Constants.CLUSTER_KEY, AvailableCluster.NAME);
                invoker = cluster.join(new StaticDirectory(u, invokers));
            // 无注册中心,全部都是服务直连
            } else { // not a registry url
                invoker = cluster.join(new StaticDirectory(invokers));
            }
        }
    }

    // 启动时检查
    Boolean c = check;
    if (c == null && consumer != null) {
        c = consumer.isCheck();
    }
    if (c == null) {
        c = true; // default true
    }
    if (c && !invoker.isAvailable()) {
        throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion());
    }
    if (logger.isInfoEnabled()) {
        logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
    }

    // 创建 Service 代理对象
    // create service proxy
    return (T) proxyFactory.getProxy(invoker);
}

其实就是先判断是否为远程引用,然后加载注册中心的配置,创建代理对象。通过ProxyFactory创建相应的代理对象,一般是两种,一种是JdkProxyFactory,也就是jdk动态代理,一种是JavassistProxyFactory,也就是基于 Javassist 代理工厂实现类。如果实现了接口,则用jdk动态代理,否则用基于 Javassist 代理工厂实现类。
ReferenceConfig的分析就到这里了。

相关文章

网友评论

      本文标题:Dubbo源码分析(一) ReferenceConfig

      本文链接:https://www.haomeiwen.com/subject/qnioxqtx.html