You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/07/13 14:48:49 UTC

[dubbo] branch 3.0 updated: [3.0] p2p invoke support wildcard url match. (#8247)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new b4d72f9  [3.0] p2p invoke support wildcard url match. (#8247)
b4d72f9 is described below

commit b4d72f9ef0394e1421834642e6f327c09199cbeb
Author: ZakiFan <34...@qq.com>
AuthorDate: Tue Jul 13 22:48:40 2021 +0800

    [3.0] p2p invoke support wildcard url match. (#8247)
    
    * p2p invoke support wildcard url match.
    
    * add license
    
    * add more testcases.
    
    Co-authored-by: Stefan <zj...@trip.com>
---
 .../apache/dubbo/common/utils/RegexProperties.java | 60 ++++++++++++++++++++++
 .../apache/dubbo/config/ReferenceConfigBase.java   |  3 +-
 .../dubbo/common/utils/RegexPropertiesTest.java    | 38 ++++++++++++++
 3 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/RegexProperties.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/RegexProperties.java
new file mode 100644
index 0000000..cb7f24c
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/RegexProperties.java
@@ -0,0 +1,60 @@
+/*
+ * 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.utils;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * Regex matching of keys is supported.
+ */
+public class RegexProperties extends Properties {
+
+    @Override
+    public String getProperty(String key) {
+        String value = super.getProperty(key);
+        if (value != null) {
+            return value;
+        }
+
+        // Sort the keys to solve the problem of matching priority.
+        List<String> sortedKeyList = keySet()
+                .stream()
+                .map(k -> (String) k)
+                .sorted(Comparator.reverseOrder())
+                .collect(Collectors.toList());
+
+        String keyPattern = sortedKeyList
+                .stream()
+                .filter(k -> {
+                    String matchingKey = k;
+                    if(matchingKey.startsWith(CommonConstants.ANY_VALUE)){
+                        matchingKey = CommonConstants.HIDE_KEY_PREFIX + matchingKey;
+                    }
+                    return Pattern.matches(matchingKey, key);
+                })
+                .findFirst()
+                .orElse(null);
+
+        return keyPattern == null ? null : super.getProperty(keyPattern);
+    }
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java b/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java
index d9e41d3..ac0f7ce 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.config;
 
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.utils.ClassUtils;
+import org.apache.dubbo.common.utils.RegexProperties;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.config.annotation.Reference;
 import org.apache.dubbo.config.support.Parameter;
@@ -263,7 +264,7 @@ public abstract class ReferenceConfigBase<T> extends AbstractReferenceConfig {
                 }
             }
             if (resolveFile != null && resolveFile.length() > 0) {
-                Properties properties = new Properties();
+                Properties properties = new RegexProperties();
                 try (FileInputStream fis = new FileInputStream(new File(resolveFile))) {
                     properties.load(fis);
                 } catch (IOException e) {
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/RegexPropertiesTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/RegexPropertiesTest.java
new file mode 100644
index 0000000..395b032
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/RegexPropertiesTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.utils;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class RegexPropertiesTest {
+    @Test
+    public void testGetProperty(){
+        RegexProperties regexProperties = new RegexProperties();
+        regexProperties.setProperty("org.apache.dubbo.provider.*", "http://localhost:20880");
+        regexProperties.setProperty("org.apache.dubbo.provider.config.*", "http://localhost:30880");
+        regexProperties.setProperty("org.apache.dubbo.provider.config.demo", "http://localhost:40880");
+        regexProperties.setProperty("org.apache.dubbo.consumer.*.demo", "http://localhost:50880");
+        regexProperties.setProperty("*.service", "http://localhost:60880");
+
+        Assertions.assertEquals("http://localhost:20880", regexProperties.getProperty("org.apache.dubbo.provider.cluster"));
+        Assertions.assertEquals("http://localhost:30880", regexProperties.getProperty("org.apache.dubbo.provider.config.cluster"));
+        Assertions.assertEquals("http://localhost:40880", regexProperties.getProperty("org.apache.dubbo.provider.config.demo"));
+        Assertions.assertEquals("http://localhost:50880", regexProperties.getProperty("org.apache.dubbo.consumer.service.demo"));
+        Assertions.assertEquals("http://localhost:60880", regexProperties.getProperty("org.apache.dubbo.service"));
+    }
+}