From 8fdce6f763663a8ac0a3c974cf9532cb45a88061 Mon Sep 17 00:00:00 2001 From: Albumen Date: Sat, 13 Feb 2021 01:20:19 +0800 Subject: [PATCH 1/6] clear parameter from registry url, preventing from polluting consumerUrl --- .../dubbo/rpc/cluster/directory/AbstractDirectory.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java index fffb40efe30..8d2c24e1c5b 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java @@ -72,7 +72,10 @@ public AbstractDirectory(URL url, RouterChain routerChain) { this.consumedProtocol = this.queryMap.get(PROTOCOL_KEY) == null ? DUBBO : this.queryMap.get(PROTOCOL_KEY); this.url = url.removeParameter(REFER_KEY).removeParameter(MONITOR_KEY); - this.consumerUrl = this.url.setProtocol(consumedProtocol).setPath(path == null ? queryMap.get(INTERFACE_KEY) : path).addParameters(queryMap) + this.consumerUrl = this.url.setProtocol(consumedProtocol) + .setPath(path == null ? queryMap.get(INTERFACE_KEY) : path) + .clearParameters() + .addParameters(queryMap) .removeParameter(MONITOR_KEY); setRouterChain(routerChain); From 65b8dac1aecc22a74d99150948fcdace02bdfdd5 Mon Sep 17 00:00:00 2001 From: Albumen Date: Sat, 13 Feb 2021 01:36:45 +0800 Subject: [PATCH 2/6] recover most of param --- .../apache/dubbo/rpc/cluster/directory/AbstractDirectory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java index 8d2c24e1c5b..2d72178965c 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java @@ -32,6 +32,7 @@ import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; +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.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; @@ -74,7 +75,7 @@ public AbstractDirectory(URL url, RouterChain routerChain) { this.consumerUrl = this.url.setProtocol(consumedProtocol) .setPath(path == null ? queryMap.get(INTERFACE_KEY) : path) - .clearParameters() + .removeParameter(GROUP_KEY) .addParameters(queryMap) .removeParameter(MONITOR_KEY); From 158a8ac85db810bd10a28e79501c7ea47164b146 Mon Sep 17 00:00:00 2001 From: Albumen Date: Sat, 13 Feb 2021 21:22:46 +0800 Subject: [PATCH 3/6] make remove parameters optional --- .../cluster/directory/AbstractDirectory.java | 22 +- .../cluster/directory/StaticDirectory.java | 226 +++++++++--------- .../integration/DynamicDirectory.java | 2 +- 3 files changed, 127 insertions(+), 123 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java index 2d72178965c..fb09057ca62 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java @@ -32,7 +32,6 @@ import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; -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.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; @@ -41,7 +40,6 @@ /** * Abstract implementation of Directory: Invoker list returned from this Directory's list method have been filtered by Routers - * */ public abstract class AbstractDirectory implements Directory { @@ -60,10 +58,14 @@ public abstract class AbstractDirectory implements Directory { protected RouterChain routerChain; public AbstractDirectory(URL url) { - this(url, null); + this(url, null, false); + } + + public AbstractDirectory(URL url, boolean isUrlFromRegistry) { + this(url, null, isUrlFromRegistry); } - public AbstractDirectory(URL url, RouterChain routerChain) { + public AbstractDirectory(URL url, RouterChain routerChain, boolean isUrlFromRegistry) { if (url == null) { throw new IllegalArgumentException("url == null"); } @@ -73,11 +75,13 @@ public AbstractDirectory(URL url, RouterChain routerChain) { this.consumedProtocol = this.queryMap.get(PROTOCOL_KEY) == null ? DUBBO : this.queryMap.get(PROTOCOL_KEY); this.url = url.removeParameter(REFER_KEY).removeParameter(MONITOR_KEY); - this.consumerUrl = this.url.setProtocol(consumedProtocol) - .setPath(path == null ? queryMap.get(INTERFACE_KEY) : path) - .removeParameter(GROUP_KEY) - .addParameters(queryMap) - .removeParameter(MONITOR_KEY); + URL consumerUrlFrom = this.url.setProtocol(consumedProtocol) + .setPath(path == null ? queryMap.get(INTERFACE_KEY) : path); + if (isUrlFromRegistry) { + // reserve parameters if url is already a consumer url + consumerUrlFrom.clearParameters(); + } + this.consumerUrl = consumerUrlFrom.addParameters(queryMap).removeParameter(MONITOR_KEY); setRouterChain(routerChain); } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java index 0595c6366ef..253c5e2f8cc 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java @@ -1,113 +1,113 @@ -/* - * 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; - -/** - * StaticDirectory - */ -public class StaticDirectory extends AbstractDirectory { - private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class); - - private final List> invokers; - - public StaticDirectory(List> invokers) { - this(null, invokers, null); - } - - public StaticDirectory(List> invokers, RouterChain routerChain) { - this(null, invokers, routerChain); - } - - public StaticDirectory(URL url, List> invokers) { - this(url, invokers, null); - } - - public StaticDirectory(URL url, List> invokers, RouterChain routerChain) { - super(url == null && CollectionUtils.isNotEmpty(invokers) ? invokers.get(0).getUrl() : url, routerChain); - if (CollectionUtils.isEmpty(invokers)) { - throw new IllegalArgumentException("invokers == null"); - } - this.invokers = invokers; - } - - @Override - public Class getInterface() { - return invokers.get(0).getInterface(); - } - - @Override - public List> getAllInvokers() { - return invokers; - } - - @Override - public boolean isAvailable() { - if (isDestroyed()) { - return false; - } - for (Invoker invoker : invokers) { - if (invoker.isAvailable()) { - return true; - } - } - return false; - } - - @Override - public void destroy() { - if (isDestroyed()) { - return; - } - super.destroy(); - for (Invoker invoker : invokers) { - invoker.destroy(); - } - invokers.clear(); - } - - public void buildRouterChain() { - RouterChain routerChain = RouterChain.buildChain(getUrl()); - routerChain.setInvokers(invokers); - this.setRouterChain(routerChain); - } - - @Override - protected List> doList(Invocation invocation) throws RpcException { - List> 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; - } - -} +/* + * 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; + +/** + * StaticDirectory + */ +public class StaticDirectory extends AbstractDirectory { + private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class); + + private final List> invokers; + + public StaticDirectory(List> invokers) { + this(null, invokers, null); + } + + public StaticDirectory(List> invokers, RouterChain routerChain) { + this(null, invokers, routerChain); + } + + public StaticDirectory(URL url, List> invokers) { + this(url, invokers, null); + } + + public StaticDirectory(URL url, List> invokers, RouterChain 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 getInterface() { + return invokers.get(0).getInterface(); + } + + @Override + public List> getAllInvokers() { + return invokers; + } + + @Override + public boolean isAvailable() { + if (isDestroyed()) { + return false; + } + for (Invoker invoker : invokers) { + if (invoker.isAvailable()) { + return true; + } + } + return false; + } + + @Override + public void destroy() { + if (isDestroyed()) { + return; + } + super.destroy(); + for (Invoker invoker : invokers) { + invoker.destroy(); + } + invokers.clear(); + } + + public void buildRouterChain() { + RouterChain routerChain = RouterChain.buildChain(getUrl()); + routerChain.setInvokers(invokers); + this.setRouterChain(routerChain); + } + + @Override + protected List> doList(Invocation invocation) throws RpcException { + List> 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-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/DynamicDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/DynamicDirectory.java index 01b0600f461..486fd579beb 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/DynamicDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/DynamicDirectory.java @@ -95,7 +95,7 @@ public abstract class DynamicDirectory extends AbstractDirectory implement protected ServiceInstancesChangedListener serviceListener; public DynamicDirectory(Class serviceType, URL url) { - super(url); + super(url, true); if (serviceType == null) { throw new IllegalArgumentException("service type is null."); } From 67f224238388775834a669cb71cd4457f50eb494 Mon Sep 17 00:00:00 2001 From: Albumen Date: Sat, 13 Feb 2021 22:33:46 +0800 Subject: [PATCH 4/6] fix refer --- .../apache/dubbo/rpc/cluster/directory/AbstractDirectory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java index fb09057ca62..95a540c9300 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java @@ -79,7 +79,7 @@ public AbstractDirectory(URL url, RouterChain routerChain, boolean isUrlFromR .setPath(path == null ? queryMap.get(INTERFACE_KEY) : path); if (isUrlFromRegistry) { // reserve parameters if url is already a consumer url - consumerUrlFrom.clearParameters(); + consumerUrlFrom = consumerUrlFrom.clearParameters(); } this.consumerUrl = consumerUrlFrom.addParameters(queryMap).removeParameter(MONITOR_KEY); From 4e3e9c13c30c5c54f8c7f3abd9dfd848a5481dee Mon Sep 17 00:00:00 2001 From: Albumen Date: Sat, 20 Feb 2021 11:48:35 +0800 Subject: [PATCH 5/6] recover crlf --- .../cluster/directory/StaticDirectory.java | 227 +++++++++--------- 1 file changed, 114 insertions(+), 113 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java index 253c5e2f8cc..442c61758da 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java @@ -1,113 +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; - -/** - * StaticDirectory - */ -public class StaticDirectory extends AbstractDirectory { - private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class); - - private final List> invokers; - - public StaticDirectory(List> invokers) { - this(null, invokers, null); - } - - public StaticDirectory(List> invokers, RouterChain routerChain) { - this(null, invokers, routerChain); - } - - public StaticDirectory(URL url, List> invokers) { - this(url, invokers, null); - } - - public StaticDirectory(URL url, List> invokers, RouterChain 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 getInterface() { - return invokers.get(0).getInterface(); - } - - @Override - public List> getAllInvokers() { - return invokers; - } - - @Override - public boolean isAvailable() { - if (isDestroyed()) { - return false; - } - for (Invoker invoker : invokers) { - if (invoker.isAvailable()) { - return true; - } - } - return false; - } - - @Override - public void destroy() { - if (isDestroyed()) { - return; - } - super.destroy(); - for (Invoker invoker : invokers) { - invoker.destroy(); - } - invokers.clear(); - } - - public void buildRouterChain() { - RouterChain routerChain = RouterChain.buildChain(getUrl()); - routerChain.setInvokers(invokers); - this.setRouterChain(routerChain); - } - - @Override - protected List> doList(Invocation invocation) throws RpcException { - List> 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; - } - -} +/* + * 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; + +/** + * StaticDirectory + */ +public class StaticDirectory extends AbstractDirectory { + + private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class); + + private final List> invokers; + + public StaticDirectory(List> invokers) { + this(null, invokers, null); + } + + public StaticDirectory(List> invokers, RouterChain routerChain) { + this(null, invokers, routerChain); + } + + public StaticDirectory(URL url, List> invokers) { + this(url, invokers, null); + } + + public StaticDirectory(URL url, List> invokers, RouterChain 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 getInterface() { + return invokers.get(0).getInterface(); + } + + @Override + public List> getAllInvokers() { + return invokers; + } + + @Override + public boolean isAvailable() { + if (isDestroyed()) { + return false; + } + for (Invoker invoker : invokers) { + if (invoker.isAvailable()) { + return true; + } + } + return false; + } + + @Override + public void destroy() { + if (isDestroyed()) { + return; + } + super.destroy(); + for (Invoker invoker : invokers) { + invoker.destroy(); + } + invokers.clear(); + } + + public void buildRouterChain() { + RouterChain routerChain = RouterChain.buildChain(getUrl()); + routerChain.setInvokers(invokers); + this.setRouterChain(routerChain); + } + + @Override + protected List> doList(Invocation invocation) throws RpcException { + List> 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; + } + +} From e4a1524b6f6058c0a4309553632e33dcab4356f2 Mon Sep 17 00:00:00 2001 From: Albumen Date: Sat, 20 Feb 2021 11:49:24 +0800 Subject: [PATCH 6/6] recover crlf --- .../org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java index 442c61758da..db1b2024838 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java @@ -32,7 +32,6 @@ * StaticDirectory */ public class StaticDirectory extends AbstractDirectory { - private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class); private final List> invokers;