From 3c3aebd5e6d8e45dfe2e8a01a5f2cb2dcf4b4863 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Mon, 12 Jul 2021 01:01:33 +0800 Subject: [PATCH 1/2] 1.metadata-report-config support protocol and port. 2.make ConfigCenterConfig setAddress be same with RegistryConfig. --- .../dubbo/config/ConfigCenterConfig.java | 20 ++++-- .../dubbo/config/MetadataReportConfig.java | 63 +++++++++++++++++-- .../apache/dubbo/config/RegistryConfig.java | 5 -- .../src/main/resources/META-INF/dubbo.xsd | 10 +++ .../main/resources/spring/dubbo-provider.xml | 2 +- 5 files changed, 84 insertions(+), 16 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java index b0d5b5ae6b4..2ca2f7faf2f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java @@ -32,6 +32,8 @@ import static org.apache.dubbo.common.constants.CommonConstants.CONFIG_ENABLE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; +import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY; +import static org.apache.dubbo.common.utils.PojoUtils.updatePropertyIfAbsent; import static org.apache.dubbo.config.Constants.CONFIG_APP_CONFIGFILE_KEY; import static org.apache.dubbo.config.Constants.ZOOKEEPER_PROTOCOL; @@ -147,12 +149,18 @@ public void setAddress(String address) { if (address != null) { try { URL url = URL.valueOf(address); - setUsername(url.getUsername()); - setPassword(url.getPassword()); - updateIdIfAbsent(url.getProtocol()); - updateProtocolIfAbsent(url.getProtocol()); - updatePortIfAbsent(url.getPort()); - updateParameters(url.getParameters()); + + // Refactor since 2.7.8 + updatePropertyIfAbsent(this::getUsername, this::setUsername, url.getUsername()); + updatePropertyIfAbsent(this::getPassword, this::setPassword, url.getPassword()); + updatePropertyIfAbsent(this::getProtocol, this::setProtocol, url.getProtocol()); + updatePropertyIfAbsent(this::getPort, this::setPort, url.getPort()); + + Map params = url.getParameters(); + if (CollectionUtils.isNotEmptyMap(params)) { + params.remove(BACKUP_KEY); + } + updateParameters(params); } catch (Exception ignored) { } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java index 2a1272dc0e1..24b622bdc66 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/MetadataReportConfig.java @@ -17,6 +17,7 @@ package org.apache.dubbo.config; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; @@ -25,6 +26,8 @@ import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; import static org.apache.dubbo.common.constants.CommonConstants.PROPERTIES_CHAR_SEPARATOR; +import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY; +import static org.apache.dubbo.common.utils.PojoUtils.updatePropertyIfAbsent; import static org.apache.dubbo.common.utils.StringUtils.isEmpty; /** @@ -41,16 +44,23 @@ public class MetadataReportConfig extends AbstractConfig { private static final String PREFIX_TAG = StringUtils.camelToSplitName( MetadataReportConfig.class.getSimpleName().substring(0, MetadataReportConfig.class.getSimpleName().length() - 6), PROPERTIES_CHAR_SEPARATOR); - // Register center address + private String protocol; + + // metadata center address private String address; - // Username to login register center + /** + * Default port for metadata center + */ + private Integer port; + + // Username to login metadata center private String username; - // Password to login register center + // Password to login metadata center private String password; - // Request timeout in milliseconds for register center + // Request timeout in milliseconds for metadata center private Integer timeout; /** @@ -118,6 +128,14 @@ public URL toUrl() throws IllegalArgumentException { } + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + @Parameter(excluded = true) public String getAddress() { return address; @@ -125,6 +143,32 @@ public String getAddress() { public void setAddress(String address) { this.address = address; + if (address != null) { + try { + URL url = URL.valueOf(address); + + // Refactor since 2.7.8 + updatePropertyIfAbsent(this::getUsername, this::setUsername, url.getUsername()); + updatePropertyIfAbsent(this::getPassword, this::setPassword, url.getPassword()); + updatePropertyIfAbsent(this::getProtocol, this::setProtocol, url.getProtocol()); + updatePropertyIfAbsent(this::getPort, this::setPort, url.getPort()); + + Map params = url.getParameters(); + if (CollectionUtils.isNotEmptyMap(params)) { + params.remove(BACKUP_KEY); + } + updateParameters(params); + } catch (Exception ignored) { + } + } + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; } public String getUsername() { @@ -238,4 +282,15 @@ public String getFile() { public void setFile(String file) { this.file = file; } + + public void updateParameters(Map parameters) { + if (CollectionUtils.isEmptyMap(parameters)) { + return; + } + if (this.parameters == null) { + this.parameters = parameters; + } else { + this.parameters.putAll(parameters); + } + } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java index 3d012408790..2de4970e5f6 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java @@ -223,11 +223,6 @@ public void setAddress(String address) { updatePropertyIfAbsent(this::getProtocol, this::setProtocol, url.getProtocol()); updatePropertyIfAbsent(this::getPort, this::setPort, url.getPort()); -// setUsername(url.getUsername()); -// setPassword(url.getPassword()); -// updateIdIfAbsent(url.getProtocol()); -// updateProtocolIfAbsent(url.getProtocol()); -// updatePortIfAbsent(url.getPort()); Map params = url.getParameters(); if (CollectionUtils.isNotEmptyMap(params)) { params.remove(BACKUP_KEY); diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd index a1f68383047..9df1f9ae3e3 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd @@ -634,11 +634,21 @@ + + + + + + + + + + diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml index a0b3a5f04cf..24b4e7cb4e8 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml @@ -26,7 +26,7 @@ - + From a32e71c62d3ff6b1e24a15836044e30e8f47c96b Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Mon, 12 Jul 2021 01:05:45 +0800 Subject: [PATCH 2/2] revert provider-demo-xml xml config. --- .../src/main/resources/spring/dubbo-provider.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml index 24b4e7cb4e8..a0b3a5f04cf 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/resources/spring/dubbo-provider.xml @@ -26,7 +26,7 @@ - +