You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by gu...@apache.org on 2021/09/16 06:55:05 UTC

[dubbo] branch 3.0 updated: [3.0] Fix consumer using URL direct connection can not get ServerContext (#8757)

This is an automated email from the ASF dual-hosted git repository.

guohao pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new a9aba45  [3.0] Fix consumer using URL direct connection can not get ServerContext (#8757)
a9aba45 is described below

commit a9aba45a001aea45049e9607f93ff9b0d607c6a6
Author: earthchen <yo...@duobei.com>
AuthorDate: Thu Sep 16 01:54:30 2021 -0500

    [3.0] Fix consumer using URL direct connection can not get ServerContext (#8757)
    
    * Fixed consumer using URL direct connection can not get ServerContext
    
    * fix ut
    
    * remove unused code
---
 .../rpc/cluster/directory/UrlStaticDirectory.java  | 114 +++++++++++++++++++++
 .../org/apache/dubbo/config/ReferenceConfig.java   |  26 +++--
 2 files changed, 130 insertions(+), 10 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/UrlStaticDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/UrlStaticDirectory.java
new file mode 100644
index 0000000..37bf1eb
--- /dev/null
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/UrlStaticDirectory.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.rpc.cluster.directory;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.cluster.RouterChain;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * copy StaticDirectory for rpc protocol url direct
+ */
+public class UrlStaticDirectory<T> extends AbstractDirectory<T> {
+    private static final Logger logger = LoggerFactory.getLogger(UrlStaticDirectory.class);
+
+    private final List<Invoker<T>> invokers;
+
+    public UrlStaticDirectory(List<Invoker<T>> invokers) {
+        this(null, invokers, null);
+    }
+
+    public UrlStaticDirectory(List<Invoker<T>> invokers, RouterChain<T> routerChain) {
+        this(null, invokers, routerChain);
+    }
+
+    public UrlStaticDirectory(URL url, List<Invoker<T>> invokers) {
+        this(url, invokers, null);
+    }
+
+    public UrlStaticDirectory(URL url, List<Invoker<T>> invokers, RouterChain<T> routerChain) {
+        super(url == null && CollectionUtils.isNotEmpty(invokers) ? invokers.get(0).getUrl() : url, routerChain, false);
+        if (CollectionUtils.isEmpty(invokers)) {
+            throw new IllegalArgumentException("invokers == null");
+        }
+        this.invokers = invokers;
+    }
+
+    @Override
+    public Class<T> getInterface() {
+        return invokers.get(0).getInterface();
+    }
+
+    @Override
+    public List<Invoker<T>> getAllInvokers() {
+        return invokers;
+    }
+
+    @Override
+    public boolean isAvailable() {
+        if (isDestroyed()) {
+            return false;
+        }
+        for (Invoker<T> invoker : invokers) {
+            if (invoker.isAvailable()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public void destroy() {
+        if (isDestroyed()) {
+            return;
+        }
+        super.destroy();
+        for (Invoker<T> invoker : invokers) {
+            invoker.destroy();
+        }
+        invokers.clear();
+    }
+
+    public void buildRouterChain() {
+        RouterChain<T> routerChain = RouterChain.buildChain(getUrl());
+        routerChain.setInvokers(invokers);
+        routerChain.loop(true);
+        this.setRouterChain(routerChain);
+    }
+
+    @Override
+    protected List<Invoker<T>> doList(Invocation invocation) throws RpcException {
+        List<Invoker<T>> finalInvokers = invokers;
+        if (routerChain != null) {
+            try {
+                finalInvokers = routerChain.route(getConsumerUrl(), invocation);
+            } catch (Throwable t) {
+                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
+            }
+        }
+        return finalInvokers == null ? Collections.emptyList() : finalInvokers;
+    }
+
+}
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
index e13400e..795fa20 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
@@ -37,6 +37,7 @@ import org.apache.dubbo.rpc.Protocol;
 import org.apache.dubbo.rpc.ProxyFactory;
 import org.apache.dubbo.rpc.cluster.Cluster;
 import org.apache.dubbo.rpc.cluster.directory.StaticDirectory;
+import org.apache.dubbo.rpc.cluster.directory.UrlStaticDirectory;
 import org.apache.dubbo.rpc.cluster.support.ClusterUtils;
 import org.apache.dubbo.rpc.cluster.support.registry.ZoneAwareCluster;
 import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -464,7 +465,13 @@ public class ReferenceConfig<T> extends ReferenceConfigBase<T> {
     @SuppressWarnings({"unchecked", "rawtypes"})
     private void createInvokerForRemote() {
         if (urls.size() == 1) {
-            invoker = protocolSPI.refer(interfaceClass, urls.get(0));
+            URL curUrl = urls.get(0);
+            invoker = protocolSPI.refer(interfaceClass,curUrl);
+            if (!UrlUtils.isRegistry(curUrl)){
+                List<Invoker<?>> invokers = new ArrayList<>();
+                invokers.add(invoker);
+                invoker = Cluster.getCluster(scopeModel, Cluster.DEFAULT).join(new UrlStaticDirectory(curUrl,invokers));
+            }
         } else {
             List<Invoker<?>> invokers = new ArrayList<>();
             URL registryUrl = null;
@@ -483,18 +490,17 @@ public class ReferenceConfig<T> extends ReferenceConfigBase<T> {
                 // registry url is available
                 // for multi-subscription scenario, use 'zone-aware' policy by default
                 String cluster = registryUrl.getParameter(CLUSTER_KEY, ZoneAwareCluster.NAME);
-                // The invoker wrap sequence would be: ZoneAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, routing happens here) -> Invoker
+                // The invoker wrap sequence would be: ZoneAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker
+                // (RegistryDirectory, routing happens here) -> Invoker
                 invoker = Cluster.getCluster(registryUrl.getScopeModel(), cluster, false).join(new StaticDirectory(registryUrl, invokers));
             } else {
                 // not a registry url, must be direct invoke.
-                String cluster = CollectionUtils.isNotEmpty(invokers) ?
-                    (invokers.get(0).getUrl() != null ? invokers.get(0).getUrl().getParameter(CLUSTER_KEY, ZoneAwareCluster.NAME) :
-                        Cluster.DEFAULT)
-                    : Cluster.DEFAULT;
-                ScopeModel scopeModel = CollectionUtils.isNotEmpty(invokers) ?
-                    (invokers.get(0).getUrl() != null ? invokers.get(0).getUrl().getScopeModel() : null)
-                    : null;
-                invoker = Cluster.getCluster(scopeModel, cluster).join(new StaticDirectory(invokers));
+                if (CollectionUtils.isEmpty(invokers)) {
+                    throw new IllegalArgumentException("invokers == null");
+                }
+                URL curUrl = invokers.get(0).getUrl();
+                String cluster = curUrl.getParameter(CLUSTER_KEY, Cluster.DEFAULT);
+                invoker = Cluster.getCluster(scopeModel, cluster).join(new UrlStaticDirectory(curUrl, invokers));
             }
         }
     }