From 15f4a7d7cbbd4ba1c580f4c3673b2854cef52f0b Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Sun, 9 Oct 2022 09:08:10 +0800 Subject: [PATCH 1/5] Revert "Local references support mergeable (#9645)" (#10707) This reverts commit 72326f42ba2add37b5f22541ad40fc3644dbe156. --- .../apache/dubbo/config/ReferenceConfig.java | 6 ++- .../dubbo/config/ReferenceConfigTest.java | 7 +-- .../registry/multiple/MultipleRegistry.java | 2 +- .../rpc/protocol/injvm/InjvmInvoker.java | 9 ++-- .../rpc/protocol/injvm/InjvmProtocol.java | 52 +------------------ .../rpc/protocol/injvm/Hello1ServiceImpl.java | 31 ----------- .../rpc/protocol/injvm/Hello2ServiceImpl.java | 33 ------------ .../rpc/protocol/injvm/HelloService.java | 24 --------- .../rpc/protocol/injvm/InjvmProtocolTest.java | 40 ++------------ 9 files changed, 23 insertions(+), 181 deletions(-) delete mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java delete mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java delete mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java 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 931adce942..6d5e3163a8 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 @@ -448,7 +448,11 @@ public class ReferenceConfig extends ReferenceConfigBase { URL url = new ServiceConfigURL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName(), referenceParameters); url = url.setScopeModel(getScopeModel()); url = url.setServiceModel(consumerModel); - invoker = protocolSPI.refer(interfaceClass, url); + Invoker withFilter = protocolSPI.refer(interfaceClass, url); + // Local Invoke ( Support Cluster Filter / Filter ) + List> invokers = new ArrayList<>(); + invokers.add(withFilter); + invoker = Cluster.getCluster(url.getScopeModel(), Cluster.DEFAULT).join(new StaticDirectory(url, invokers), true); if (logger.isInfoEnabled()) { logger.info("Using in jvm service " + interfaceClass.getName()); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index 41943fc587..7d34883629 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -477,9 +477,10 @@ public class ReferenceConfigTest { .initialize(); referenceConfig.init(); - Invoker withFilter = ((ListenerInvokerWrapper) referenceConfig.getInvoker()).getInvoker(); - withFilter = ((MockClusterInvoker) withFilter).getDirectory().getAllInvokers().get(0); - Assertions.assertTrue(withFilter instanceof InjvmInvoker); + Assertions.assertTrue(referenceConfig.getInvoker() instanceof MockClusterInvoker); + Invoker withFilter = ((MockClusterInvoker) referenceConfig.getInvoker()).getDirectory().getAllInvokers().get(0); + Assertions.assertTrue(withFilter instanceof ListenerInvokerWrapper); + Assertions.assertTrue(((ListenerInvokerWrapper) withFilter).getInvoker() instanceof InjvmInvoker); URL url = withFilter.getUrl(); Assertions.assertEquals("application1", url.getParameter("application")); Assertions.assertEquals("value1", url.getParameter("key1")); diff --git a/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java b/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java index be9c9c92ff..0b1e5d923e 100644 --- a/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java +++ b/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java @@ -253,7 +253,7 @@ public class MultipleRegistry extends AbstractRegistry { protected static class MultipleNotifyListenerWrapper implements NotifyListener { - Map registryMap = new ConcurrentHashMap<>(4); + Map registryMap = new ConcurrentHashMap(4); NotifyListener sourceNotifyListener; public MultipleNotifyListenerWrapper(NotifyListener sourceNotifyListener) { diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index 18c08e8236..719e036d84 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -42,6 +42,7 @@ import org.apache.dubbo.rpc.support.RpcUtils; import java.lang.reflect.Type; import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -62,7 +63,7 @@ public class InjvmInvoker extends AbstractInvoker { private final String key; - private final Exporter exporter; + private final Map> exporterMap; private final ExecutorRepository executorRepository; @@ -70,10 +71,10 @@ public class InjvmInvoker extends AbstractInvoker { private final boolean shouldIgnoreSameModule; - InjvmInvoker(Class type, URL url, String key, Exporter exporter) { + InjvmInvoker(Class type, URL url, String key, Map> exporterMap) { super(type, url); this.key = key; - this.exporter = exporter; + this.exporterMap = exporterMap; this.executorRepository = url.getOrDefaultApplicationModel().getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel().getExtensionLoader(ParamDeepCopyUtil.class) .getExtension(url.getParameter(CommonConstants.INJVM_COPY_UTIL_KEY, DefaultParamDeepCopyUtil.NAME)); @@ -82,6 +83,7 @@ public class InjvmInvoker extends AbstractInvoker { @Override public boolean isAvailable() { + InjvmExporter exporter = (InjvmExporter) exporterMap.get(key); if (exporter == null) { return false; } else { @@ -91,6 +93,7 @@ public class InjvmInvoker extends AbstractInvoker { @Override public Result doInvoke(Invocation invocation) throws Throwable { + Exporter exporter = InjvmProtocol.getExporter(exporterMap, getUrl()); if (exporter == null) { throw new RpcException("Service [" + key + "] not found."); } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java index fdc9e101f6..75a51f37be 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java @@ -18,29 +18,19 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.cluster.Cluster; -import org.apache.dubbo.rpc.cluster.ClusterInvoker; -import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; -import org.apache.dubbo.rpc.cluster.support.MergeableCluster; import org.apache.dubbo.rpc.model.ScopeModel; import org.apache.dubbo.rpc.protocol.AbstractProtocol; import org.apache.dubbo.rpc.support.ProtocolUtils; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.BROADCAST_CLUSTER; import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; 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; @@ -79,7 +69,7 @@ public class InjvmProtocol extends AbstractProtocol { if (result == null) { return null; } else if (ProtocolUtils.isGeneric( - result.getInvoker().getUrl().getParameter(GENERIC_KEY))) { + result.getInvoker().getUrl().getParameter(GENERIC_KEY))) { return null; } else { return result; @@ -98,15 +88,7 @@ public class InjvmProtocol extends AbstractProtocol { @Override public Invoker protocolBindingRefer(Class serviceType, URL url) throws RpcException { - // group="a,b" or group="*" - String group = url.getParameter(GROUP_KEY); - if (StringUtils.isNotEmpty(group)) { - if ((COMMA_SPLIT_PATTERN.split(group)).length > 1 || "*".equals(group)) { - return doCreateInvoker(url, Cluster.getCluster(url.getScopeModel(), MergeableCluster.NAME), serviceType); - } - } - Cluster cluster = Cluster.getCluster(url.getScopeModel(), url.getParameter(CLUSTER_KEY)); - return doCreateInvoker(url, cluster, serviceType); + return new InjvmInvoker(serviceType, url, url.getServiceKey(), exporterMap); } public boolean isInjvmRefer(URL url) { @@ -134,34 +116,4 @@ public class InjvmProtocol extends AbstractProtocol { return false; } } - - @SuppressWarnings({"unchecked", "rawtypes"}) - protected ClusterInvoker doCreateInvoker(URL url, Cluster cluster, Class type) { - StaticDirectory directory = new StaticDirectory(url, getInvokers(exporterMap, url, type)); - return (ClusterInvoker) cluster.join(directory, true); - } - - private List> getInvokers(Map> map, URL url, Class type) { - List> result = new ArrayList<>(); - - if (!url.getServiceKey().contains("*")) { - Exporter exporter = map.get(url.getServiceKey()); - InjvmInvoker invoker = new InjvmInvoker<>(type, url, url.getServiceKey(), exporter); - result.add(invoker); - } else { - if (CollectionUtils.isNotEmptyMap(map)) { - for (Exporter exporter : map.values()) { - if (UrlUtils.isServiceKeyMatch(url, exporter.getInvoker().getUrl())) { - URL providerUrl = exporter.getInvoker().getUrl(); - URL consumerUrl = url.addParameter(GROUP_KEY, providerUrl.getGroup()) - .addParameter(VERSION_KEY, providerUrl.getVersion()); - InjvmInvoker invoker = new InjvmInvoker<>(type, consumerUrl, consumerUrl.getServiceKey(), exporter); - result.add(invoker); - } - } - } - } - - return result; - } } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java deleted file mode 100644 index ee5b7737e2..0000000000 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.protocol.injvm; - - -import java.util.ArrayList; -import java.util.List; - -public class Hello1ServiceImpl implements HelloService { - - @Override - public List hellos() { - List res = new ArrayList<>(); - res.add("consumer-hello-1"); - return res; - } -} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java deleted file mode 100644 index 8937f5096c..0000000000 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.protocol.injvm; - - -import java.util.ArrayList; -import java.util.List; - - -public class Hello2ServiceImpl implements HelloService { - - @Override - public List hellos() { - List res = new ArrayList<>(); - res.add("consumer-hello-2"); - return res; - } -} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java deleted file mode 100644 index cd6bc45c62..0000000000 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.protocol.injvm; - -import java.util.List; - -public interface HelloService { - List hellos(); -} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java index d7dd373591..4e57d054b4 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java @@ -29,16 +29,18 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import static org.apache.dubbo.common.constants.CommonConstants.*; +import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.apache.dubbo.rpc.Constants.ASYNC_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_LOCAL; import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; -import static org.apache.dubbo.rpc.Constants.MERGER_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; @@ -74,7 +76,7 @@ public class InjvmProtocolTest { assertEquals(service.getSize(new String[]{"", "", ""}), 3); service.invoke("injvm://127.0.0.1/TestService", "invoke"); - InjvmInvoker injvmInvoker = new InjvmInvoker<>(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, null); + InjvmInvoker injvmInvoker = new InjvmInvoker<>(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, new HashMap<>()); assertFalse(injvmInvoker.isAvailable()); } @@ -135,36 +137,4 @@ public class InjvmProtocolTest { assertNull(service.getAsyncResult()); } - @Test - public void testLocalProtocolForMergeResult() throws Exception { - HelloService helloService1 = new Hello1ServiceImpl(); - URL url = URL.valueOf("injvm://127.0.0.1/HelloService") - .addParameter(INTERFACE_KEY, HelloService.class.getName()) - .addParameter(APPLICATION_KEY, "consumer") - .addParameter(GROUP_KEY, "g1"); - Invoker invoker1 = proxy.getInvoker(helloService1, HelloService.class, url); - assertTrue(invoker1.isAvailable()); - Exporter exporter1 = protocol.export(invoker1); - exporters.add(exporter1); - - URL url2 = URL.valueOf("injvm://127.0.0.1/HelloService") - .addParameter(INTERFACE_KEY, HelloService.class.getName()) - .addParameter(APPLICATION_KEY, "consumer") - .addParameter(GROUP_KEY, "g2"); - HelloService helloService2 = new Hello2ServiceImpl(); - Invoker invoker2 = proxy.getInvoker(helloService2, HelloService.class, url2); - assertTrue(invoker2.isAvailable()); - Exporter exporter2 = protocol.export(invoker2); - exporters.add(exporter2); - - - URL referUrl = URL.valueOf("injvm://127.0.0.1/HelloService") - .addParameter(INTERFACE_KEY, HelloService.class.getName()) - .addParameter(APPLICATION_KEY, "consumer") - .addParameter(GROUP_KEY, "*") - .addParameter(MERGER_KEY, "list"); - List list = proxy.getProxy(protocol.refer(HelloService.class, referUrl)).hellos(); - assertEquals(2, list.size()); - } - } From 9bbca3aef2b09f717037cead942b728e22975cb5 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Wed, 16 Nov 2022 15:32:17 +0800 Subject: [PATCH 2/5] Remove guava usage in nacos registry (#10945) --- .../registry/nacos/util/NacosInstanceManageUtil.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java index 125f22a776..41f3d26e0b 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java @@ -19,12 +19,12 @@ package org.apache.dubbo.registry.nacos.util; import org.apache.dubbo.common.utils.CollectionUtils; import com.alibaba.nacos.api.naming.pojo.Instance; -import com.alibaba.nacos.shaded.com.google.common.collect.Lists; -import com.alibaba.nacos.shaded.com.google.common.collect.Maps; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * Instance manage util for multiple serviceNames @@ -37,12 +37,12 @@ public class NacosInstanceManageUtil { /** * serviceName -> refreshed instance list */ - private static final Map> SERVICE_INSTANCE_LIST_MAP = Maps.newConcurrentMap(); + private static final Map> SERVICE_INSTANCE_LIST_MAP = new ConcurrentHashMap<>(); /** * serviceName -> corresponding serviceName list */ - private static final Map> CORRESPONDING_SERVICE_NAMES_MAP = Maps.newConcurrentMap(); + private static final Map> CORRESPONDING_SERVICE_NAMES_MAP = new ConcurrentHashMap<>(); public static void setCorrespondingServiceNames(String serviceName, Set serviceNames) { CORRESPONDING_SERVICE_NAMES_MAP.put(serviceName, serviceNames); @@ -58,9 +58,9 @@ public class NacosInstanceManageUtil { public static List getAllCorrespondingServiceInstanceList(String serviceName) { if (!CORRESPONDING_SERVICE_NAMES_MAP.containsKey(serviceName)) { - return Lists.newArrayList(); + return new ArrayList<>(); } - List allInstances = Lists.newArrayList(); + List allInstances = new ArrayList<>(); for (String correspondingServiceName : CORRESPONDING_SERVICE_NAMES_MAP.get(serviceName)) { if (SERVICE_INSTANCE_LIST_MAP.containsKey(correspondingServiceName) && CollectionUtils.isNotEmpty(SERVICE_INSTANCE_LIST_MAP.get(correspondingServiceName))) { From ac62dfd6394099ed2b22e16af14b74543844cd0e Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Mon, 21 Nov 2022 10:35:11 +0800 Subject: [PATCH 3/5] Remove NacosInstanceManageUtil (#10949) --- .../nacos/NacosAggregateListener.java | 6 +- .../dubbo/registry/nacos/NacosRegistry.java | 20 ---- .../nacos/util/NacosInstanceManageUtil.java | 73 ------------- .../util/NacosInstanceManageUtilTest.java | 100 ------------------ 4 files changed, 5 insertions(+), 194 deletions(-) delete mode 100644 dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java delete mode 100644 dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtilTest.java diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java index 5593acd915..4eafb9b6f5 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java @@ -39,7 +39,11 @@ public class NacosAggregateListener { public List saveAndAggregateAllInstances(String serviceName, List instances) { serviceNames.add(serviceName); - serviceInstances.put(serviceName, instances); + if (instances == null) { + serviceInstances.remove(serviceName); + } else { + serviceInstances.put(serviceName, instances); + } return serviceInstances.values().stream().flatMap(List::stream).collect(Collectors.toList()); } diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java index 2b5cb33804..2bf71008fb 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java @@ -28,7 +28,6 @@ import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryNotifier; -import org.apache.dubbo.registry.nacos.util.NacosInstanceManageUtil; import org.apache.dubbo.registry.support.FailbackRegistry; import org.apache.dubbo.rpc.RpcException; @@ -209,13 +208,6 @@ public class NacosRegistry extends FailbackRegistry { Set serviceNames = getServiceNames(url, nacosAggregateListener); - //Set corresponding serviceNames for easy search later - if (isServiceNamesWithCompatibleMode(url)) { - for (String serviceName : serviceNames) { - NacosInstanceManageUtil.setCorrespondingServiceNames(serviceName, serviceNames); - } - } - doSubscribe(url, nacosAggregateListener, serviceNames); } @@ -235,7 +227,6 @@ public class NacosRegistry extends FailbackRegistry { for (String serviceName : serviceNames) { List instances = namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP)); - NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances); notifySubscriber(url, serviceName, listener, instances); } for (String serviceName : serviceNames) { @@ -288,9 +279,6 @@ public class NacosRegistry extends FailbackRegistry { Set serviceNames = nacosAggregateListener.getServiceNames(); try { doUnsubscribe(url, nacosAggregateListener, serviceNames); - for (String serviceName : serviceNames) { - NacosInstanceManageUtil.removeCorrespondingServiceNames(serviceName); - } } catch (NacosException e) { logger.error(REGISTRY_NACOS_EXCEPTION, "", "", "Failed to unsubscribe " + url + " to nacos " + getUrl() + ", cause: " + e.getMessage(), e); } @@ -715,14 +703,6 @@ public class NacosRegistry extends FailbackRegistry { @Override protected void doNotify(Object rawAddresses) { List instances = (List) rawAddresses; - if (isServiceNamesWithCompatibleMode(consumerUrl)) { - /** - * Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned - * in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899 - */ - NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances); - instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName); - } NacosRegistry.this.notifySubscriber(consumerUrl, serviceName, listener, instances); } }; diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java deleted file mode 100644 index 41f3d26e0b..0000000000 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtil.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.registry.nacos.util; - -import org.apache.dubbo.common.utils.CollectionUtils; - -import com.alibaba.nacos.api.naming.pojo.Instance; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Instance manage util for multiple serviceNames - * To resolve bug with https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899 - * - * @since 2.7.6 - */ -public class NacosInstanceManageUtil { - - /** - * serviceName -> refreshed instance list - */ - private static final Map> SERVICE_INSTANCE_LIST_MAP = new ConcurrentHashMap<>(); - - /** - * serviceName -> corresponding serviceName list - */ - private static final Map> CORRESPONDING_SERVICE_NAMES_MAP = new ConcurrentHashMap<>(); - - public static void setCorrespondingServiceNames(String serviceName, Set serviceNames) { - CORRESPONDING_SERVICE_NAMES_MAP.put(serviceName, serviceNames); - } - - public static void initOrRefreshServiceInstanceList(String serviceName, List instanceList) { - SERVICE_INSTANCE_LIST_MAP.put(serviceName, instanceList); - } - - public static Set removeCorrespondingServiceNames(String serviceName) { - return CORRESPONDING_SERVICE_NAMES_MAP.remove(serviceName); - } - - public static List getAllCorrespondingServiceInstanceList(String serviceName) { - if (!CORRESPONDING_SERVICE_NAMES_MAP.containsKey(serviceName)) { - return new ArrayList<>(); - } - List allInstances = new ArrayList<>(); - for (String correspondingServiceName : CORRESPONDING_SERVICE_NAMES_MAP.get(serviceName)) { - if (SERVICE_INSTANCE_LIST_MAP.containsKey(correspondingServiceName) - && CollectionUtils.isNotEmpty(SERVICE_INSTANCE_LIST_MAP.get(correspondingServiceName))) { - allInstances.addAll(SERVICE_INSTANCE_LIST_MAP.get(correspondingServiceName)); - } - } - return allInstances; - } - -} diff --git a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtilTest.java b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtilTest.java deleted file mode 100644 index bb5ae16feb..0000000000 --- a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/util/NacosInstanceManageUtilTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.registry.nacos.util; - -import com.alibaba.nacos.api.naming.pojo.Instance; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Test for NacosInstanceManageUtil - */ -public class NacosInstanceManageUtilTest { - - @Test - public void testSetCorrespondingServiceNames() { - String serviceName = "serviceName"; - Set serviceNames = new HashSet() {{ - add("serviceName1"); - add("serviceName2"); - add("serviceName3"); - }}; - NacosInstanceManageUtil.setCorrespondingServiceNames(serviceName, serviceNames); - List allCorrespondingServiceInstanceList = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName); - Assertions.assertEquals(0, allCorrespondingServiceInstanceList.size()); - } - - @Test - public void testInitOrRefreshServiceInstanceList() { - String serviceName = "serviceName"; - Set serviceNames = new HashSet() {{ - add("serviceName1"); - add("serviceName2"); - add("serviceName3"); - }}; - NacosInstanceManageUtil.setCorrespondingServiceNames(serviceName, serviceNames); - - Instance instance1 = new Instance(); - instance1.setInstanceId("1"); - Instance instance2 = new Instance(); - instance2.setInstanceId("2"); - Instance instance3 = new Instance(); - instance3.setInstanceId("3"); - - List instanceList = new ArrayList() {{ - add(instance1); - add(instance2); - add(instance3); - }}; - NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instanceList); - List allCorrespondingServiceInstanceList = - NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName); - Assertions.assertEquals(0, allCorrespondingServiceInstanceList.size()); - } - - @Test - public void testGetAllCorrespondingServiceInstanceList() { - String serviceName = "serviceName"; - Set serviceNames = new HashSet() {{ - add("serviceName"); - add("serviceName1"); - }}; - NacosInstanceManageUtil.setCorrespondingServiceNames(serviceName, serviceNames); - - Instance instance1 = new Instance(); - instance1.setInstanceId("1"); - Instance instance2 = new Instance(); - instance2.setInstanceId("2"); - Instance instance3 = new Instance(); - instance3.setInstanceId("3"); - - List instanceList = new ArrayList() {{ - add(instance1); - add(instance2); - add(instance3); - }}; - NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instanceList); - List allCorrespondingServiceInstanceList = - NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName); - Assertions.assertEquals(3, allCorrespondingServiceInstanceList.size()); - } -} From 67d94aea5804e2a057537a04f4b509ef63200d56 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Sun, 9 Oct 2022 09:08:10 +0800 Subject: [PATCH 4/5] Revert "Local references support mergeable (#9645)" (#10707) This reverts commit 72326f42ba2add37b5f22541ad40fc3644dbe156. --- .../apache/dubbo/config/ReferenceConfig.java | 6 ++- .../dubbo/config/ReferenceConfigTest.java | 7 +-- .../registry/multiple/MultipleRegistry.java | 2 +- .../rpc/protocol/injvm/InjvmInvoker.java | 9 ++-- .../rpc/protocol/injvm/InjvmProtocol.java | 52 +------------------ .../rpc/protocol/injvm/Hello1ServiceImpl.java | 31 ----------- .../rpc/protocol/injvm/Hello2ServiceImpl.java | 33 ------------ .../rpc/protocol/injvm/HelloService.java | 24 --------- .../rpc/protocol/injvm/InjvmProtocolTest.java | 40 ++------------ 9 files changed, 23 insertions(+), 181 deletions(-) delete mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java delete mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java delete mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java 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 ed35be6278..f87b25e60d 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 @@ -543,7 +543,11 @@ public class ReferenceConfig extends ReferenceConfigBase { URL url = new ServiceConfigURL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName(), referenceParameters); url = url.setScopeModel(getScopeModel()); url = url.setServiceModel(consumerModel); - invoker = protocolSPI.refer(interfaceClass, url); + Invoker withFilter = protocolSPI.refer(interfaceClass, url); + // Local Invoke ( Support Cluster Filter / Filter ) + List> invokers = new ArrayList<>(); + invokers.add(withFilter); + invoker = Cluster.getCluster(url.getScopeModel(), Cluster.DEFAULT).join(new StaticDirectory(url, invokers), true); if (logger.isInfoEnabled()) { logger.info("Using in jvm service " + interfaceClass.getName()); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index 41943fc587..7d34883629 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -477,9 +477,10 @@ public class ReferenceConfigTest { .initialize(); referenceConfig.init(); - Invoker withFilter = ((ListenerInvokerWrapper) referenceConfig.getInvoker()).getInvoker(); - withFilter = ((MockClusterInvoker) withFilter).getDirectory().getAllInvokers().get(0); - Assertions.assertTrue(withFilter instanceof InjvmInvoker); + Assertions.assertTrue(referenceConfig.getInvoker() instanceof MockClusterInvoker); + Invoker withFilter = ((MockClusterInvoker) referenceConfig.getInvoker()).getDirectory().getAllInvokers().get(0); + Assertions.assertTrue(withFilter instanceof ListenerInvokerWrapper); + Assertions.assertTrue(((ListenerInvokerWrapper) withFilter).getInvoker() instanceof InjvmInvoker); URL url = withFilter.getUrl(); Assertions.assertEquals("application1", url.getParameter("application")); Assertions.assertEquals("value1", url.getParameter("key1")); diff --git a/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java b/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java index a28e8ad043..7a24a40779 100644 --- a/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java +++ b/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java @@ -263,7 +263,7 @@ public class MultipleRegistry extends AbstractRegistry { protected static class MultipleNotifyListenerWrapper implements NotifyListener { - Map registryMap = new ConcurrentHashMap<>(4); + Map registryMap = new ConcurrentHashMap(4); NotifyListener sourceNotifyListener; public MultipleNotifyListenerWrapper(NotifyListener sourceNotifyListener) { diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index 71587e6dd5..c06a815fdb 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -42,6 +42,7 @@ import org.apache.dubbo.rpc.support.RpcUtils; import java.lang.reflect.Type; import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -62,7 +63,7 @@ public class InjvmInvoker extends AbstractInvoker { private final String key; - private final Exporter exporter; + private final Map> exporterMap; private final ExecutorRepository executorRepository; @@ -70,10 +71,10 @@ public class InjvmInvoker extends AbstractInvoker { private final boolean shouldIgnoreSameModule; - InjvmInvoker(Class type, URL url, String key, Exporter exporter) { + InjvmInvoker(Class type, URL url, String key, Map> exporterMap) { super(type, url); this.key = key; - this.exporter = exporter; + this.exporterMap = exporterMap; this.executorRepository = url.getOrDefaultApplicationModel().getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel().getExtensionLoader(ParamDeepCopyUtil.class) .getExtension(url.getParameter(CommonConstants.INJVM_COPY_UTIL_KEY, DefaultParamDeepCopyUtil.NAME)); @@ -82,6 +83,7 @@ public class InjvmInvoker extends AbstractInvoker { @Override public boolean isAvailable() { + InjvmExporter exporter = (InjvmExporter) exporterMap.get(key); if (exporter == null) { return false; } else { @@ -91,6 +93,7 @@ public class InjvmInvoker extends AbstractInvoker { @Override public Result doInvoke(Invocation invocation) throws Throwable { + Exporter exporter = InjvmProtocol.getExporter(exporterMap, getUrl()); if (exporter == null) { throw new RpcException("Service [" + key + "] not found."); } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java index fdc9e101f6..75a51f37be 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java @@ -18,29 +18,19 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.CollectionUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.cluster.Cluster; -import org.apache.dubbo.rpc.cluster.ClusterInvoker; -import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; -import org.apache.dubbo.rpc.cluster.support.MergeableCluster; import org.apache.dubbo.rpc.model.ScopeModel; import org.apache.dubbo.rpc.protocol.AbstractProtocol; import org.apache.dubbo.rpc.support.ProtocolUtils; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.BROADCAST_CLUSTER; import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; 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; @@ -79,7 +69,7 @@ public class InjvmProtocol extends AbstractProtocol { if (result == null) { return null; } else if (ProtocolUtils.isGeneric( - result.getInvoker().getUrl().getParameter(GENERIC_KEY))) { + result.getInvoker().getUrl().getParameter(GENERIC_KEY))) { return null; } else { return result; @@ -98,15 +88,7 @@ public class InjvmProtocol extends AbstractProtocol { @Override public Invoker protocolBindingRefer(Class serviceType, URL url) throws RpcException { - // group="a,b" or group="*" - String group = url.getParameter(GROUP_KEY); - if (StringUtils.isNotEmpty(group)) { - if ((COMMA_SPLIT_PATTERN.split(group)).length > 1 || "*".equals(group)) { - return doCreateInvoker(url, Cluster.getCluster(url.getScopeModel(), MergeableCluster.NAME), serviceType); - } - } - Cluster cluster = Cluster.getCluster(url.getScopeModel(), url.getParameter(CLUSTER_KEY)); - return doCreateInvoker(url, cluster, serviceType); + return new InjvmInvoker(serviceType, url, url.getServiceKey(), exporterMap); } public boolean isInjvmRefer(URL url) { @@ -134,34 +116,4 @@ public class InjvmProtocol extends AbstractProtocol { return false; } } - - @SuppressWarnings({"unchecked", "rawtypes"}) - protected ClusterInvoker doCreateInvoker(URL url, Cluster cluster, Class type) { - StaticDirectory directory = new StaticDirectory(url, getInvokers(exporterMap, url, type)); - return (ClusterInvoker) cluster.join(directory, true); - } - - private List> getInvokers(Map> map, URL url, Class type) { - List> result = new ArrayList<>(); - - if (!url.getServiceKey().contains("*")) { - Exporter exporter = map.get(url.getServiceKey()); - InjvmInvoker invoker = new InjvmInvoker<>(type, url, url.getServiceKey(), exporter); - result.add(invoker); - } else { - if (CollectionUtils.isNotEmptyMap(map)) { - for (Exporter exporter : map.values()) { - if (UrlUtils.isServiceKeyMatch(url, exporter.getInvoker().getUrl())) { - URL providerUrl = exporter.getInvoker().getUrl(); - URL consumerUrl = url.addParameter(GROUP_KEY, providerUrl.getGroup()) - .addParameter(VERSION_KEY, providerUrl.getVersion()); - InjvmInvoker invoker = new InjvmInvoker<>(type, consumerUrl, consumerUrl.getServiceKey(), exporter); - result.add(invoker); - } - } - } - } - - return result; - } } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java deleted file mode 100644 index ee5b7737e2..0000000000 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.protocol.injvm; - - -import java.util.ArrayList; -import java.util.List; - -public class Hello1ServiceImpl implements HelloService { - - @Override - public List hellos() { - List res = new ArrayList<>(); - res.add("consumer-hello-1"); - return res; - } -} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java deleted file mode 100644 index 8937f5096c..0000000000 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.protocol.injvm; - - -import java.util.ArrayList; -import java.util.List; - - -public class Hello2ServiceImpl implements HelloService { - - @Override - public List hellos() { - List res = new ArrayList<>(); - res.add("consumer-hello-2"); - return res; - } -} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java deleted file mode 100644 index cd6bc45c62..0000000000 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.protocol.injvm; - -import java.util.List; - -public interface HelloService { - List hellos(); -} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java index d7dd373591..4e57d054b4 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java @@ -29,16 +29,18 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import static org.apache.dubbo.common.constants.CommonConstants.*; +import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.apache.dubbo.rpc.Constants.ASYNC_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_LOCAL; import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; -import static org.apache.dubbo.rpc.Constants.MERGER_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; @@ -74,7 +76,7 @@ public class InjvmProtocolTest { assertEquals(service.getSize(new String[]{"", "", ""}), 3); service.invoke("injvm://127.0.0.1/TestService", "invoke"); - InjvmInvoker injvmInvoker = new InjvmInvoker<>(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, null); + InjvmInvoker injvmInvoker = new InjvmInvoker<>(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, new HashMap<>()); assertFalse(injvmInvoker.isAvailable()); } @@ -135,36 +137,4 @@ public class InjvmProtocolTest { assertNull(service.getAsyncResult()); } - @Test - public void testLocalProtocolForMergeResult() throws Exception { - HelloService helloService1 = new Hello1ServiceImpl(); - URL url = URL.valueOf("injvm://127.0.0.1/HelloService") - .addParameter(INTERFACE_KEY, HelloService.class.getName()) - .addParameter(APPLICATION_KEY, "consumer") - .addParameter(GROUP_KEY, "g1"); - Invoker invoker1 = proxy.getInvoker(helloService1, HelloService.class, url); - assertTrue(invoker1.isAvailable()); - Exporter exporter1 = protocol.export(invoker1); - exporters.add(exporter1); - - URL url2 = URL.valueOf("injvm://127.0.0.1/HelloService") - .addParameter(INTERFACE_KEY, HelloService.class.getName()) - .addParameter(APPLICATION_KEY, "consumer") - .addParameter(GROUP_KEY, "g2"); - HelloService helloService2 = new Hello2ServiceImpl(); - Invoker invoker2 = proxy.getInvoker(helloService2, HelloService.class, url2); - assertTrue(invoker2.isAvailable()); - Exporter exporter2 = protocol.export(invoker2); - exporters.add(exporter2); - - - URL referUrl = URL.valueOf("injvm://127.0.0.1/HelloService") - .addParameter(INTERFACE_KEY, HelloService.class.getName()) - .addParameter(APPLICATION_KEY, "consumer") - .addParameter(GROUP_KEY, "*") - .addParameter(MERGER_KEY, "list"); - List list = proxy.getProxy(protocol.refer(HelloService.class, referUrl)).hellos(); - assertEquals(2, list.size()); - } - } From bfce75cf66689874b5cf8a6136546385ffbf3ba0 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Mon, 21 Nov 2022 11:31:29 +0800 Subject: [PATCH 5/5] Revert "Revert "Local references support mergeable (#9645)" (#10707)" This reverts commit 67d94aea5804e2a057537a04f4b509ef63200d56. --- .../apache/dubbo/config/ReferenceConfig.java | 6 +-- .../dubbo/config/ReferenceConfigTest.java | 7 ++- .../registry/multiple/MultipleRegistry.java | 2 +- .../rpc/protocol/injvm/InjvmInvoker.java | 9 ++-- .../rpc/protocol/injvm/InjvmProtocol.java | 52 ++++++++++++++++++- .../rpc/protocol/injvm/Hello1ServiceImpl.java | 31 +++++++++++ .../rpc/protocol/injvm/Hello2ServiceImpl.java | 33 ++++++++++++ .../rpc/protocol/injvm/HelloService.java | 24 +++++++++ .../rpc/protocol/injvm/InjvmProtocolTest.java | 40 ++++++++++++-- 9 files changed, 181 insertions(+), 23 deletions(-) create mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java create mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java create mode 100644 dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java 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 f87b25e60d..ed35be6278 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 @@ -543,11 +543,7 @@ public class ReferenceConfig extends ReferenceConfigBase { URL url = new ServiceConfigURL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName(), referenceParameters); url = url.setScopeModel(getScopeModel()); url = url.setServiceModel(consumerModel); - Invoker withFilter = protocolSPI.refer(interfaceClass, url); - // Local Invoke ( Support Cluster Filter / Filter ) - List> invokers = new ArrayList<>(); - invokers.add(withFilter); - invoker = Cluster.getCluster(url.getScopeModel(), Cluster.DEFAULT).join(new StaticDirectory(url, invokers), true); + invoker = protocolSPI.refer(interfaceClass, url); if (logger.isInfoEnabled()) { logger.info("Using in jvm service " + interfaceClass.getName()); diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index 7d34883629..41943fc587 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -477,10 +477,9 @@ public class ReferenceConfigTest { .initialize(); referenceConfig.init(); - Assertions.assertTrue(referenceConfig.getInvoker() instanceof MockClusterInvoker); - Invoker withFilter = ((MockClusterInvoker) referenceConfig.getInvoker()).getDirectory().getAllInvokers().get(0); - Assertions.assertTrue(withFilter instanceof ListenerInvokerWrapper); - Assertions.assertTrue(((ListenerInvokerWrapper) withFilter).getInvoker() instanceof InjvmInvoker); + Invoker withFilter = ((ListenerInvokerWrapper) referenceConfig.getInvoker()).getInvoker(); + withFilter = ((MockClusterInvoker) withFilter).getDirectory().getAllInvokers().get(0); + Assertions.assertTrue(withFilter instanceof InjvmInvoker); URL url = withFilter.getUrl(); Assertions.assertEquals("application1", url.getParameter("application")); Assertions.assertEquals("value1", url.getParameter("key1")); diff --git a/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java b/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java index 7a24a40779..a28e8ad043 100644 --- a/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java +++ b/dubbo-registry/dubbo-registry-multiple/src/main/java/org/apache/dubbo/registry/multiple/MultipleRegistry.java @@ -263,7 +263,7 @@ public class MultipleRegistry extends AbstractRegistry { protected static class MultipleNotifyListenerWrapper implements NotifyListener { - Map registryMap = new ConcurrentHashMap(4); + Map registryMap = new ConcurrentHashMap<>(4); NotifyListener sourceNotifyListener; public MultipleNotifyListenerWrapper(NotifyListener sourceNotifyListener) { diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java index c06a815fdb..71587e6dd5 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java @@ -42,7 +42,6 @@ import org.apache.dubbo.rpc.support.RpcUtils; import java.lang.reflect.Type; import java.util.HashMap; -import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -63,7 +62,7 @@ public class InjvmInvoker extends AbstractInvoker { private final String key; - private final Map> exporterMap; + private final Exporter exporter; private final ExecutorRepository executorRepository; @@ -71,10 +70,10 @@ public class InjvmInvoker extends AbstractInvoker { private final boolean shouldIgnoreSameModule; - InjvmInvoker(Class type, URL url, String key, Map> exporterMap) { + InjvmInvoker(Class type, URL url, String key, Exporter exporter) { super(type, url); this.key = key; - this.exporterMap = exporterMap; + this.exporter = exporter; this.executorRepository = url.getOrDefaultApplicationModel().getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); this.paramDeepCopyUtil = url.getOrDefaultFrameworkModel().getExtensionLoader(ParamDeepCopyUtil.class) .getExtension(url.getParameter(CommonConstants.INJVM_COPY_UTIL_KEY, DefaultParamDeepCopyUtil.NAME)); @@ -83,7 +82,6 @@ public class InjvmInvoker extends AbstractInvoker { @Override public boolean isAvailable() { - InjvmExporter exporter = (InjvmExporter) exporterMap.get(key); if (exporter == null) { return false; } else { @@ -93,7 +91,6 @@ public class InjvmInvoker extends AbstractInvoker { @Override public Result doInvoke(Invocation invocation) throws Throwable { - Exporter exporter = InjvmProtocol.getExporter(exporterMap, getUrl()); if (exporter == null) { throw new RpcException("Service [" + key + "] not found."); } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java index 75a51f37be..fdc9e101f6 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocol.java @@ -18,19 +18,29 @@ package org.apache.dubbo.rpc.protocol.injvm; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.CollectionUtils; +import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.cluster.Cluster; +import org.apache.dubbo.rpc.cluster.ClusterInvoker; +import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; +import org.apache.dubbo.rpc.cluster.support.MergeableCluster; import org.apache.dubbo.rpc.model.ScopeModel; import org.apache.dubbo.rpc.protocol.AbstractProtocol; import org.apache.dubbo.rpc.support.ProtocolUtils; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.BROADCAST_CLUSTER; import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; 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; @@ -69,7 +79,7 @@ public class InjvmProtocol extends AbstractProtocol { if (result == null) { return null; } else if (ProtocolUtils.isGeneric( - result.getInvoker().getUrl().getParameter(GENERIC_KEY))) { + result.getInvoker().getUrl().getParameter(GENERIC_KEY))) { return null; } else { return result; @@ -88,7 +98,15 @@ public class InjvmProtocol extends AbstractProtocol { @Override public Invoker protocolBindingRefer(Class serviceType, URL url) throws RpcException { - return new InjvmInvoker(serviceType, url, url.getServiceKey(), exporterMap); + // group="a,b" or group="*" + String group = url.getParameter(GROUP_KEY); + if (StringUtils.isNotEmpty(group)) { + if ((COMMA_SPLIT_PATTERN.split(group)).length > 1 || "*".equals(group)) { + return doCreateInvoker(url, Cluster.getCluster(url.getScopeModel(), MergeableCluster.NAME), serviceType); + } + } + Cluster cluster = Cluster.getCluster(url.getScopeModel(), url.getParameter(CLUSTER_KEY)); + return doCreateInvoker(url, cluster, serviceType); } public boolean isInjvmRefer(URL url) { @@ -116,4 +134,34 @@ public class InjvmProtocol extends AbstractProtocol { return false; } } + + @SuppressWarnings({"unchecked", "rawtypes"}) + protected ClusterInvoker doCreateInvoker(URL url, Cluster cluster, Class type) { + StaticDirectory directory = new StaticDirectory(url, getInvokers(exporterMap, url, type)); + return (ClusterInvoker) cluster.join(directory, true); + } + + private List> getInvokers(Map> map, URL url, Class type) { + List> result = new ArrayList<>(); + + if (!url.getServiceKey().contains("*")) { + Exporter exporter = map.get(url.getServiceKey()); + InjvmInvoker invoker = new InjvmInvoker<>(type, url, url.getServiceKey(), exporter); + result.add(invoker); + } else { + if (CollectionUtils.isNotEmptyMap(map)) { + for (Exporter exporter : map.values()) { + if (UrlUtils.isServiceKeyMatch(url, exporter.getInvoker().getUrl())) { + URL providerUrl = exporter.getInvoker().getUrl(); + URL consumerUrl = url.addParameter(GROUP_KEY, providerUrl.getGroup()) + .addParameter(VERSION_KEY, providerUrl.getVersion()); + InjvmInvoker invoker = new InjvmInvoker<>(type, consumerUrl, consumerUrl.getServiceKey(), exporter); + result.add(invoker); + } + } + } + } + + return result; + } } diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java new file mode 100644 index 0000000000..ee5b7737e2 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello1ServiceImpl.java @@ -0,0 +1,31 @@ +/* + * 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.protocol.injvm; + + +import java.util.ArrayList; +import java.util.List; + +public class Hello1ServiceImpl implements HelloService { + + @Override + public List hellos() { + List res = new ArrayList<>(); + res.add("consumer-hello-1"); + return res; + } +} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java new file mode 100644 index 0000000000..8937f5096c --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/Hello2ServiceImpl.java @@ -0,0 +1,33 @@ +/* + * 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.protocol.injvm; + + +import java.util.ArrayList; +import java.util.List; + + +public class Hello2ServiceImpl implements HelloService { + + @Override + public List hellos() { + List res = new ArrayList<>(); + res.add("consumer-hello-2"); + return res; + } +} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java new file mode 100644 index 0000000000..cd6bc45c62 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/HelloService.java @@ -0,0 +1,24 @@ +/* + * 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.protocol.injvm; + +import java.util.List; + +public interface HelloService { + List hellos(); +} diff --git a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java index 4e57d054b4..d7dd373591 100644 --- a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java @@ -29,18 +29,16 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.*; import static org.apache.dubbo.rpc.Constants.ASYNC_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_LOCAL; import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; +import static org.apache.dubbo.rpc.Constants.MERGER_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; @@ -76,7 +74,7 @@ public class InjvmProtocolTest { assertEquals(service.getSize(new String[]{"", "", ""}), 3); service.invoke("injvm://127.0.0.1/TestService", "invoke"); - InjvmInvoker injvmInvoker = new InjvmInvoker<>(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, new HashMap<>()); + InjvmInvoker injvmInvoker = new InjvmInvoker<>(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, null); assertFalse(injvmInvoker.isAvailable()); } @@ -137,4 +135,36 @@ public class InjvmProtocolTest { assertNull(service.getAsyncResult()); } + @Test + public void testLocalProtocolForMergeResult() throws Exception { + HelloService helloService1 = new Hello1ServiceImpl(); + URL url = URL.valueOf("injvm://127.0.0.1/HelloService") + .addParameter(INTERFACE_KEY, HelloService.class.getName()) + .addParameter(APPLICATION_KEY, "consumer") + .addParameter(GROUP_KEY, "g1"); + Invoker invoker1 = proxy.getInvoker(helloService1, HelloService.class, url); + assertTrue(invoker1.isAvailable()); + Exporter exporter1 = protocol.export(invoker1); + exporters.add(exporter1); + + URL url2 = URL.valueOf("injvm://127.0.0.1/HelloService") + .addParameter(INTERFACE_KEY, HelloService.class.getName()) + .addParameter(APPLICATION_KEY, "consumer") + .addParameter(GROUP_KEY, "g2"); + HelloService helloService2 = new Hello2ServiceImpl(); + Invoker invoker2 = proxy.getInvoker(helloService2, HelloService.class, url2); + assertTrue(invoker2.isAvailable()); + Exporter exporter2 = protocol.export(invoker2); + exporters.add(exporter2); + + + URL referUrl = URL.valueOf("injvm://127.0.0.1/HelloService") + .addParameter(INTERFACE_KEY, HelloService.class.getName()) + .addParameter(APPLICATION_KEY, "consumer") + .addParameter(GROUP_KEY, "*") + .addParameter(MERGER_KEY, "list"); + List list = proxy.getProxy(protocol.refer(HelloService.class, referUrl)).hellos(); + assertEquals(2, list.size()); + } + }