From c76e97dc616509fe6f6b2bef3ab922642af7cd06 Mon Sep 17 00:00:00 2001 From: Phoenix Backendon <100568933+PhoenixBackendon@users.noreply.github.com> Date: Fri, 19 May 2023 15:31:54 +0800 Subject: [PATCH] Fix issue #12334.provide broadcast rules to adapt the calling mode. (#12347) * Fix issue #12334.provide broadcast rules to adapt the calling mode. * add isDebugEnabled --- .../support/wrapper/ScopeClusterInvoker.java | 24 +++++++++++++++++++ .../wrapper/ScopeClusterInvokerTest.java | 18 ++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvoker.java index 445aaf236b..7ac76c2ac3 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvoker.java @@ -18,6 +18,8 @@ package org.apache.dubbo.rpc.cluster.support.wrapper; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.url.component.ServiceConfigURL; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; @@ -38,12 +40,14 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; import static org.apache.dubbo.rpc.Constants.SCOPE_KEY; import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL; import static org.apache.dubbo.rpc.cluster.Constants.PEER_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.BROADCAST_CLUSTER; /** * ScopeClusterInvoker is a cluster invoker which handles the invocation logic of a single service in a specific scope. @@ -53,6 +57,10 @@ import static org.apache.dubbo.rpc.cluster.Constants.PEER_KEY; * @param the type of service interface */ public class ScopeClusterInvoker implements ClusterInvoker, ExporterChangeListener { + + private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ScopeClusterInvoker.class); + + private final Object createLock = new Object(); private Protocol protocolSPI; private final Directory directory; @@ -119,14 +127,30 @@ public class ScopeClusterInvoker implements ClusterInvoker, ExporterChange */ @Override public Result invoke(Invocation invocation) throws RpcException { + // When broadcasting, it should be called remotely. + if (BROADCAST_CLUSTER.equalsIgnoreCase(getUrl().getParameter(CLUSTER_KEY))) { + if (logger.isDebugEnabled()) { + logger.debug("Performing broadcast call for method: " + invocation.getMethodName() + " of service: " + getUrl().getServiceKey()); + } + return invoker.invoke(invocation); + } if (peerFlag) { + if (logger.isDebugEnabled()) { + logger.debug("Performing point-to-point call for method: " + invocation.getMethodName() + " of service: " + getUrl().getServiceKey()); + } // If it's a point-to-point direct connection, invoke the original Invoker return invoker.invoke(invocation); } if (isInjvmExported()) { + if (logger.isDebugEnabled()) { + logger.debug("Performing local JVM call for method: " + invocation.getMethodName() + " of service: " + getUrl().getServiceKey()); + } // If it's exported to the local JVM, invoke the corresponding Invoker return injvmInvoker.invoke(invocation); } + if (logger.isDebugEnabled()) { + logger.debug("Performing remote call for method: " + invocation.getMethodName() + " of service: " + getUrl().getServiceKey()); + } // Otherwise, delegate the invocation to the original Invoker return invoker.invoke(invocation); } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvokerTest.java index 44132df55c..2d54ba72e8 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvokerTest.java @@ -320,6 +320,24 @@ class ScopeClusterInvokerTest { Assertions.assertEquals("doSomething8", ret3.getValue()); } + @Test + void testBroadcast() { + URL url = URL.valueOf("remote://1.2.3.4/" + DemoService.class.getName()); + url = url.addParameter(REFER_KEY, + URL.encode(PATH_KEY + "=" + DemoService.class.getName())); + url = url.addParameter("cluster","broadcast"); + url = url.setScopeModel(ApplicationModel.defaultModel().getDefaultModule()); + Invoker cluster = getClusterInvoker(url); + + invokers.add(cluster); + + RpcInvocation invocation = new RpcInvocation(); + invocation.setMethodName("doSomething8"); + invocation.setParameterTypes(new Class[]{}); + Result ret = cluster.invoke(invocation); + Assertions.assertEquals("doSomething8", ret.getValue()); + } + private Invoker getClusterInvoker(URL url) { final URL durl = url.addParameter("proxy", "jdk"); invokers.clear();