Dubbo消費端Reference過程(Version2.7.3)


前言

前面Dubbo服務暴露完成了兩個任務:1. 啟動本地服務器。2. 將服務注冊到注冊中心。

服務暴露開始於ServiceBean,那么與之對應的,服務引用開始於ReferenceBean。

入口

入口有兩個,都在ReferenceBean中

// 懶漢式(在 ReferenceBean 對應的服務被注入到其他類中時)
public Object getObject() {
    return get();
}

public void afterPropertiesSet() throws Exception {
    // 省略無關代碼

    // 餓漢式(在 Spring 容器調用 ReferenceBean 的 afterPropertiesSet 方法時)
    if (shouldInit()) {
        getObject();
    }
}

默認不會走afterPropertiesSet方法里的getObject方法,在啟動容器注入依賴的時候,會走ReferenceBean的父類ReferenceConfig#get方法

public synchronized T get() {
    checkAndUpdateSubConfigs();
    if (destroyed) {
        throw new IllegalStateException("The invoker of ReferenceConfig(" + url + ") has already destroyed!");
    }
    if (ref == null) {
        init();
    }
    return ref;
}

調試開始

init方法(從這里開始,Apache的實現和alibaba的實現就完全不一樣了,但是大體思路還是一樣的

// org.apache.dubbo.config.ReferenceConfig#init
private void init() {
    // 已經初始化就返回
    if (initialized) {
        return;
    }
    // 檢查local或者stub 本地存根 是否有配置一個當前接口類型參數的構造函數
    checkStubAndLocal(interfaceClass);
    // 檢查mock配置
    checkMock(interfaceClass);
    Map<String, String> map = new HashMap<String, String>();

    map.put(SIDE_KEY, CONSUMER_SIDE);

    appendRuntimeParameters(map);
    // 不是泛型
    if (!isGeneric()) {
        // 獲取接口版本:1.0
        String revision = Version.getVersion(interfaceClass, version);
        if (revision != null && revision.length() > 0) {
            map.put(REVISION_KEY, revision);
        }
        // 獲取接口方法列表:{"hello"}
        String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();
        if (methods.length == 0) {
            logger.warn("No method found in service interface " + interfaceClass.getName());
            map.put(METHODS_KEY, ANY_VALUE);
        } else {
            map.put(METHODS_KEY, StringUtils.join(new HashSet<String>(Arrays.asList(methods)), COMMA_SEPARATOR));
        }
    }
    map.put(INTERFACE_KEY, interfaceName);
    appendParameters(map, metrics);
    appendParameters(map, application);
    appendParameters(map, module);
    // remove 'default.' prefix for configs from ConsumerConfig
    // appendParameters(map, consumer, Constants.DEFAULT_KEY);
    appendParameters(map, consumer);
    appendParameters(map, this);
    Map<String, Object> attributes = null;
    // 查看有沒有方法配置(config)
    if (CollectionUtils.isNotEmpty(methods)) {
        attributes = new HashMap<String, Object>();
        for (MethodConfig methodConfig : methods) {
            appendParameters(map, methodConfig, methodConfig.getName());
            String retryKey = methodConfig.getName() + ".retry";
            if (map.containsKey(retryKey)) {
                String retryValue = map.remove(retryKey);
                if ("false".equals(retryValue)) {
                    map.put(methodConfig.getName() + ".retries", "0");
                }
            }
            attributes.put(methodConfig.getName(), convertMethodConfig2AyncInfo(methodConfig));
        }
    }
    // 獲取注冊地址
    String hostToRegistry = ConfigUtils.getSystemProperty(DUBBO_IP_TO_REGISTRY);
    if (StringUtils.isEmpty(hostToRegistry)) {
        // 本地地址作為注冊地址
        hostToRegistry = NetUtils.getLocalHost();
    } else if (isInvalidLocalHost(hostToRegistry)) {
        throw new IllegalArgumentException("Specified invalid registry ip from property:" + DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
    }
    map.put(REGISTER_IP_KEY, hostToRegistry);

    // *** 不僅僅是創建代理對象,還要創建Invoker,訂閱服務,啟動客戶端等
    ref = createProxy(map);

    String serviceKey = URL.buildKey(interfaceName, group, version);
    ApplicationModel.initConsumerModel(serviceKey, buildConsumerModel(serviceKey, attributes));
    initialized = true;
}

上面代碼做了什么事情呢

1. 收集配置信息,比如注冊地址、接口名稱、接口方法、版本信息等

2. 執行createProxy方法

private T createProxy(Map<String, String> map) {
    // 是否從當前JVM中引用目標服務的實例(如果指定了injvm,則true;如果指定了url,則認為是遠程調用,false;如果沒有指定,但是目標服務是在相同的JVM中提供的,進行本地調用,true)
    if (shouldJvmRefer(map)) {
        // 生成本地引用 URL,協議為 injvm
        URL url = new URL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map);
        invoker = REF_PROTOCOL.refer(interfaceClass, url);
        if (logger.isInfoEnabled()) {
            logger.info("Using injvm service " + interfaceClass.getName());
        }
    } else {
        // 引用重試初始化會把url添加到url集合里,最終導致內存溢出。
        urls.clear(); 
        // url 不為空,表明用戶可能想進行點對點調用
        if (url != null && url.length() > 0) { 
            // 當需要配置多個 url 時,可用分號進行分割,這里會進行切分
            String[] us = SEMICOLON_SPLIT_PATTERN.split(url);
            if (us != null && us.length > 0) {
                for (String u : us) {
                    URL url = URL.valueOf(u);
                    if (StringUtils.isEmpty(url.getPath())) {
                        // 設置接口全限定名為 url 路徑
                        url = url.setPath(interfaceName);
                    }
                    // 檢測 url 協議是否為 registry,若是,表明用戶想使用指定的注冊中心
                    if (REGISTRY_PROTOCOL.equals(url.getProtocol())) {
                        urls.add(url.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
                    } else {
                        urls.add(ClusterUtils.mergeUrl(url, map));
                    }
                }
            }
        } else { // 從注冊中心的配置中組裝URL
            // 如果協議不是injvm
            if (!LOCAL_PROTOCOL.equalsIgnoreCase(getProtocol())){
                // 檢查注冊中心
                checkRegistry();
                // 加載注冊中心 url
                List<URL> us = loadRegistries(false);
                if (CollectionUtils.isNotEmpty(us)) {
                    for (URL u : us) {
                        URL monitorUrl = loadMonitor(u);
                        if (monitorUrl != null) {
                            map.put(MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
                        }
                        urls.add(u.addParameterAndEncoded(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.");
                }
            }
        }

        // 單個注冊中心或服務提供者
        if (urls.size() == 1) {
            // ***調用 RegistryProtocol 的 refer 構建 Invoker 實例
            invoker = REF_PROTOCOL.refer(interfaceClass, urls.get(0));
        } else {
            // 多個注冊中心或多個服務提供者,或者兩者混合
            List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
            URL registryURL = null;
            for (URL url : urls) {
                invokers.add(REF_PROTOCOL.refer(interfaceClass, url));
                if (REGISTRY_PROTOCOL.equals(url.getProtocol())) {
                    registryURL = url; // use last registry url
                }
            }
            if (registryURL != null) { // registry url is available
                // use RegistryAwareCluster only when register's CLUSTER is available
                URL u = registryURL.addParameter(CLUSTER_KEY, RegistryAwareCluster.NAME);
                // The invoker wrap relation would be: RegistryAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, will execute route) -> Invoker
                invoker = CLUSTER.join(new StaticDirectory(u, invokers));
            } else { // not a registry url, must be direct invoke.
                invoker = CLUSTER.join(new StaticDirectory(invokers));
            }
        }
    }

    if (shouldCheck() && !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());
    }
    /**
     * @since 2.7.0
     * ServiceData Store
     */
    MetadataReportService metadataReportService = null;
    if ((metadataReportService = getMetadataReportService()) != null) {
        URL consumerURL = new URL(CONSUMER_PROTOCOL, map.remove(REGISTER_IP_KEY), 0, map.get(INTERFACE_KEY), map);
        metadataReportService.publishConsumer(consumerURL);
    }
    // 最后創建服務代理對象
    return (T) PROXY_FACTORY.getProxy(invoker);
}

這么多,邏輯如下

1. 收集服務引用的配置信息,比如注冊地址、接口名稱、接口方法、版本信息等
2. 調用createProxy
  - 首先根據配置檢查是否為本地調用,若是,則調用 InjvmProtocol 的 refer 方法生成 InjvmInvoker 實例。
  - 若不是,則讀取直連配置項,或注冊中心 url,並將讀取到的 url 存儲到 urls 中。
  - 若 urls 元素數量為1,則直接通過 Protocol 自適應拓展類構建 Invoker 實例接口。
  - 若 urls 元素數量大於1,即存在多個注冊中心或服務直連 url,此時先根據 url 構建 Invoker。然后再通過 Cluster 合並多個 Invoker。
  [重點]創建 Invoker
    - Invoker 是由 Protocol 實現類 RegistryProtocol 構建而來。

  [重點]最后調用 ProxyFactory 生成代理類。

[重點]創建 Invoker

// org.apache.dubbo.registry.integration.RegistryProtocol#refer
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
    url = URLBuilder.from(url)
            // 給注冊中心URL設置協議,比如:zookeeper
            .setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY))
            .removeParameter(REGISTRY_KEY)
            .build();
    // 獲取注冊中心實例
    Registry registry = registryFactory.getRegistry(url);
    if (RegistryService.class.equals(type)) {
        return proxyFactory.getInvoker((T) registry, type, url);
    }

    // 根據url解析查詢參數,比如:"register.ip" -> "10.204.241.39"  "interface" -> "com.demo.common.HelloService"  鍵值對
    Map<String, String> qs = StringUtils.parseQueryString(url.getParameterAndDecoded(REFER_KEY));
    // 獲取"group"對應的值,未設置為null
    String group = qs.get(GROUP_KEY);
    if (group != null && group.length() > 0) {
        if ((COMMA_SPLIT_PATTERN.split(group)).length > 1 || "*".equals(group)) {
            return doRefer(getMergeableCluster(), registry, type, url);
        }
    }
    // 執行doRefer方法(這種方法風格和Spring一樣,比如Spring里getBean調用doGetBean等)
    return doRefer(cluster, registry, type, url);
}


private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type, URL url) {
    // 注冊中心URL:zookeeper://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-consumer&client=curator&dubbo=2.0.2&interface=org.apache.dubbo.registry.RegistryService&pid=108324&qos.enable=false&release=2.7.3&timeout=5000&timestamp=1584672980993
    // RegistryDirectory對象,基於注冊中心動態感知服務提供者的變化
    RegistryDirectory<T> directory = new RegistryDirectory<T>(type, url);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    // 獲取URL查詢參數鍵值對
    Map<String, String> parameters = new HashMap<String, String>(directory.getUrl().getParameters());
    // 組裝訂閱URL
    // consumer://10.204.241.39/com.demo.common.HelloService?application=dubbo-consumer&dubbo=2.0.2&interface=com.demo.common.HelloService&lazy=false&methods=hello&pid=108324&qos.enable=false&release=2.7.3&revision=1.0&side=consumer&sticky=false&timestamp=1584672980764&version=1.0
    URL subscribeUrl = new URL(CONSUMER_PROTOCOL, parameters.remove(REGISTER_IP_KEY), 0, type.getName(), parameters);
    if (!ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(REGISTER_KEY, true)) {
        directory.setRegisteredConsumerUrl(getRegisteredConsumerUrl(subscribeUrl, url));
        registry.register(directory.getRegisteredConsumerUrl());
    }
    directory.buildRouterChain(subscribeUrl);
    // 執行訂閱邏輯
    directory.subscribe(subscribeUrl.addParameter(CATEGORY_KEY,
            PROVIDERS_CATEGORY + "," + CONFIGURATORS_CATEGORY + "," + ROUTERS_CATEGORY));
    // 創建Invoker並返回
    Invoker invoker = cluster.join(directory);
    ProviderConsumerRegTable.registerConsumer(invoker, url, subscribeUrl, directory);
    return invoker;
}

上面完成了

1. refer方法主要完成:根據注冊中心URL獲取注冊中心實例。

2. doRefer方法:

  - 獲取RegistryDirectory對象,基於注冊中心動態感知服務提供者的變化。

  * 執行subscribe方法。

  * 創建Invoker並返回

執行subscribe方法

// org.apache.dubbo.registry.integration.RegistryDirectory#subscribe
public void subscribe(URL url) {
    // 設置消費者URL
    setConsumerUrl(url);
    CONSUMER_CONFIGURATION_LISTENER.addNotifyListener(this);
    serviceConfigurationListener = new ReferenceConfigurationListener(this, url);
    // 這里的registry是doRefer中設置的,所以這里執行的是注冊中心的subscribe方法
    registry.subscribe(url, this);
}
// org.apache.dubbo.registry.support.FailbackRegistry#subscribe
// Failback:出故障時自動恢復
public void subscribe(URL url, NotifyListener listener) {
    super.subscribe(url, listener);
    removeFailedSubscribed(url, listener);
    try {
        // 向服務端發送一個訂閱請求
        doSubscribe(url, listener);
    } catch (Exception e) {
        Throwable t = e;

        List<URL> urls = getCacheUrls(url);
        if (CollectionUtils.isNotEmpty(urls)) {
            notify(url, listener, urls);
            logger.error("Failed to subscribe " + url + ", Using cached list: " + urls + " from cache file: " + getUrl().getParameter(FILE_KEY, System.getProperty("user.home") + "/dubbo-registry-" + url.getHost() + ".cache") + ", cause: " + t.getMessage(), t);
        } else {
            // If the startup detection is opened, the Exception is thrown directly.
            boolean check = getUrl().getParameter(Constants.CHECK_KEY, true)
                    && url.getParameter(Constants.CHECK_KEY, true);
            boolean skipFailback = t instanceof SkipFailbackWrapperException;
            if (check || skipFailback) {
                if (skipFailback) {
                    t = t.getCause();
                }
                throw new IllegalStateException("Failed to subscribe " + url + ", cause: " + t.getMessage(), t);
            } else {
                logger.error("Failed to subscribe " + url + ", waiting for retry, cause: " + t.getMessage(), t);
            }
        }

        // Record a failed registration request to a failed list, retry regularly
        addFailedSubscribed(url, listener);
    }
}

上面僅僅是做了一些屬性設置,重點在於doSubscribe方法

ZookeeperRegistry

// org.apache.dubbo.registry.zookeeper.ZookeeperRegistry#doSubscribe
public void doSubscribe(final URL url, final NotifyListener listener) {
    try {
        // 處理所有Service層發起的訂閱,例如監控中心的訂閱
        if (ANY_VALUE.equals(url.getServiceInterface())) {
            // 獲得根目錄
            String root = toRootPath();
            // 獲得url對應的監聽器集合
            ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
            // 不存在就創建監聽器集合
            if (listeners == null) {
                zkListeners.putIfAbsent(url, new ConcurrentHashMap<>());
                listeners = zkListeners.get(url);
            }
            // 獲得節點監聽器
            ChildListener zkListener = listeners.get(listener);
            if (zkListener == null) {
                listeners.putIfAbsent(listener, (parentPath, currentChilds) -> {
                    // 遍歷現有的節點,如果現有的服務集合中沒有該節點,則加入該節點,然后訂閱該節點
                    for (String child : currentChilds) {
                        child = URL.decode(child);
                        if (!anyServices.contains(child)) {
                            anyServices.add(child);
                            subscribe(url.setPath(child).addParameters(INTERFACE_KEY, child,
                                    Constants.CHECK_KEY, String.valueOf(false)), listener);
                        }
                    }
                });
                // 重新獲取,為了保證一致性
                zkListener = listeners.get(listener);
            }
            // 創建service節點
            zkClient.create(root, false);
            // 向zookeeper的service節點發起訂閱,獲得Service接口全名數組
            List<String> services = zkClient.addChildListener(root, zkListener);
            if (CollectionUtils.isNotEmpty(services)) {
                // 遍歷Service接口全名數組
                for (String service : services) {
                    service = URL.decode(service);
                    anyServices.add(service);
                    // 發起該service層的訂閱
                    subscribe(url.setPath(service).addParameters(INTERFACE_KEY, service,
                            Constants.CHECK_KEY, String.valueOf(false)), listener);
                }
            }
        } else {
            // 處理指定 Service 層的發起訂閱,例如服務消費者的訂閱
            List<URL> urls = new ArrayList<>();
            for (String path : toCategoriesPath(url)) {
                // path的可能值:
                // 0 = "/dubbo/com.demo.common.HelloService/providers"
                // 1 = "/dubbo/com.demo.common.HelloService/configurators"
                // 2 = "/dubbo/com.demo.common.HelloService/routers"
                // 獲得監聽器集合
                ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
                if (listeners == null) {
                    zkListeners.putIfAbsent(url, new ConcurrentHashMap<>());
                    listeners = zkListeners.get(url);
                }
                // 獲得節點監聽器
                ChildListener zkListener = listeners.get(listener);
                if (zkListener == null) {
                    listeners.putIfAbsent(listener, (parentPath, currentChilds) -> ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds)));
                    zkListener = listeners.get(listener);
                }
                // 創建type節點
                zkClient.create(path, false);
                // 向zookeeper的type節點發起訂閱
                List<String> children = zkClient.addChildListener(path, zkListener);
                if (children != null) {
                    urls.addAll(toUrlsWithEmpty(url, path, children));
                }
            }
            // 通知數據變化
            notify(url, listener, urls);
        }
    } catch (Throwable e) {
        throw new RpcException("Failed to subscribe " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}

這里注意,是訂閱邏輯的主要實現(設計模式為觀察者模式)

FailbackRegistry

// org.apache.dubbo.registry.support.FailbackRegistry#notify
protected void notify(URL url, NotifyListener listener, List<URL> urls) {
    if (url == null) {
        throw new IllegalArgumentException("notify url == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("notify listener == null");
    }
    try {
        // 執行doNotify
        doNotify(url, listener, urls);
    } catch (Exception t) {
        // Record a failed registration request to a failed list, retry regularly
        addFailedNotified(url, listener, urls);
        logger.error("Failed to notify for subscribe " + url + ", waiting for retry, cause: " + t.getMessage(), t);
    }
}

protected void doNotify(URL url, NotifyListener listener, List<URL> urls) {
    // 交給父類
    super.notify(url, listener, urls);
}

AbstractRegistry

// org.apache.dubbo.registry.support.AbstractRegistry#notify(URL, NotifyListener, List<URL>)
protected void notify(URL url, NotifyListener listener, List<URL> urls) {
    if (url == null) {
        throw new IllegalArgumentException("notify url == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("notify listener == null");
    }
    if ((CollectionUtils.isEmpty(urls))
            && !ANY_VALUE.equals(url.getServiceInterface())) {
        logger.warn("Ignore empty notify urls for subscribe url " + url);
        return;
    }
    if (logger.isInfoEnabled()) {
        logger.info("Notify urls for subscribe url " + url + ", urls: " + urls);
    }
    // 按照類別存儲URL,比如:"providers" "routers" "configurators" 三種類型作為key,value是對應的URL列表
    Map<String, List<URL>> result = new HashMap<>();
    for (URL u : urls) {
        if (UrlUtils.isMatch(url, u)) {
            String category = u.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY);
            List<URL> categoryList = result.computeIfAbsent(category, k -> new ArrayList<>());
            categoryList.add(u);
        }
    }
    if (result.size() == 0) {
        return;
    }
    Map<String, List<URL>> categoryNotified = notified.computeIfAbsent(url, u -> new ConcurrentHashMap<>());
    // 遍歷分好類別的URL
    for (Map.Entry<String, List<URL>> entry : result.entrySet()) {
        String category = entry.getKey();
        List<URL> categoryList = entry.getValue();
        categoryNotified.put(category, categoryList);
        // 通知URL的變化(告訴觀察者)
        listener.notify(categoryList);
        // We will update our cache file after each notification.
        // When our Registry has a subscribe failure due to network jitter, we can return at least the existing cache URL.
        saveProperties(url);
    }
}

這里注意,是通知邏輯的主要實現(設計模式為觀察者模式),下面進入notify方法

RegistryDirectory

// org.apache.dubbo.registry.integration.RegistryDirectory#notify
public synchronized void notify(List<URL> urls) {
    // 先把URL分好類
    Map<String, List<URL>> categoryUrls = urls.stream()
            .filter(Objects::nonNull)
            .filter(this::isValidCategory)
            .filter(this::isNotCompatibleFor26x)
            .collect(Collectors.groupingBy(url -> {
                if (UrlUtils.isConfigurator(url)) {
                    return CONFIGURATORS_CATEGORY;
                } else if (UrlUtils.isRoute(url)) {
                    return ROUTERS_CATEGORY;
                } else if (UrlUtils.isProvider(url)) {
                    return PROVIDERS_CATEGORY;
                }
                return "";
            }));
    // 處理類型為configurators的URL
    List<URL> configuratorURLs = categoryUrls.getOrDefault(CONFIGURATORS_CATEGORY, Collections.emptyList());
    this.configurators = Configurator.toConfigurators(configuratorURLs).orElse(this.configurators);
    // 處理類型的routers的URL
    List<URL> routerURLs = categoryUrls.getOrDefault(ROUTERS_CATEGORY, Collections.emptyList());
    toRouters(routerURLs).ifPresent(this::addRouters);

    // 處理類型為providers的URL
    List<URL> providerURLs = categoryUrls.getOrDefault(PROVIDERS_CATEGORY, Collections.emptyList());
    refreshOverrideAndInvoker(providerURLs);
}

這里主要是收到URL的變化而響應的處理過程。我們知道Invoker 是 Dubbo 的核心模型,代表一個可執行體。那么我們檢測到服務地址變化了,那么對應的Invoker肯定要一起改變。

在Debug過程遇到三種格式的URL:

empty://10.204.241.39/com.demo.common.HelloService?application=dubbo-consumer&category=routers&dubbo=2.0.2&interface=com.demo.common.HelloService&lazy=false&methods=hello&pid=10336&qos.enable=false&release=2.7.3&revision=1.0&side=consumer&sticky=false&timestamp=1584948513183&version=1.0

empty://10.204.241.39/com.demo.common.HelloService?application=dubbo-consumer&category=configurators&dubbo=2.0.2&interface=com.demo.common.HelloService&lazy=false&methods=hello&pid=10336&qos.enable=false&release=2.7.3&revision=1.0&side=consumer&sticky=false&timestamp=1584948513183&version=1.0

dubbo://10.204.241.14:20880/com.demo.common.HelloService?actives=5&anyhost=true&application=service-provider&bean.name=providers:dubbo:com.demo.common.HelloService:1.0&default.deprecated=false&default.dynamic=false&default.register=true&deprecated=false&dubbo=2.0.2&dynamic=false&generic=false&interface=com.demo.common.HelloService&methods=hello&pid=49328&register=true&release=2.7.1&retries=3&revision=1.0&side=provider&timeout=3000&timestamp=1563100343715&version=1.0

來看刷新Invoker方法

private void refreshOverrideAndInvoker(List<URL> urls) {
    // mock zookeeper://xxx?mock=return null
    overrideDirectoryUrl();
    refreshInvoker(urls);
}


private void refreshInvoker(List<URL> invokerUrls) {
    Assert.notNull(invokerUrls, "invokerUrls should not be null");

    // 如果地址只有一個並且協議參數值為empty,則禁止訪問
    if (invokerUrls.size() == 1
            && invokerUrls.get(0) != null
            && EMPTY_PROTOCOL.equals(invokerUrls.get(0).getProtocol())) {
        this.forbidden = true; // Forbid to access
        this.invokers = Collections.emptyList();
        routerChain.setInvokers(this.invokers);
        destroyAllInvokers(); // Close all invokers
    } else {
        this.forbidden = false; // Allow to access
        // 臨時存儲舊的UrlInvokerMap
        Map<String, Invoker<T>> oldUrlInvokerMap = this.urlInvokerMap; // local reference
        if (invokerUrls == Collections.<URL>emptyList()) {
            invokerUrls = new ArrayList<>();
        }
        // 如果invokerUrls為空並且緩存不為空
        if (invokerUrls.isEmpty() && this.cachedInvokerUrls != null) {
            invokerUrls.addAll(this.cachedInvokerUrls);
        } else {
            // 否則,添加到緩存
            this.cachedInvokerUrls = new HashSet<>();
            this.cachedInvokerUrls.addAll(invokerUrls);//Cached invoker urls, convenient for comparison
        }
        // 再次判斷是否為空,為空則返回
        if (invokerUrls.isEmpty()) {
            return;
        }
        // 能走到這里的地址格式:
        // dubbo://10.204.241.14:20880/com.demo.common.HelloService?...
        // 生成Map,key為url,value為Invoker
        Map<String, Invoker<T>> newUrlInvokerMap = toInvokers(invokerUrls);// Translate url list to Invoker map

        /**
         * If the calculation is wrong, it is not processed.
         *
         * 1. The protocol configured by the client is inconsistent with the protocol of the server.
         *    eg: consumer protocol = dubbo, provider only has other protocol services(rest).
         * 2. The registration center is not robust and pushes illegal specification data.
         *
         */
        if (CollectionUtils.isEmptyMap(newUrlInvokerMap)) {
            logger.error(new IllegalStateException("urls to invokers error .invokerUrls.size :" + invokerUrls.size() + ", invoker.size :0. urls :" + invokerUrls
                    .toString()));
            return;
        }

        // 獲取Invoker列表
        List<Invoker<T>> newInvokers = Collections.unmodifiableList(new ArrayList<>(newUrlInvokerMap.values()));
        // pre-route and build cache, notice that route cache should build on original Invoker list.
        // toMergeMethodInvokerMap() will wrap some invokers having different groups, those wrapped invokers not should be routed.
        routerChain.setInvokers(newInvokers);
        this.invokers = multiGroup ? toMergeInvokerList(newInvokers) : newInvokers;
        this.urlInvokerMap = newUrlInvokerMap;

        try {
            // 銷毀未使用的Invoker
            destroyUnusedInvokers(oldUrlInvokerMap, newUrlInvokerMap); // Close the unused Invoker
        } catch (Exception e) {
            logger.warn("destroyUnusedInvokers error. ", e);
        }
    }
}

private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
    Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<>();
    if (urls == null || urls.isEmpty()) {
        return newUrlInvokerMap;
    }
    Set<String> keys = new HashSet<>();
    String queryProtocols = this.queryMap.get(PROTOCOL_KEY);
    // 遍歷URL
    for (URL providerUrl : urls) {
        // 如果在引用一端配置了protocol,只會匹配已選擇的protocol
        if (queryProtocols != null && queryProtocols.length() > 0) {
            boolean accept = false;
            String[] acceptProtocols = queryProtocols.split(",");
            for (String acceptProtocol : acceptProtocols) {
                if (providerUrl.getProtocol().equals(acceptProtocol)) {
                    accept = true;
                    break;
                }
            }
            if (!accept) {
                continue;
            }
        }
        // 如果URL的protocol參數為empty,則跳過此次循環
        if (EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
            continue;
        }
        // 校驗,是否有此協議的具體處理類
        if (!ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
            logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() +
                    " in notified url: " + providerUrl + " from registry " + getUrl().getAddress() +
                    " to consumer " + NetUtils.getLocalHost() + ", supported protocol: " +
                    ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
            continue;
        }
        // 合並URL,下面是合並前后的URL:
        // dubbo://10.204.241.14:20880/com.demo.common.HelloService?actives=5&anyhost=true&application=service-provider&bean.name=providers:dubbo:com.demo.common.HelloService:1.0&default.deprecated=false&default.dynamic=false&default.register=true&deprecated=false&dubbo=2.0.2&dynamic=false&generic=false&interface=com.demo.common.HelloService&methods=hello&pid=49328&register=true&release=2.7.1&retries=3&revision=1.0&side=provider&timeout=3000&timestamp=1563100343715&version=1.0
        // dubbo://10.204.241.14:20880/com.demo.common.HelloService?actives=5&anyhost=true&application=dubbo-consumer&bean.name=providers:dubbo:com.demo.common.HelloService:1.0&check=false&default.deprecated=false&default.dynamic=false&default.register=true&deprecated=false&dubbo=2.0.2&dynamic=false&generic=false&interface=com.demo.common.HelloService&lazy=false&methods=hello&pid=110788&qos.enable=false&register=true&register.ip=10.204.241.39&release=2.7.1&remote.application=service-provider&retries=3&revision=1.0&side=consumer&sticky=false&timeout=3000&timestamp=1563100343715&version=1.0
        URL url = mergeUrl(providerUrl);

        // 把URL當做key
        String key = url.toFullString(); // The parameter urls are sorted
        if (keys.contains(key)) { // Repeated url
            continue;
        }
        // 緩存key
        keys.add(key);
        // Cache key is url that does not merge with consumer side parameters, regardless of how the consumer combines parameters, if the server url changes, then refer again
        Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
        // 查看這個key是否有對應的Invoker
        Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
        // 緩存中沒有對應的Invoker,則會創建
        if (invoker == null) {
            try {
                boolean enabled = true;
                if (url.hasParameter(DISABLED_KEY)) {
                    enabled = !url.getParameter(DISABLED_KEY, false);
                } else {
                    enabled = url.getParameter(ENABLED_KEY, true);
                }
                if (enabled) {
                    // 創建Invoker,來看refer方法
                    invoker = new InvokerDelegate<>(protocol.refer(serviceType, url), url, providerUrl);
                }
            } catch (Throwable t) {
                logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t);
            }
            if (invoker != null) { // Put new invoker in cache
                newUrlInvokerMap.put(key, invoker);
            }
        } else {
            // 緩存中有,放進新緩存中
            newUrlInvokerMap.put(key, invoker);
        }
    }
    keys.clear();
    return newUrlInvokerMap;
}

這部分主要是關於刷新Invoker的:

1. 如果地址只有一個並且協議參數值為empty,則禁止訪問。

2. 先查看緩存並保存副本,注意這個副本后面要用到。如果invokerUrls為空而緩存中不為空,則把緩存的值賦給invokerUrls;否則,把invokerUrls添加到緩存。

3. 把invokerUrls轉換成key為url,value為Invoker的新Map集合。

  - 遍歷URL列表

  - 對URL的protocol參數做校驗

  - 合並URL,並把URL的全稱作為key

  - 查看本地緩存有沒有相應的key,沒有則創建(調用AbstractProtocol#refer),並放入緩存

4. 把新生成的Map和之前保存的副本(舊Map)做對比,刪除新Map中沒有的元素(unused)。

AbstractProtocol

// org.apache.dubbo.rpc.protocol.AbstractProtocol#refer
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
    // 調用由子類實現的protocolBindingRefer方法
    return new AsyncToSyncInvoker<>(protocolBindingRefer(type, url));
}

 

DubboProtocol

從RegistryProtocol到RegistryDirectory到DubboProtocol,繞了一大圈。
好不容易找到這里,仔細想來也合情合理:
1. 首先根據配置創建注冊中心,然后 RegistryDirectory 執行服務訂閱的邏輯;
2. 數據發生變化,肯定要隨之更新影響的組件,執行 notify 邏輯,更新服務URl和Invoker;
3. Invoker又是由DubboProtocol創建的,就到了這里

// org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol#protocolBindingRefer
public <T> Invoker<T> protocolBindingRefer(Class<T> serviceType, URL url) throws RpcException {
    optimizeSerialization(url);
    // 1. 創建 rpc invoker
    // 2. 啟動Netty客戶端
    DubboInvoker<T> invoker = new DubboInvoker<T>(serviceType, url, getClients(url), invokers);
    invokers.add(invoker);
    return invoker;
}

來看getClients

private ExchangeClient[] getClients(URL url) {
    // whether to share connection

    boolean useShareConnect = false;

    // 如果沒有配置,這里是0
    int connections = url.getParameter(CONNECTIONS_KEY, 0);
    List<ReferenceCountExchangeClient> shareClients = null;
    // if not configured, connection is shared, otherwise, one connection for one service
    if (connections == 0) {
        // 沒有配置connection,就選擇共享的;否則一個服務一個connection
        useShareConnect = true;

        /**
         * The xml configuration should have a higher priority than properties.
         */
        String shareConnectionsStr = url.getParameter(SHARE_CONNECTIONS_KEY, (String) null);
        connections = Integer.parseInt(StringUtils.isBlank(shareConnectionsStr) ? ConfigUtils.getProperty(SHARE_CONNECTIONS_KEY,
                DEFAULT_SHARE_CONNECTIONS) : shareConnectionsStr);
        shareClients = getSharedClient(url, connections);
    }

    // 根據前面計算得出的connections,創建ExchangeClient數組
    ExchangeClient[] clients = new ExchangeClient[connections];
    // 給數組分配值,如果使用共享的,則從共享客戶端獲取;否則新建
    for (int i = 0; i < clients.length; i++) {
        if (useShareConnect) {
            clients[i] = shareClients.get(i);

        } else {
            clients[i] = initClient(url);
        }
    }

    return clients;
}

private List<ReferenceCountExchangeClient> getSharedClient(URL url, int connectNum) {
    // 地址作為key:10.204.241.14:20880
    String key = url.getAddress();
    // 先從緩存獲取
    List<ReferenceCountExchangeClient> clients = referenceClientMap.get(key);

    // 如果緩存中的客戶端可用,則返回
    if (checkClientCanUse(clients)) {
        batchClientRefIncr(clients);
        return clients;
    }

    locks.putIfAbsent(key, new Object());
    synchronized (locks.get(key)) {
        // 再獲取一遍,排除已創建的情況
        clients = referenceClientMap.get(key);
        // dubbo check
        // 這里我覺得作者應該想寫成 double check(雙重檢查)
        if (checkClientCanUse(clients)) {
            batchClientRefIncr(clients);
            return clients;
        }

        // connectNum 必須大於等於1
        connectNum = Math.max(connectNum, 1);

        // clients為空,先初始化
        if (CollectionUtils.isEmpty(clients)) {
            // *** 創建客戶端
            clients = buildReferenceCountExchangeClientList(url, connectNum);
            // 放入緩存
            referenceClientMap.put(key, clients);

        } else {
            for (int i = 0; i < clients.size(); i++) {
                ReferenceCountExchangeClient referenceCountExchangeClient = clients.get(i);
                // If there is a client in the list that is no longer available, create a new one to replace him.
                if (referenceCountExchangeClient == null || referenceCountExchangeClient.isClosed()) {
                    clients.set(i, buildReferenceCountExchangeClient(url));
                    continue;
                }

                referenceCountExchangeClient.incrementAndGetCount();
            }
        }

        /**
         * I understand that the purpose of the remove operation here is to avoid the expired url key
         * always occupying this memory space.
         */
        locks.remove(key);

        return clients;
    }
}

private List<ReferenceCountExchangeClient> buildReferenceCountExchangeClientList(URL url, int connectNum) {
    List<ReferenceCountExchangeClient> clients = new ArrayList<>();
    // 根據connectNum決定初始化多少個客戶端
    for (int i = 0; i < connectNum; i++) {
        clients.add(buildReferenceCountExchangeClient(url));
    }
    return clients;
}

// 包裝一個ReferenceCountExchangeClient
private ReferenceCountExchangeClient buildReferenceCountExchangeClient(URL url) {
    // 初始化
    ExchangeClient exchangeClient = initClient(url);
    return new ReferenceCountExchangeClient(exchangeClient);
}

private ExchangeClient initClient(URL url) {

    // 獲取客戶端類型,比如:netty
    String str = url.getParameter(CLIENT_KEY, url.getParameter(SERVER_KEY, DEFAULT_REMOTING_CLIENT));

    url = url.addParameter(CODEC_KEY, DubboCodec.NAME);
    // 默認開啟心跳檢測
    url = url.addParameterIfAbsent(HEARTBEAT_KEY, String.valueOf(DEFAULT_HEARTBEAT));

    // 如果沒有str表示類型的Transporter具體實現類,拋出異常
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
        throw new RpcException("Unsupported client type: " + str + "," +
                " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
    }

    ExchangeClient client;
    try {
        // 判斷是否懶加載
        if (url.getParameter(LAZY_CONNECT_KEY, false)) {
            client = new LazyConnectExchangeClient(url, requestHandler);

        } else {
            // 非懶加載,立即初始化
            client = Exchangers.connect(url, requestHandler);
        }

    } catch (RemotingException e) {
        throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
    }

    return client;
}

這么多,做了什么事呢

1. 判斷是否共享連接。沒有配置connections屬性則獲取共享客戶端,否則初始化新的客戶端。【實際上如果共享客戶端沒有初始化,也會執行初始化的邏輯。】

2. 從URl中獲取 IP:端口,作為key。先從緩存中獲取引用客戶端(ReferenceCountExchangeClient),緩存中沒有則新建。

3. 根據connectNum決定初始化多少個客戶端,執行初始化代碼。

4. 先獲取客戶端類型,並校驗是否存在此類型的處理類;然后判斷是否配置為懶加載,否則把初始化任務交給Exchange層的connect方法。

Exchangers

// org.apache.dubbo.remoting.exchange.Exchangers#connect(URL, ExchangeHandler)
public static ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
    if (url == null) {
        throw new IllegalArgumentException("url == null");
    }
    if (handler == null) {
        throw new IllegalArgumentException("handler == null");
    }
    url = url.addParameterIfAbsent(Constants.CODEC_KEY, "exchange");
    // 上面做了參數校驗,然后通過Dubbo SPI獲取具體的處理類,執行對應的connect方法
    return getExchanger(url).connect(url, handler);
}

// org.apache.dubbo.remoting.exchange.support.header.HeaderExchanger#connect
public ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
    // 把處理交給Transport層,並將結果封裝為HeaderExchangeClient
    return new HeaderExchangeClient(Transporters.connect(url, new DecodeHandler(new HeaderExchangeHandler(handler))), true);
}

Dubbo把這一次定義為【exchange 信息交換層】,作用是封裝請求響應模式,同步轉異步。

Transporters

// org.apache.dubbo.remoting.Transporters#connect(URL, ChannelHandler...)
public static Client connect(URL url, ChannelHandler... handlers) throws RemotingException {
    if (url == null) {
        throw new IllegalArgumentException("url == null");
    }
    // 初始化ChannelHandler
    ChannelHandler handler;
    if (handlers == null || handlers.length == 0) {
        handler = new ChannelHandlerAdapter();
    } else if (handlers.length == 1) {
        handler = handlers[0];
    } else {
        handler = new ChannelHandlerDispatcher(handlers);
    }
    // 根據Dubbo SPI獲取具體的處理類,並執行connect方法
    return getTransporter().connect(url, handler);
}

// org.apache.dubbo.remoting.transport.netty4.NettyTransporter#connect
public Client connect(URL url, ChannelHandler listener) throws RemotingException {
    // new一個Netty客戶端
    return new NettyClient(url, listener);
}

Dubbo把這一層定義為【transport 網絡傳輸層】,作用是抽象 mina 和 netty 為統一接口。

NettyClient

// org.apache.dubbo.remoting.transport.netty4.NettyClient
public NettyClient(final URL url, final ChannelHandler handler) throws RemotingException {
    // you can customize name and type of client thread pool by THREAD_NAME_KEY and THREADPOOL_KEY in CommonConstants.
    // the handler will be warped: MultiMessageHandler->HeartbeatHandler->handler
    super(url, wrapChannelHandler(url, handler));
}

// 這里面的 doOpen 和 connect 體現的是模板方法模式,調用過程由抽象類指定,方法由子類實現
public AbstractClient(URL url, ChannelHandler handler) throws RemotingException {
    super(url, handler);

    needReconnect = url.getParameter(Constants.SEND_RECONNECT_KEY, false);

    try {
        // 開啟客戶端
        doOpen();
    } catch (Throwable t) {
        close();
        throw new RemotingException(url.toInetSocketAddress(), null,
                "Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress()
                        + " connect to the server " + getRemoteAddress() + ", cause: " + t.getMessage(), t);
    }
    try {
        // 連接
        connect();
        if (logger.isInfoEnabled()) {
            logger.info("Start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress() + " connect to the server " + getRemoteAddress());
        }
    } catch (RemotingException t) {
        if (url.getParameter(Constants.CHECK_KEY, true)) {
            close();
            throw t;
        } else {
            logger.warn("Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress()
                    + " connect to the server " + getRemoteAddress() + " (check == false, ignore and retry later!), cause: " + t.getMessage(), t);
        }
    } catch (Throwable t) {
        close();
        throw new RemotingException(url.toInetSocketAddress(), null,
                "Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress()
                        + " connect to the server " + getRemoteAddress() + ", cause: " + t.getMessage(), t);
    }

    executor = (ExecutorService) ExtensionLoader.getExtensionLoader(DataStore.class)
            .getDefaultExtension().get(CONSUMER_SIDE, Integer.toString(url.getPort()));
    ExtensionLoader.getExtensionLoader(DataStore.class)
            .getDefaultExtension().remove(CONSUMER_SIDE, Integer.toString(url.getPort()));
}

// org.apache.dubbo.remoting.transport.netty4.NettyClient#doOpen
protected void doOpen() throws Throwable {
    final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this);
    bootstrap = new Bootstrap();
    bootstrap.group(nioEventLoopGroup)
            .option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            //.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout())
            .channel(NioSocketChannel.class);

    if (getConnectTimeout() < 3000) {
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000);
    } else {
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout());
    }

    bootstrap.handler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            int heartbeatInterval = UrlUtils.getHeartbeat(getUrl());
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
                    .addLast("decoder", adapter.getDecoder())
                    .addLast("encoder", adapter.getEncoder())
                    .addLast("client-idle-handler", new IdleStateHandler(heartbeatInterval, 0, 0, MILLISECONDS))
                    .addLast("handler", nettyClientHandler);
            String socksProxyHost = ConfigUtils.getProperty(SOCKS_PROXY_HOST);
            if(socksProxyHost != null) {
                int socksProxyPort = Integer.parseInt(ConfigUtils.getProperty(SOCKS_PROXY_PORT, DEFAULT_SOCKS_PROXY_PORT));
                Socks5ProxyHandler socks5ProxyHandler = new Socks5ProxyHandler(new InetSocketAddress(socksProxyHost, socksProxyPort));
                ch.pipeline().addFirst(socks5ProxyHandler);
            }
        }
    });
}

這部分就是和Netty客戶端最接近的代碼了,自行去熟悉Netty即可。

除了Netty構建客戶端的過程之外,這部分值得學習的就是模板方法的實際應用了。

最后看下整體的調用過程

createProxy:396, ReferenceConfig
refer:392, RegistryProtocol
doRefer:411, RegistryProtocol

subscribe:172, RegistryDirectory
subscribe:295, FailbackRegistry
doSubscribe:183, ZookeeperRegistry
notify:360, FailbackRegistry
doNotify:369, FailbackRegistry
notify:418, AbstractRegistry

notify:233, RegistryDirectory
refreshOverrideAndInvoker:239, RegistryDirectory
refreshInvoker:280, RegistryDirectory
toInvokers:423, RegistryDirectory

refer:91, AbstractProtocol
protocolBindingRefer:407, DubboProtocol
getClients:430, DubboProtocol
getSharedClient:475, DubboProtocol
buildReferenceCountExchangeClientList:550, DubboProtocol
buildReferenceCountExchangeClient:563, DubboProtocol
initClient:595, DubboProtocol

connect:109, Exchangers
connect:39, HeaderExchanger
connect:75, Transporters
connect:40, NettyTransporter
<init>:80, NettyClient
<init>:60, AbstractClient
doOpen:91, NettyClient

這個大體上滿足官方所闡述的《整體設計》層次

另附各層說明

  • config 配置層:對外配置接口,以 ServiceConfigReferenceConfig 為中心,可以直接初始化配置類,也可以通過 spring 解析配置生成配置類
  • proxy 服務代理層:服務接口透明代理,生成服務的客戶端 Stub 和服務器端 Skeleton, 以 ServiceProxy為中心,擴展接口為 ProxyFactory
  • registry 注冊中心層:封裝服務地址的注冊與發現,以服務 URL 為中心,擴展接口為 RegistryFactoryRegistryRegistryService
  • cluster 路由層:封裝多個提供者的路由及負載均衡,並橋接注冊中心,以 Invoker 為中心,擴展接口為 ClusterDirectoryRouterLoadBalance
  • monitor 監控層:RPC 調用次數和調用時間監控,以 Statistics 為中心,擴展接口為 MonitorFactoryMonitorMonitorService
  • protocol 遠程調用層:封裝 RPC 調用,以 InvocationResult 為中心,擴展接口為 ProtocolInvokerExporter
  • exchange 信息交換層:封裝請求響應模式,同步轉異步,以 RequestResponse 為中心,擴展接口為 ExchangerExchangeChannelExchangeClientExchangeServer
  • transport 網絡傳輸層:抽象 mina 和 netty 為統一接口,以 Message 為中心,擴展接口為 ChannelTransporterClientServerCodec
  • serialize 數據序列化層:可復用的一些工具,擴展接口為 SerializationObjectInputObjectOutputThreadPool

可以結合這個層次結構去熟悉源碼

[重點]最后調用 ProxyFactory 生成代理類

進入的是抽象代理工廠

AbstractProxyFactory

// org.apache.dubbo.rpc.proxy.AbstractProxyFactory
public <T> T getProxy(Invoker<T> invoker) throws RpcException {
    // 交給重載方法
    return getProxy(invoker, false);
}


public <T> T getProxy(Invoker<T> invoker, boolean generic) throws RpcException {
    // interface com.demo.common.HelloService -> zookeeper://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?actives=5&anyhost=true&application=dubbo-consumer&bean.name=providers:dubbo:com.demo.common.HelloService:1.0&check=false&default.deprecated=false&default.dynamic=false&default.register=true&deprecated=false&dubbo=2.0.2&dynamic=false&generic=false&interface=com.demo.common.HelloService&lazy=false&methods=hello&pid=11484&qos.enable=false&register=true&register.ip=10.204.241.39&release=2.7.3&remote.application=service-provider&retries=3&revision=1.0&side=consumer&sticky=false&timeout=3000&timestamp=1584952094835&version=1.0
    Class<?>[] interfaces = null;
    // 獲取interfaces參數值
    String config = invoker.getUrl().getParameter(INTERFACES);
    // 如果有interfaces參數值
    if (config != null && config.length() > 0) {
        // 拆分接口列表
        String[] types = COMMA_SPLIT_PATTERN.split(config);
        if (types != null && types.length > 0) {
            interfaces = new Class<?>[types.length + 2];
            interfaces[0] = invoker.getInterface();
            interfaces[1] = EchoService.class;
            for (int i = 0; i < types.length; i++) {
                // 加載接口類
                // 官網文檔是不是有錯誤,是interfaces[i + 1]。如果這樣的話,這里就會覆蓋interfaces[1]的值了。
                interfaces[i + 2] = ReflectUtils.forName(types[i]);
            }
        }
    }
    // 初始化interfaces
    if (interfaces == null) {
        // invoker.getInterface(): com.demo.common.HelloService
        interfaces = new Class<?>[]{invoker.getInterface(), EchoService.class};
    }

    // 為 http 和 hessian 協議提供泛化調用支持
    if (!GenericService.class.isAssignableFrom(invoker.getInterface()) && generic) {
        int len = interfaces.length;
        Class<?>[] temp = interfaces;
        interfaces = new Class<?>[len + 1];
        System.arraycopy(temp, 0, interfaces, 0, len);
        interfaces[len] = com.alibaba.dubbo.rpc.service.GenericService.class;
    }

    // 進入JavassistProxyFactory類
    return getProxy(invoker, interfaces);
}

這一步主要利用Invoker創建interface的Class列表。

JavassistProxyFactory

public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
    // 1. Proxy.getProxy(interfaces),利用Javassist動態生成Proxy子類,其中實現了newInstance的細節。
    // 2. 調用Proxy子類的newInstance方法生成服務接口代理類的一個實例
    return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
}

進入getProxy方法:

吐槽一下:這兩個類的名字真騷啊,注意p的大小寫

看一下生成的類大概是什么樣子的。

利用阿里 Java 應用診斷工具 Arthas 

1. 下載解壓

 2. 輸入  java -jar arthas-boot.jar

 3. 上圖紅框的是我消費者應用,選擇2

 4. 輸入 sc *.proxy0

5. 輸入 jad org.apache.dubbo.common.bytecode.proxy0

完整代碼

ClassLoader:
  +-sun.misc.Launcher$AppClassLoader@18b4aac2
  +-sun.misc.Launcher$ExtClassLoader@4b53f538

Location:
/E:/apache-maven-repo/org/apache/dubbo/dubbo/2.7.3/dubbo-2.7.3.jar

/*
 * Decompiled with CFR.
 *
 * Could not load the following classes:
 *  com.demo.common.HelloService
 */
package org.apache.dubbo.common.bytecode;

import com.alibaba.dubbo.rpc.service.EchoService;
import com.demo.common.HelloService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import org.apache.dubbo.common.bytecode.ClassGenerator;

public class proxy0
implements ClassGenerator.DC,
HelloService,
EchoService {
    public static Method[] methods;
    private InvocationHandler handler;

    public String hello() {
        Object[] arrobject = new Object[]{};
        Object object = this.handler.invoke(this, methods[0], arrobject);
        return (String)object;
    }

    @Override
    public Object $echo(Object object) {
        Object[] arrobject = new Object[]{object};
        Object object2 = this.handler.invoke(this, methods[1], arrobject);
        return object2;
    }

    public proxy0() {
    }

    public proxy0(InvocationHandler invocationHandler) {
        this.handler = invocationHandler;
    }
}

再看一下Proxy子類的,輸入 sc *.Proxy0

輸入 jad org.apache.dubbo.common.bytecode.Proxy0

 

這個類里實現了Proxy抽象類定義的newInstance方法。

所以Proxy.getProxy方法一共生成了兩個類的字節碼,不是一個。

總結

注冊中心細節:

服務提供者啟動時: 向 /dubbo/com.demo.common.HelloService/providers 目錄下寫入自己的 URL 地址
服務消費者啟動時: 訂閱 /dubbo/com.demo.common.HelloService/providers 目錄下的提供者 URL 地址。並向 /dubbo/com.demo.common.HelloService/consumers 目錄下寫入自己的 URL 地址。

 

讀源碼體會:

1. 要先去了解框架的層次,比如自上而下:config 配置層、proxy 服務代理層、registry 注冊中心層、cluster 路由層、monitor 監控層、protocol 遠程調用層、exchange 信息交換層、transport 網絡傳輸層、serialize 數據序列化層。這些層次在源碼中都有體現。

2. 要先大致清楚這個框架或者框架的某個功能解決了什么問題,你才能大致“猜到”某些源代碼的目的。

3. 在代碼細節方面學習的是代碼習慣,在全局范圍方面學習的是設計模式以及解決問題的思路,進而學習別人如何把系統變得健壯易擴展。

========================================2020-05-22更新:服務引用流程圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM