Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-DynamicConfig] compatible configurators url with type JsonArray in ConfigCenter #6216

Merged
merged 2 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>

<!-- Temporarily add this part to exclude transitive dependency -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.dubbo.rpc.cluster.configurator.parser;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONValidator;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
Expand All @@ -41,6 +43,11 @@
public class ConfigParser {

public static List<URL> parseConfigurators(String rawConfig) {
// compatible url JsonArray, such as [ "override://xxx", "override://xxx" ]
if (isJsonArray(rawConfig)) {
return parseJsonArray(rawConfig);
}

List<URL> urls = new ArrayList<>();
ConfiguratorConfig configuratorConfig = parseObject(rawConfig);

Expand All @@ -56,6 +63,15 @@ public static List<URL> parseConfigurators(String rawConfig) {
return urls;
}

private static List<URL> parseJsonArray(String rawConfig) {
List<URL> urls = new ArrayList<>();
List<String> list = JSON.parseArray(rawConfig, String.class);
if (!CollectionUtils.isEmpty(list)) {
list.forEach(u -> urls.add(URL.valueOf(u)));
}
return urls;
}

private static <T> T parseObject(String rawConfig) {
Constructor constructor = new Constructor(ConfiguratorConfig.class);
TypeDescription itemDescription = new TypeDescription(ConfiguratorConfig.class);
Expand Down Expand Up @@ -200,4 +216,14 @@ private static List<String> parseAddresses(ConfigItem item) {
}
return addresses;
}

private static boolean isJsonArray(String rawConfig) {
try {
JSONValidator validator = JSONValidator.from(rawConfig);
return validator.validate() && validator.getType() == JSONValidator.Type.Array;
} catch (Exception e) {
// ignore exception and return false
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,20 @@ public void parseConsumerSpecificProvidersTest() throws IOException {
}
}

@Test
public void parseURLJsonArrayCompatible() {

String configData = "[\"override://0.0.0.0/com.xx.Service?category=configurators&timeout=6666&disabled=true&dynamic=false&enabled=true&group=dubbo&priority=1&version=1.0\" ]";

List<URL> urls = ConfigParser.parseConfigurators(configData);

Assertions.assertNotNull(urls);
Assertions.assertEquals(1, urls.size());
URL url = urls.get(0);

Assertions.assertEquals("0.0.0.0", url.getAddress());
Assertions.assertEquals("com.xx.Service", url.getServiceInterface());
Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0));
}

}