From df3e16c495b32ead5823bed1579fbe3e6ff86dac Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Mon, 18 Feb 2019 18:23:20 +0800 Subject: [PATCH 1/5] refactor URL to URLBuilder. #3494 --- .../router/file/FileRouterFactory.java | 8 +- .../java/org/apache/dubbo/common/URL.java | 40 +- .../org/apache/dubbo/common/URLBuilder.java | 350 ++++++++++++++++++ .../apache/dubbo/common/URLBuilderTest.java | 100 +++++ .../dubbo/config/AbstractInterfaceConfig.java | 13 +- .../apache/dubbo/config/ServiceConfig.java | 6 +- .../config/spring/SimpleRegistryExporter.java | 8 +- .../integration/MetadataReportService.java | 6 +- .../monitor/dubbo/DubboMonitorFactory.java | 12 +- .../integration/RegistryDirectory.java | 7 +- .../integration/RegistryProtocol.java | 6 +- .../support/AbstractRegistryFactory.java | 7 +- .../registry/dubbo/DubboRegistryFactory.java | 7 +- .../dubbo/registry/redis/RedisRegistry.java | 7 +- .../registry/zookeeper/ZookeeperRegistry.java | 6 +- .../wrapper/StubProxyFactoryWrapper.java | 14 +- .../rpc/protocol/dubbo/DubboProtocol.java | 13 +- .../dubbo/ReferenceCountExchangeClient.java | 7 +- .../rpc/protocol/rest/RestProtocolTest.java | 7 +- 19 files changed, 565 insertions(+), 59 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java index 2d509422d12..075dd5cbc63 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.utils.IOUtils; import org.apache.dubbo.rpc.cluster.Router; import org.apache.dubbo.rpc.cluster.RouterFactory; @@ -55,9 +56,12 @@ public Router getRouter(URL url) { // FIXME: this code looks useless boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false); - URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type) + URL script = new URLBuilder(url) + .setProtocol(protocol) + .addParameter(Constants.TYPE_KEY, type) .addParameter(Constants.RUNTIME_KEY, runtime) - .addParameterAndEncoded(Constants.RULE_KEY, rule); + .addParameterAndEncoded(Constants.RULE_KEY, rule) + .build(); return routerFactory.getRouter(script); } catch (IOException e) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 780914066fd..9c617e6e29a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -71,7 +71,7 @@ * @see java.net.URL * @see java.net.URI */ -public /**final**/ +public /*final**/ class URL implements Serializable { private static final long serialVersionUID = -1985165475234910535L; @@ -166,9 +166,9 @@ public URL(String protocol, String username, String password, String host, int p } this.path = path; if (parameters == null) { - parameters = new HashMap(); + parameters = new HashMap<>(); } else { - parameters = new HashMap(parameters); + parameters = new HashMap<>(parameters); } this.parameters = Collections.unmodifiableMap(parameters); } @@ -191,10 +191,10 @@ public static URL valueOf(String url) { int port = 0; String path = null; Map parameters = null; - int i = url.indexOf("?"); // seperator between body and parameters + int i = url.indexOf("?"); // separator between body and parameters if (i >= 0) { - String[] parts = url.substring(i + 1).split("\\&"); - parameters = new HashMap(); + String[] parts = url.substring(i + 1).split("&"); + parameters = new HashMap<>(); for (String part : parts) { part = part.trim(); if (part.length() > 0) { @@ -265,7 +265,7 @@ public static URL valueOf(String url, String... reserveParams) { if (reserveParams == null || reserveParams.length == 0) { return result; } - Map newMap = new HashMap(reserveParams.length); + Map newMap = new HashMap<>(reserveParams.length); Map oldMap = result.getParameters(); for (String reserveParam : reserveParams) { String tmp = oldMap.get(reserveParam); @@ -277,7 +277,7 @@ public static URL valueOf(String url, String... reserveParams) { } public static URL valueOf(URL url, String[] reserveParams, String[] reserveParamPrefixs) { - Map newMap = new HashMap(); + Map newMap = new HashMap<>(); Map oldMap = url.getParameters(); if (reserveParamPrefixs != null && reserveParamPrefixs.length != 0) { for (Map.Entry entry : oldMap.entrySet()) { @@ -425,7 +425,7 @@ public String getBackupAddress(int defaultPort) { } public List getBackupUrls() { - List urls = new ArrayList(); + List urls = new ArrayList<>(); urls.add(this); String[] backups = getParameter(Constants.BACKUP_KEY, new String[0]); if (backups != null && backups.length > 0) { @@ -510,14 +510,14 @@ public List getParameter(String key, List defaultValue) { private Map getNumbers() { if (numbers == null) { // concurrent initialization is tolerant - numbers = new ConcurrentHashMap(); + numbers = new ConcurrentHashMap<>(); } return numbers; } private Map getUrls() { if (urls == null) { // concurrent initialization is tolerant - urls = new ConcurrentHashMap(); + urls = new ConcurrentHashMap<>(); } return urls; } @@ -1004,7 +1004,7 @@ public URL addParameter(String key, String value) { return this; } - Map map = new HashMap(getParameters()); + Map map = new HashMap<>(getParameters()); map.put(key, value); return new URL(protocol, username, password, host, port, path, map); } @@ -1017,7 +1017,7 @@ public URL addParameterIfAbsent(String key, String value) { if (hasParameter(key)) { return this; } - Map map = new HashMap(getParameters()); + Map map = new HashMap<>(getParameters()); map.put(key, value); return new URL(protocol, username, password, host, port, path, map); } @@ -1053,7 +1053,7 @@ public URL addParameters(Map parameters) { return this; } - Map map = new HashMap(getParameters()); + Map map = new HashMap<>(getParameters()); map.putAll(parameters); return new URL(protocol, username, password, host, port, path, map); } @@ -1062,7 +1062,7 @@ public URL addParametersIfAbsent(Map parameters) { if (CollectionUtils.isEmptyMap(parameters)) { return this; } - Map map = new HashMap(parameters); + Map map = new HashMap<>(parameters); map.putAll(getParameters()); return new URL(protocol, username, password, host, port, path, map); } @@ -1074,7 +1074,7 @@ public URL addParameters(String... pairs) { if (pairs.length % 2 != 0) { throw new IllegalArgumentException("Map pairs can not be odd number."); } - Map map = new HashMap(); + Map map = new HashMap<>(); int len = pairs.length / 2; for (int i = 0; i < len; i++) { map.put(pairs[2 * i], pairs[2 * i + 1]); @@ -1107,7 +1107,7 @@ public URL removeParameters(String... keys) { if (keys == null || keys.length == 0) { return this; } - Map map = new HashMap(getParameters()); + Map map = new HashMap<>(getParameters()); for (String key : keys) { map.remove(key); } @@ -1118,7 +1118,7 @@ public URL removeParameters(String... keys) { } public URL clearParameters() { - return new URL(protocol, username, password, host, port, path, new HashMap()); + return new URL(protocol, username, password, host, port, path, new HashMap<>()); } public String getRawParameter(String key) { @@ -1144,7 +1144,7 @@ public String getRawParameter(String key) { } public Map toMap() { - Map map = new HashMap(parameters); + Map map = new HashMap<>(parameters); if (protocol != null) { map.put("protocol", protocol); } @@ -1217,7 +1217,7 @@ private void buildParameters(StringBuilder buf, boolean concat, String[] paramet if (CollectionUtils.isNotEmptyMap(getParameters())) { List includes = (ArrayUtils.isEmpty(parameters) ? null : Arrays.asList(parameters)); boolean first = true; - for (Map.Entry entry : new TreeMap(getParameters()).entrySet()) { + for (Map.Entry entry : new TreeMap<>(getParameters()).entrySet()) { if (entry.getKey() != null && entry.getKey().length() > 0 && (includes == null || includes.contains(entry.getKey()))) { if (first) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java new file mode 100644 index 00000000000..49089e9fc0e --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java @@ -0,0 +1,350 @@ +/* + * 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.common; + +import org.apache.dubbo.common.utils.CollectionUtils; +import org.apache.dubbo.common.utils.StringUtils; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public final class URLBuilder { + private String protocol; + + private String username; + + private String password; + + // by default, host to registry + private String host; + + // by default, port to registry + private int port; + + private String path; + + private Map parameters; + + public URLBuilder() { + protocol = null; + username = null; + password = null; + host = null; + port = 0; + path = null; + parameters = new HashMap<>(); + } + + public URLBuilder(String protocol, String host, int port) { + this(protocol, null, null, host, port, null, null); + } + + public URLBuilder(String protocol, String host, int port, String[] pairs) { // varargs ... conflict with the following path argument, use array instead. + this(protocol, null, null, host, port, null, CollectionUtils.toStringMap(pairs)); + } + + public URLBuilder(String protocol, String host, int port, Map parameters) { + this(protocol, null, null, host, port, null, parameters); + } + + public URLBuilder(String protocol, String host, int port, String path) { + this(protocol, null, null, host, port, path, null); + } + + public URLBuilder(String protocol, String host, int port, String path, String... pairs) { + this(protocol, null, null, host, port, path, CollectionUtils.toStringMap(pairs)); + } + + public URLBuilder(String protocol, String host, int port, String path, Map parameters) { + this(protocol, null, null, host, port, path, parameters); + } + + public URLBuilder(String protocol, String username, String password, String host, int port, String path, Map parameters) { + this.protocol = protocol; + this.username = username; + this.password = password; + this.host = host; + this.port = port; + this.path = path; + this.parameters = parameters != null ? parameters : new HashMap<>(); + } + + public static URLBuilder from(URL url) { + String protocol = url.getProtocol(); + String username = url.getUsername(); + String password = url.getPassword(); + String host = url.getHost(); + int port = url.getPort(); + String path = url.getPath(); + Map parameters = new HashMap<>(url.getParameters()); + return new URLBuilder( + protocol, + username, + password, + host, + port, + path, + parameters); + } + + public URL build() { + if (StringUtils.isEmpty(username) && StringUtils.isNotEmpty(password)) { + throw new IllegalArgumentException("Invalid url, password without username!"); + } + port = port < 0 ? 0 : port; + // trim the leading "/" + int firstNonSlash = 0; + if (path != null) { + while (firstNonSlash < path.length() && path.charAt(firstNonSlash) == '/') { + firstNonSlash++; + } + if (firstNonSlash >= path.length()) { + path = ""; + } else if (firstNonSlash > 0) { + path = path.substring(firstNonSlash); + } + } + return new URL(protocol, username, password, host, port, path, parameters); + } + + + public URLBuilder setProtocol(String protocol) { + this.protocol = protocol; + return this; + } + + public URLBuilder setUsername(String username) { + this.username = username; + return this; + } + + public URLBuilder setPassword(String password) { + this.password = password; + return this; + } + + public URLBuilder setHost(String host) { + this.host = host; + return this; + } + + public URLBuilder setPort(int port) { + this.port = port; + return this; + } + + public URLBuilder setAddress(String address) { + int i = address.lastIndexOf(':'); + String host; + int port = this.port; + if (i >= 0) { + host = address.substring(0, i); + port = Integer.parseInt(address.substring(i + 1)); + } else { + host = address; + } + this.host = host; + this.port = port; + return this; + } + + public URLBuilder setPath(String path) { + this.path = path; + return this; + } + + public URLBuilder addParameterAndEncoded(String key, String value) { + if (StringUtils.isEmpty(value)) { + return this; + } + return addParameter(key, URL.encode(value)); + } + + public URLBuilder addParameter(String key, boolean value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, char value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, byte value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, short value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, int value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, long value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, float value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, double value) { + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, Enum value) { + if (value == null) { + return this; + } + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, Number value) { + if (value == null) { + return this; + } + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, CharSequence value) { + if (value == null || value.length() == 0) { + return this; + } + return addParameter(key, String.valueOf(value)); + } + + public URLBuilder addParameter(String key, String value) { + if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { + return this; + } + // if value doesn't change, return immediately + if (value.equals(parameters.get(key))) { // value != null + return this; + } + + parameters.put(key, value); + return this; + } + + public URLBuilder addParameterIfAbsent(String key, String value) { + if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { + return this; + } + if (hasParameter(key)) { + return this; + } + parameters.put(key, value); + return this; + } + + public URLBuilder addParameters(Map parameters) { + if (CollectionUtils.isEmptyMap(parameters)) { + return this; + } + + boolean hasAndEqual = true; + for (Map.Entry entry : parameters.entrySet()) { + String oldValue = this.parameters.get(entry.getKey()); + String newValue = entry.getValue(); + if (!Objects.equals(oldValue, newValue)) { + hasAndEqual = false; + break; + } + } + // return immediately if there's no change + if (hasAndEqual) { + return this; + } + + this.parameters.putAll(parameters); + return this; + } + + public URLBuilder addParametersIfAbsent(Map parameters) { + if (CollectionUtils.isEmptyMap(parameters)) { + return this; + } + this.parameters.putAll(parameters); + return this; + } + + public URLBuilder addParameters(String... pairs) { + if (pairs == null || pairs.length == 0) { + return this; + } + if (pairs.length % 2 != 0) { + throw new IllegalArgumentException("Map pairs can not be odd number."); + } + Map map = new HashMap<>(); + int len = pairs.length / 2; + for (int i = 0; i < len; i++) { + map.put(pairs[2 * i], pairs[2 * i + 1]); + } + return addParameters(map); + } + + public URLBuilder addParameterString(String query) { + if (StringUtils.isEmpty(query)) { + return this; + } + return addParameters(StringUtils.parseQueryString(query)); + } + + public URLBuilder removeParameter(String key) { + if (StringUtils.isEmpty(key)) { + return this; + } + return removeParameters(key); + } + + public URLBuilder removeParameters(Collection keys) { + if (CollectionUtils.isEmpty(keys)) { + return this; + } + return removeParameters(keys.toArray(new String[0])); + } + + public URLBuilder removeParameters(String... keys) { + if (keys == null || keys.length == 0) { + return this; + } + for (String key : keys) { + parameters.remove(key); + } + return this; + } + + public URLBuilder clearParameters() { + parameters.clear(); + return this; + } + + public boolean hasParameter(String key) { + String value = getParameter(key); + return value != null && value.length() > 0; + } + + public String getParameter(String key) { + String value = parameters.get(key); + if (StringUtils.isEmpty(value)) { + value = parameters.get(Constants.DEFAULT_KEY_PREFIX + key); + } + return value; + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java new file mode 100644 index 00000000000..c607bd385c6 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLBuilderTest.java @@ -0,0 +1,100 @@ +/* + * 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.common; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +public class URLBuilderTest { + @Test + public void testNoArgConstructor() { + URL url = new URLBuilder().build(); + assertThat(url.toString(), equalTo("")); + } + + @Test + public void shouldAddParameter() { + URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan"); + URL url2 = URLBuilder.from(url1) + .addParameter("newKey1", "newValue1") // string + .addParameter("newKey2", 2) // int + .addParameter("version", 1) // override + .build(); + assertThat(url2.getParameter("newKey1"), equalTo("newValue1")); + assertThat(url2.getParameter("newKey2"), equalTo("2")); + assertThat(url2.getParameter("version"), equalTo("1")); + } + + @Test + public void shouldSet() { + URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan"); + URL url2 = URLBuilder.from(url1) + .setProtocol("rest") + .setUsername("newUsername") + .setPassword("newPassword") + .setHost("newHost") + .setPath("newContext") + .setPort(1234) + .build(); + assertThat(url2.getProtocol(), equalTo("rest")); + assertThat(url2.getUsername(), equalTo("newUsername")); + assertThat(url2.getPassword(), equalTo("newPassword")); + assertThat(url2.getHost(), equalTo("newHost")); + assertThat(url2.getPort(), equalTo(1234)); + assertThat(url2.getPath(), equalTo("newContext")); + + url2 = URLBuilder.from(url1) + .setAddress("newHost2:2345") + .build(); + assertThat(url2.getHost(), equalTo("newHost2")); + assertThat(url2.getPort(), equalTo(2345)); + } + + @Test + public void shouldClearParameters() { + URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan"); + URL url2 = URLBuilder.from(url1) + .clearParameters() + .build(); + assertThat(url2.getParameters().size(), equalTo(0)); + } + + @Test + public void shouldRemoveParameters() { + URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan&key2=v2"); + URL url2 = URLBuilder.from(url1) + .removeParameters(Arrays.asList("key2", "application")) + .build(); + assertThat(url2.getParameters().size(), equalTo(1)); + assertThat(url2.getParameter("version"), equalTo("1.0.0")); + } + + @Test + public void shouldAddIfAbsent() { + URL url1 = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?version=1.0.0&application=morgan&key2=v2"); + URL url2 = URLBuilder.from(url1) + .addParameterIfAbsent("absentKey", "absentValue") + .addParameterIfAbsent("version", "2.0.0") // should not override + .build(); + assertThat(url2.getParameter("version"), equalTo("1.0.0")); + assertThat(url2.getParameter("absentKey"), equalTo("absentValue")); + } +} diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index 937ed377344..fa696cf0bce 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.extension.ExtensionLoader; @@ -304,8 +305,10 @@ protected List loadRegistries(boolean provider) { List urls = UrlUtils.parseURLs(address, map); for (URL url : urls) { - url = url.addParameter(Constants.REGISTRY_KEY, url.getProtocol()); - url = url.setProtocol(Constants.REGISTRY_PROTOCOL); + url = new URLBuilder(url) + .addParameter(Constants.REGISTRY_KEY, url.getProtocol()) + .setProtocol(Constants.REGISTRY_PROTOCOL) + .build(); if ((provider && url.getParameter(Constants.REGISTER_KEY, true)) || (!provider && url.getParameter(Constants.SUBSCRIBE_KEY, true))) { registryList.add(url); @@ -352,7 +355,11 @@ protected URL loadMonitor(URL registryURL) { } return UrlUtils.parseURL(address, map); } else if (Constants.REGISTRY_PROTOCOL.equals(monitor.getProtocol()) && registryURL != null) { - return registryURL.setProtocol(Constants.DUBBO_PROTOCOL).addParameter(Constants.PROTOCOL_KEY, Constants.REGISTRY_PROTOCOL).addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)); + return new URLBuilder(registryURL) + .setProtocol(Constants.DUBBO_PROTOCOL) + .addParameter(Constants.PROTOCOL_KEY, Constants.REGISTRY_PROTOCOL) + .addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)) + .build(); } return null; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index 73fd83ffa51..9e687e8126c 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.config.Environment; @@ -570,10 +571,11 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r @SuppressWarnings({"unchecked", "rawtypes"}) private void exportLocal(URL url) { if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) { - URL local = URL.valueOf(url.toFullString()) + URL local = new URLBuilder(url) .setProtocol(Constants.LOCAL_PROTOCOL) .setHost(LOCALHOST_VALUE) - .setPort(0); + .setPort(0) + .build(); Exporter exporter = protocol.export( proxyFactory.getInvoker(ref, (Class) interfaceClass, local)); exporters.add(exporter); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java index 39878595e09..0db362af074 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryExporter.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.RegistryService; @@ -52,14 +53,15 @@ public static Exporter export(int port) { public static Exporter export(int port, RegistryService registryService) { return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class, - new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName()) + new URLBuilder(Constants.DUBBO_PROTOCOL, NetUtils.getLocalHost(), port, RegistryService.class.getName()) .setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .addParameter(Constants.CLUSTER_STICKY_KEY, "true") .addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "1000") .addParameter("ondisconnect", "disconnect") .addParameter("subscribe.1.callback", "true") - .addParameter("unsubscribe.1.callback", "false"))); + .addParameter("unsubscribe.1.callback", "false") + .build())); } -} \ No newline at end of file +} diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java index c457950f2f8..c93498cb5bb 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; @@ -48,7 +49,10 @@ public class MetadataReportService { MetadataReportService(URL metadataReportURL) { if (Constants.METADATA_REPORT_KEY.equals(metadataReportURL.getProtocol())) { String protocol = metadataReportURL.getParameter(Constants.METADATA_REPORT_KEY, Constants.DEFAULT_DIRECTORY); - metadataReportURL = metadataReportURL.setProtocol(protocol).removeParameter(Constants.METADATA_REPORT_KEY); + metadataReportURL = new URLBuilder(metadataReportURL) + .setProtocol(protocol) + .removeParameter(Constants.METADATA_REPORT_KEY) + .build(); } this.metadataReportUrl = metadataReportURL; metadataReport = metadataReportFactory.getMetadataReport(this.metadataReportUrl); diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java index 9d8e70f3f5c..9d001fc67b4 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java @@ -17,6 +17,7 @@ package org.apache.dubbo.monitor.dubbo; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorService; @@ -26,6 +27,7 @@ import org.apache.dubbo.rpc.ProxyFactory; import static org.apache.dubbo.common.Constants.CHECK_KEY; +import static org.apache.dubbo.common.Constants.DUBBO_PROTOCOL; import static org.apache.dubbo.common.Constants.PROTOCOL_KEY; import static org.apache.dubbo.common.Constants.REFERENCE_FILTER_KEY; @@ -48,9 +50,10 @@ public void setProxyFactory(ProxyFactory proxyFactory) { @Override protected Monitor createMonitor(URL url) { - url = url.setProtocol(url.getParameter(PROTOCOL_KEY, "dubbo")); + URLBuilder urlBuilder = new URLBuilder(url); + urlBuilder.setProtocol(url.getParameter(PROTOCOL_KEY, DUBBO_PROTOCOL)); if (StringUtils.isEmpty(url.getPath())) { - url = url.setPath(MonitorService.class.getName()); + urlBuilder.setPath(MonitorService.class.getName()); } String filter = url.getParameter(REFERENCE_FILTER_KEY); if (StringUtils.isEmpty(filter)) { @@ -58,8 +61,9 @@ protected Monitor createMonitor(URL url) { } else { filter = filter + ","; } - url = url.addParameters(CHECK_KEY, String.valueOf(false), REFERENCE_FILTER_KEY, filter + "-monitor"); - Invoker monitorInvoker = protocol.refer(MonitorService.class, url); + urlBuilder.addParameters(CHECK_KEY, String.valueOf(false), + REFERENCE_FILTER_KEY, filter + "-monitor"); + Invoker monitorInvoker = protocol.refer(MonitorService.class, urlBuilder.build()); MonitorService monitorService = proxyFactory.getProxy(monitorInvoker); return new DubboMonitor(monitorInvoker, monitorService); } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index f843871b6db..2593b2e081c 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; @@ -133,10 +134,12 @@ private URL turnRegistryUrlToConsumerUrl(URL url) { if (StringUtils.isNotEmpty(isDefault)) { queryMap.put(Constants.REGISTRY_KEY + "." + Constants.DEFAULT_KEY, isDefault); } - return url.setPath(url.getServiceInterface()) + return URLBuilder.from(url) + .setPath(url.getServiceInterface()) .clearParameters() .addParameters(queryMap) - .removeParameter(Constants.MONITOR_KEY); + .removeParameter(Constants.MONITOR_KEY) + .build(); } public void setProtocol(Protocol protocol) { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index 2758760d39d..43f53845c71 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; @@ -336,7 +337,10 @@ private String getCacheKey(final Invoker originInvoker) { @Override @SuppressWarnings("unchecked") public Invoker refer(Class type, URL url) throws RpcException { - url = url.setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY)).removeParameter(REGISTRY_KEY); + url = new URLBuilder(url) + .setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY)) + .removeParameter(REGISTRY_KEY) + .build(); Registry registry = registryFactory.getRegistry(url); if (RegistryService.class.equals(type)) { return proxyFactory.getInvoker((T) registry, type, url); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java index 0f5dffc0f4c..70f4fb23a1e 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.registry.Registry; @@ -82,9 +83,11 @@ public static void destroyAll() { @Override public Registry getRegistry(URL url) { - url = url.setPath(RegistryService.class.getName()) + url = new URLBuilder(url) + .setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) - .removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY); + .removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY) + .build(); String key = url.toServiceStringWithoutResolving(); // Lock the registry access process to ensure a single instance of the registry LOCK.lock(); diff --git a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java index 27d90ed57c9..c77d65c1265 100644 --- a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -47,7 +48,8 @@ public class DubboRegistryFactory extends AbstractRegistryFactory { private Cluster cluster; private static URL getRegistryURL(URL url) { - return url.setPath(RegistryService.class.getName()) + return new URLBuilder(url) + .setPath(RegistryService.class.getName()) .removeParameter(Constants.EXPORT_KEY).removeParameter(Constants.REFER_KEY) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .addParameter(Constants.CLUSTER_STICKY_KEY, "true") @@ -61,7 +63,8 @@ private static URL getRegistryURL(URL url) { //.addParameter(Constants.STUB_EVENT_KEY, Boolean.TRUE.toString()) //for event dispatch //.addParameter(Constants.ON_DISCONNECT_KEY, "disconnect") .addParameter("subscribe.1.callback", "true") - .addParameter("unsubscribe.1.callback", "false"); + .addParameter("unsubscribe.1.callback", "false") + .build(); } public void setProtocol(Protocol protocol) { diff --git a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java index 5e4f89917e5..765a9bdd821 100644 --- a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java +++ b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java @@ -19,6 +19,7 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ArrayUtils; @@ -416,10 +417,12 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< } } if (urls.isEmpty()) { - urls.add(url.setProtocol(Constants.EMPTY_PROTOCOL) + urls.add(new URLBuilder(url) + .setProtocol(Constants.EMPTY_PROTOCOL) .setAddress(Constants.ANYHOST_VALUE) .setPath(toServiceName(key)) - .addParameter(Constants.CATEGORY_KEY, category)); + .addParameter(Constants.CATEGORY_KEY, category) + .build()); } result.addAll(urls); if (logger.isInfoEnabled()) { diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java index 14c3304b622..b08ac81eed5 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.CollectionUtils; @@ -272,7 +273,10 @@ private List toUrlsWithEmpty(URL consumer, String path, List provid if (urls == null || urls.isEmpty()) { int i = path.lastIndexOf(Constants.PATH_SEPARATOR); String category = i < 0 ? path : path.substring(i + 1); - URL empty = consumer.setProtocol(Constants.EMPTY_PROTOCOL).addParameter(Constants.CATEGORY_KEY, category); + URL empty = new URLBuilder(consumer) + .setProtocol(Constants.EMPTY_PROTOCOL) + .addParameter(Constants.CATEGORY_KEY, category) + .build(); urls.add(empty); } return urls; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java index b1f6e7a5469..1d84381d59a 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.logger.Logger; @@ -64,11 +65,12 @@ public T getProxy(Invoker invoker, boolean generic) throws RpcException { public T getProxy(Invoker invoker) throws RpcException { T proxy = proxyFactory.getProxy(invoker); if (GenericService.class != invoker.getInterface()) { - String stub = invoker.getUrl().getParameter(Constants.STUB_KEY, invoker.getUrl().getParameter(Constants.LOCAL_KEY)); + URL url = invoker.getUrl(); + String stub = url.getParameter(Constants.STUB_KEY, url.getParameter(Constants.LOCAL_KEY)); if (ConfigUtils.isNotEmpty(stub)) { Class serviceType = invoker.getInterface(); if (ConfigUtils.isDefault(stub)) { - if (invoker.getUrl().hasParameter(Constants.STUB_KEY)) { + if (url.hasParameter(Constants.STUB_KEY)) { stub = serviceType.getName() + "Stub"; } else { stub = serviceType.getName() + "Local"; @@ -83,12 +85,12 @@ public T getProxy(Invoker invoker) throws RpcException { Constructor constructor = ReflectUtils.findConstructor(stubClass, serviceType); proxy = (T) constructor.newInstance(new Object[]{proxy}); //export stub service - URL url = invoker.getUrl(); + URLBuilder urlBuilder = new URLBuilder(url); if (url.getParameter(Constants.STUB_EVENT_KEY, Constants.DEFAULT_STUB_EVENT)) { - url = url.addParameter(Constants.STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ",")); - url = url.addParameter(Constants.IS_SERVER_KEY, Boolean.FALSE.toString()); + urlBuilder.addParameter(Constants.STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ",")); + urlBuilder.addParameter(Constants.IS_SERVER_KEY, Boolean.FALSE.toString()); try { - export(proxy, (Class) invoker.getInterface(), url); + export(proxy, (Class) invoker.getInterface(), urlBuilder.build()); } catch (Exception e) { LOGGER.error("export a stub service error.", e); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java index f284dd40dd6..1665dedcaaa 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.serialize.support.SerializableClassRegistry; @@ -312,17 +313,19 @@ private void openServer(URL url) { } private ExchangeServer createServer(URL url) { - // send readonly event when server closes, it's enabled by default - url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString()); - // enable heartbeat by default - url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT)); + url = new URLBuilder(url) + // send readonly event when server closes, it's enabled by default + .addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString()) + // enable heartbeat by default + .addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT)) + .addParameter(Constants.CODEC_KEY, DubboCodec.NAME) + .build(); String str = url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_SERVER); if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) { throw new RpcException("Unsupported server type: " + str + ", url: " + url); } - url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME); ExchangeServer server; try { server = Exchangers.bind(url, requestHandler); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java index 7a720480463..481946a5757 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java @@ -19,6 +19,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.Parameters; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.ExchangeClient; @@ -165,12 +166,14 @@ public void startClose() { */ private void replaceWithLazyClient() { // this is a defensive operation to avoid client is closed by accident, the initial state of the client is false - URL lazyUrl = url.addParameter(Constants.LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.FALSE) + URL lazyUrl = new URLBuilder(url) + .addParameter(Constants.LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.FALSE) .addParameter(Constants.RECONNECT_KEY, Boolean.FALSE) .addParameter(Constants.SEND_RECONNECT_KEY, Boolean.TRUE.toString()) .addParameter("warning", Boolean.TRUE.toString()) .addParameter(LazyConnectExchangeClient.REQUEST_WITH_WARNING_KEY, true) - .addParameter("_client_memo", "referencecounthandler.replacewithlazyclient"); + .addParameter("_client_memo", "referencecounthandler.replacewithlazyclient") + .build(); /** * the order of judgment in the if statement cannot be changed. diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index c2f2360c144..3d7a97586fd 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -185,4 +185,9 @@ public void testRegFail() { public void testDefaultPort() { assertThat(protocol.getDefaultPort(), is(80)); } -} \ No newline at end of file + + @Test + public void testGetContextPath() { + new RestProtocol().getContextPath(URL.valueOf("")); + } +} From 0badef838cb4983d3b79ad3b5ef939e03d110114 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Mon, 18 Feb 2019 18:29:30 +0800 Subject: [PATCH 2/5] remove unrelated changes --- .../org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index 3d7a97586fd..6f76e9ce962 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -185,9 +185,4 @@ public void testRegFail() { public void testDefaultPort() { assertThat(protocol.getDefaultPort(), is(80)); } - - @Test - public void testGetContextPath() { - new RestProtocol().getContextPath(URL.valueOf("")); - } } From b0c937911aea350d647bb179ff1cf420df7350d3 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Mon, 18 Feb 2019 19:23:37 +0800 Subject: [PATCH 3/5] replace more with URLBuilder --- .../rpc/cluster/support/ClusterUtilsTest.java | 14 +++++++++----- .../dubbo/monitor/dubbo/DubboMonitorTest.java | 17 +++++++++++------ .../dubbo/monitor/dubbo/StatisticsTest.java | 14 +++++++++----- .../registry/dubbo/SimpleRegistryExporter.java | 8 +++++--- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java index b8f668d6784..859d1a603b0 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java @@ -19,6 +19,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -31,7 +32,8 @@ public void testMergeUrl() throws Exception { .setUsername("username") .setPassword("password"); - providerURL = providerURL.addParameter(Constants.GROUP_KEY, "dubbo") + providerURL = new URLBuilder(providerURL) + .addParameter(Constants.GROUP_KEY, "dubbo") .addParameter(Constants.VERSION_KEY, "1.2.3") .addParameter(Constants.DUBBO_VERSION_KEY, "2.3.7") .addParameter(Constants.THREADPOOL_KEY, "fixed") @@ -45,11 +47,13 @@ public void testMergeUrl() throws Exception { .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY, Integer.MAX_VALUE) .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY, Integer.MAX_VALUE) .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY, Integer.MAX_VALUE) - .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test"); + .addParameter(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY, "test") + .build(); - URL consumerURL = URL.valueOf("dubbo://localhost:55555"); - consumerURL = consumerURL.addParameter(Constants.PID_KEY, "1234"); - consumerURL = consumerURL.addParameter(Constants.THREADPOOL_KEY, "foo"); + URL consumerURL = new URLBuilder(Constants.DUBBO_PROTOCOL, "localhost", 55555) + .addParameter(Constants.PID_KEY, "1234") + .addParameter(Constants.THREADPOOL_KEY, "foo") + .build(); URL url = ClusterUtils.mergeUrl(providerURL, consumerURL.getParameters()); diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java index b92c784a797..039f4e32e6b 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/DubboMonitorTest.java @@ -16,7 +16,9 @@ */ package org.apache.dubbo.monitor.dubbo; +import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorFactory; @@ -91,7 +93,7 @@ public List lookup(URL query) { @Test public void testCount() throws Exception { DubboMonitor monitor = new DubboMonitor(monitorInvoker, monitorService); - URL statistics = new URL("dubbo", "10.20.153.10", 0) + URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -101,7 +103,8 @@ public void testCount() throws Exception { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1); + .addParameter(MonitorService.MAX_CONCURRENT, 1) + .build(); monitor.collect(statistics); monitor.send(); while (lastStatistics == null) { @@ -126,7 +129,7 @@ public void testCount() throws Exception { @Test public void testMonitorFactory() throws Exception { MockMonitorService monitorService = new MockMonitorService(); - URL statistics = new URL("dubbo", "10.20.153.10", 0) + URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -136,7 +139,8 @@ public void testMonitorFactory() throws Exception { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1); + .addParameter(MonitorService.MAX_CONCURRENT, 1) + .build(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); @@ -187,7 +191,7 @@ public void testAvailable() { @Test public void testSum() { - URL statistics = new URL("dubbo", "10.20.153.11", 0) + URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.11", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -197,7 +201,8 @@ public void testSum() { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1); + .addParameter(MonitorService.MAX_CONCURRENT, 1) + .build(); Invoker invoker = mock(Invoker.class); MonitorService monitorService = mock(MonitorService.class); diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java index 7f33369f99f..e2bdc897718 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/StatisticsTest.java @@ -16,7 +16,9 @@ */ package org.apache.dubbo.monitor.dubbo; +import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.monitor.MonitorService; import org.hamcrest.MatcherAssert; @@ -30,7 +32,7 @@ public class StatisticsTest { @Test public void testEquals() { - URL statistics = new URL("dubbo", "10.20.153.10", 0) + URL statistics = new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -40,7 +42,8 @@ public void testEquals() { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1); + .addParameter(MonitorService.MAX_CONCURRENT, 1) + .build(); Statistics statistics1 = new Statistics(statistics); Statistics statistics2 = new Statistics(statistics); @@ -71,7 +74,7 @@ public void testToString() { statistics.setService("MemberService"); assertThat(statistics.toString(), is("dubbo://10.20.153.10")); - Statistics statisticsWithDetailInfo = new Statistics(new URL("dubbo", "10.20.153.10", 0) + Statistics statisticsWithDetailInfo = new Statistics(new URLBuilder(Constants.DUBBO_PROTOCOL, "10.20.153.10", 0) .addParameter(MonitorService.APPLICATION, "morgan") .addParameter(MonitorService.INTERFACE, "MemberService") .addParameter(MonitorService.METHOD, "findPerson") @@ -82,7 +85,8 @@ public void testToString() { .addParameter(MonitorService.ELAPSED, 3) .addParameter(MonitorService.MAX_ELAPSED, 3) .addParameter(MonitorService.CONCURRENT, 1) - .addParameter(MonitorService.MAX_CONCURRENT, 1)); + .addParameter(MonitorService.MAX_CONCURRENT, 1) + .build()); MatcherAssert.assertThat(statisticsWithDetailInfo.getServer(), equalTo(statistics.getServer())); MatcherAssert.assertThat(statisticsWithDetailInfo.getService(), equalTo(statistics.getService())); @@ -91,4 +95,4 @@ public void testToString() { MatcherAssert.assertThat(statisticsWithDetailInfo.getGroup(), equalTo(statistics.getGroup())); MatcherAssert.assertThat(statisticsWithDetailInfo, not(equalTo(statistics))); } -} \ No newline at end of file +} diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java index b30462f7bf0..2b67e634143 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryExporter.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.RegistryService; @@ -53,14 +54,15 @@ public static Exporter export(int port) { public static Exporter export(int port, RegistryService registryService) { return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class, - new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName()) + new URLBuilder(Constants.DUBBO_PROTOCOL, NetUtils.getLocalHost(), port, RegistryService.class.getName()) .setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .addParameter(Constants.CLUSTER_STICKY_KEY, "true") .addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "1000") .addParameter("ondisconnect", "disconnect") .addParameter("subscribe.1.callback", "true") - .addParameter("unsubscribe.1.callback", "false"))); + .addParameter("unsubscribe.1.callback", "false") + .build())); } -} \ No newline at end of file +} From 43482e498a6bd61bf1056d698c72f435264c5d88 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Tue, 26 Feb 2019 10:43:44 +0800 Subject: [PATCH 4/5] fix ci failure --- .../dubbo/rpc/cluster/router/file/FileRouterFactory.java | 2 +- .../apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java | 2 +- .../java/org/apache/dubbo/config/AbstractInterfaceConfig.java | 4 ++-- .../src/main/java/org/apache/dubbo/config/ServiceConfig.java | 2 +- .../dubbo/metadata/integration/MetadataReportService.java | 2 +- .../org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java | 2 +- .../apache/dubbo/registry/integration/RegistryProtocol.java | 2 +- .../dubbo/registry/support/AbstractRegistryFactory.java | 2 +- .../org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java | 2 +- .../java/org/apache/dubbo/registry/redis/RedisRegistry.java | 2 +- .../apache/dubbo/registry/zookeeper/ZookeeperRegistry.java | 2 +- .../dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java | 2 +- .../org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java | 2 +- .../rpc/protocol/dubbo/ReferenceCountExchangeClient.java | 2 +- 14 files changed, 15 insertions(+), 15 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java index 075dd5cbc63..1dd446cc187 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/file/FileRouterFactory.java @@ -56,7 +56,7 @@ public Router getRouter(URL url) { // FIXME: this code looks useless boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false); - URL script = new URLBuilder(url) + URL script = URLBuilder.from(url) .setProtocol(protocol) .addParameter(Constants.TYPE_KEY, type) .addParameter(Constants.RUNTIME_KEY, runtime) diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java index 859d1a603b0..c010a157dcc 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ClusterUtilsTest.java @@ -32,7 +32,7 @@ public void testMergeUrl() throws Exception { .setUsername("username") .setPassword("password"); - providerURL = new URLBuilder(providerURL) + providerURL = URLBuilder.from(providerURL) .addParameter(Constants.GROUP_KEY, "dubbo") .addParameter(Constants.VERSION_KEY, "1.2.3") .addParameter(Constants.DUBBO_VERSION_KEY, "2.3.7") diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index fa696cf0bce..c149e2e16fa 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -305,7 +305,7 @@ protected List loadRegistries(boolean provider) { List urls = UrlUtils.parseURLs(address, map); for (URL url : urls) { - url = new URLBuilder(url) + url = URLBuilder.from(url) .addParameter(Constants.REGISTRY_KEY, url.getProtocol()) .setProtocol(Constants.REGISTRY_PROTOCOL) .build(); @@ -355,7 +355,7 @@ protected URL loadMonitor(URL registryURL) { } return UrlUtils.parseURL(address, map); } else if (Constants.REGISTRY_PROTOCOL.equals(monitor.getProtocol()) && registryURL != null) { - return new URLBuilder(registryURL) + return URLBuilder.from(registryURL) .setProtocol(Constants.DUBBO_PROTOCOL) .addParameter(Constants.PROTOCOL_KEY, Constants.REGISTRY_PROTOCOL) .addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)) diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index 9e687e8126c..45435651a9b 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -571,7 +571,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r @SuppressWarnings({"unchecked", "rawtypes"}) private void exportLocal(URL url) { if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) { - URL local = new URLBuilder(url) + URL local = URLBuilder.from(url) .setProtocol(Constants.LOCAL_PROTOCOL) .setHost(LOCALHOST_VALUE) .setPort(0) diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java index c93498cb5bb..f2428f9fe9e 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java @@ -49,7 +49,7 @@ public class MetadataReportService { MetadataReportService(URL metadataReportURL) { if (Constants.METADATA_REPORT_KEY.equals(metadataReportURL.getProtocol())) { String protocol = metadataReportURL.getParameter(Constants.METADATA_REPORT_KEY, Constants.DEFAULT_DIRECTORY); - metadataReportURL = new URLBuilder(metadataReportURL) + metadataReportURL = URLBuilder.from(metadataReportURL) .setProtocol(protocol) .removeParameter(Constants.METADATA_REPORT_KEY) .build(); diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java index 9d001fc67b4..847150c96f5 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java @@ -50,7 +50,7 @@ public void setProxyFactory(ProxyFactory proxyFactory) { @Override protected Monitor createMonitor(URL url) { - URLBuilder urlBuilder = new URLBuilder(url); + URLBuilder urlBuilder = URLBuilder.from(url); urlBuilder.setProtocol(url.getParameter(PROTOCOL_KEY, DUBBO_PROTOCOL)); if (StringUtils.isEmpty(url.getPath())) { urlBuilder.setPath(MonitorService.class.getName()); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index 43f53845c71..b7e4a6a5341 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -337,7 +337,7 @@ private String getCacheKey(final Invoker originInvoker) { @Override @SuppressWarnings("unchecked") public Invoker refer(Class type, URL url) throws RpcException { - url = new URLBuilder(url) + url = URLBuilder.from(url) .setProtocol(url.getParameter(REGISTRY_KEY, DEFAULT_REGISTRY)) .removeParameter(REGISTRY_KEY) .build(); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java index 70f4fb23a1e..4ef392ef42a 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java @@ -83,7 +83,7 @@ public static void destroyAll() { @Override public Registry getRegistry(URL url) { - url = new URLBuilder(url) + url = URLBuilder.from(url) .setPath(RegistryService.class.getName()) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) .removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY) diff --git a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java index c77d65c1265..ee331c83186 100644 --- a/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistryFactory.java @@ -48,7 +48,7 @@ public class DubboRegistryFactory extends AbstractRegistryFactory { private Cluster cluster; private static URL getRegistryURL(URL url) { - return new URLBuilder(url) + return URLBuilder.from(url) .setPath(RegistryService.class.getName()) .removeParameter(Constants.EXPORT_KEY).removeParameter(Constants.REFER_KEY) .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) diff --git a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java index 765a9bdd821..b693842eb91 100644 --- a/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java +++ b/dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java @@ -417,7 +417,7 @@ private void doNotify(Jedis jedis, Collection keys, URL url, Collection< } } if (urls.isEmpty()) { - urls.add(new URLBuilder(url) + urls.add(URLBuilder.from(url) .setProtocol(Constants.EMPTY_PROTOCOL) .setAddress(Constants.ANYHOST_VALUE) .setPath(toServiceName(key)) diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java index b08ac81eed5..93f584aac22 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java @@ -273,7 +273,7 @@ private List toUrlsWithEmpty(URL consumer, String path, List provid if (urls == null || urls.isEmpty()) { int i = path.lastIndexOf(Constants.PATH_SEPARATOR); String category = i < 0 ? path : path.substring(i + 1); - URL empty = new URLBuilder(consumer) + URL empty = URLBuilder.from(consumer) .setProtocol(Constants.EMPTY_PROTOCOL) .addParameter(Constants.CATEGORY_KEY, category) .build(); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java index 1d84381d59a..e4f7930d505 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/wrapper/StubProxyFactoryWrapper.java @@ -85,7 +85,7 @@ public T getProxy(Invoker invoker) throws RpcException { Constructor constructor = ReflectUtils.findConstructor(stubClass, serviceType); proxy = (T) constructor.newInstance(new Object[]{proxy}); //export stub service - URLBuilder urlBuilder = new URLBuilder(url); + URLBuilder urlBuilder = URLBuilder.from(url); if (url.getParameter(Constants.STUB_EVENT_KEY, Constants.DEFAULT_STUB_EVENT)) { urlBuilder.addParameter(Constants.STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ",")); urlBuilder.addParameter(Constants.IS_SERVER_KEY, Boolean.FALSE.toString()); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java index 1665dedcaaa..7678977a47a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -313,7 +313,7 @@ private void openServer(URL url) { } private ExchangeServer createServer(URL url) { - url = new URLBuilder(url) + url = URLBuilder.from(url) // send readonly event when server closes, it's enabled by default .addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString()) // enable heartbeat by default diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java index 481946a5757..faafe082852 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java @@ -166,7 +166,7 @@ public void startClose() { */ private void replaceWithLazyClient() { // this is a defensive operation to avoid client is closed by accident, the initial state of the client is false - URL lazyUrl = new URLBuilder(url) + URL lazyUrl = URLBuilder.from(url) .addParameter(Constants.LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.FALSE) .addParameter(Constants.RECONNECT_KEY, Boolean.FALSE) .addParameter(Constants.SEND_RECONNECT_KEY, Boolean.TRUE.toString()) From 92cebfd4a1c38f580de97b395ec7babe253a442a Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Mon, 4 Mar 2019 18:08:31 +0800 Subject: [PATCH 5/5] remove unnecessary comment --- .../src/main/java/org/apache/dubbo/common/URLBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java index 49089e9fc0e..c938737811d 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java @@ -55,7 +55,7 @@ public URLBuilder(String protocol, String host, int port) { this(protocol, null, null, host, port, null, null); } - public URLBuilder(String protocol, String host, int port, String[] pairs) { // varargs ... conflict with the following path argument, use array instead. + public URLBuilder(String protocol, String host, int port, String[] pairs) { this(protocol, null, null, host, port, null, CollectionUtils.toStringMap(pairs)); }