You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by ji...@apache.org on 2021/04/14 10:48:03 UTC

[rocketmq] branch develop updated: [ISSUE #2803] Fix the endpoint cannot get instanceId without http (#2804)

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

jinrongtong pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 1fbccd1  [ISSUE #2803] Fix the endpoint cannot get instanceId without http (#2804)
1fbccd1 is described below

commit 1fbccd14e03d929f64947fb75d4ab7d943603adb
Author: panzhi <pa...@qq.com>
AuthorDate: Wed Apr 14 18:47:41 2021 +0800

    [ISSUE #2803] Fix the endpoint cannot get instanceId without http (#2804)
    
    * fix the endpoint cannot get instanceId without http
    
    * fix the endpoint cannot get instanceId without http
    
    * add unit test
    
    * add unit test
    
    * add unit test
    
    Co-authored-by: panzhi33 <wb...@alibaba-inc.com>
---
 .../org/apache/rocketmq/client/ClientConfig.java   |  4 +-
 .../common/utils/NameServerAddressUtils.java       | 12 +++--
 .../common/utils/NameServerAddressUtilsTest.java   | 59 ++++++++++++++++++++++
 3 files changed, 70 insertions(+), 5 deletions(-)

diff --git a/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java b/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java
index bcd3f9b..4e56d4b 100644
--- a/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java
+++ b/client/src/main/java/org/apache/rocketmq/client/ClientConfig.java
@@ -178,8 +178,8 @@ public class ClientConfig {
     }
 
     public String getNamesrvAddr() {
-        if (StringUtils.isNotEmpty(namesrvAddr) && NameServerAddressUtils.NAMESRV_ENDPOINT_PATTERN.matcher(namesrvAddr.trim()).matches()) {
-            return namesrvAddr.substring(NameServerAddressUtils.ENDPOINT_PREFIX.length());
+        if (StringUtils.isNotEmpty(namesrvAddr) && NameServerAddressUtils.validateInstanceEndpoint(namesrvAddr.trim())) {
+            return NameServerAddressUtils.getNameSrvAddrFromNamesrvEndpoint(namesrvAddr);
         }
         return namesrvAddr;
     }
diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java b/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java
index 6aaf3a2..955e39d 100644
--- a/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java
+++ b/common/src/main/java/org/apache/rocketmq/common/utils/NameServerAddressUtils.java
@@ -19,8 +19,7 @@ import org.apache.rocketmq.common.MixAll;
 public class NameServerAddressUtils {
     public static final String INSTANCE_PREFIX = "MQ_INST_";
     public static final String INSTANCE_REGEX = INSTANCE_PREFIX + "\\w+_\\w+";
-    public static final String ENDPOINT_PREFIX = "http://";
-    public static final Pattern NAMESRV_ENDPOINT_PATTERN = Pattern.compile("^" + ENDPOINT_PREFIX + ".*");
+    public static final String ENDPOINT_PREFIX = "(\\w+://|)";
     public static final Pattern INST_ENDPOINT_PATTERN = Pattern.compile("^" + ENDPOINT_PREFIX + INSTANCE_REGEX + "\\..*");
 
     public static String getNameServerAddresses() {
@@ -35,6 +34,13 @@ public class NameServerAddressUtils {
         if (StringUtils.isEmpty(endpoint)) {
             return null;
         }
-        return endpoint.substring(ENDPOINT_PREFIX.length(), endpoint.indexOf('.'));
+        return endpoint.substring(endpoint.lastIndexOf("/")+1, endpoint.indexOf('.'));
+    }
+
+    public static String getNameSrvAddrFromNamesrvEndpoint(String nameSrvEndpoint) {
+        if (StringUtils.isEmpty(nameSrvEndpoint)) {
+            return null;
+        }
+        return nameSrvEndpoint.substring(nameSrvEndpoint.lastIndexOf('/') + 1);
     }
 }
diff --git a/common/src/test/java/org/apache/rocketmq/common/utils/NameServerAddressUtilsTest.java b/common/src/test/java/org/apache/rocketmq/common/utils/NameServerAddressUtilsTest.java
new file mode 100644
index 0000000..38cffdb
--- /dev/null
+++ b/common/src/test/java/org/apache/rocketmq/common/utils/NameServerAddressUtilsTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.rocketmq.common.utils;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class NameServerAddressUtilsTest {
+
+    private static String endpoint1 = "http://127.0.0.1:9876";
+    private static String endpoint2 = "127.0.0.1:9876";
+    private static String endpoint3
+        = "http://MQ_INST_123456789_BXXUzaee.xxx:80";
+    private static String endpoint4 = "MQ_INST_123456789_BXXUzaee.xxx:80";
+
+    @Test
+    public void testValidateInstanceEndpoint() {
+        assertThat(NameServerAddressUtils.validateInstanceEndpoint(endpoint1)).isEqualTo(false);
+        assertThat(NameServerAddressUtils.validateInstanceEndpoint(endpoint2)).isEqualTo(false);
+        assertThat(NameServerAddressUtils.validateInstanceEndpoint(endpoint3)).isEqualTo(true);
+        assertThat(NameServerAddressUtils.validateInstanceEndpoint(endpoint4)).isEqualTo(true);
+    }
+
+    @Test
+    public void testParseInstanceIdFromEndpoint() {
+        assertThat(NameServerAddressUtils.parseInstanceIdFromEndpoint(endpoint3)).isEqualTo(
+            "MQ_INST_123456789_BXXUzaee");
+        assertThat(NameServerAddressUtils.parseInstanceIdFromEndpoint(endpoint4)).isEqualTo(
+            "MQ_INST_123456789_BXXUzaee");
+    }
+
+    @Test
+    public void testGetNameSrvAddrFromNamesrvEndpoint() {
+        assertThat(NameServerAddressUtils.getNameSrvAddrFromNamesrvEndpoint(endpoint1))
+            .isEqualTo("127.0.0.1:9876");
+        assertThat(NameServerAddressUtils.getNameSrvAddrFromNamesrvEndpoint(endpoint2))
+            .isEqualTo("127.0.0.1:9876");
+        assertThat(NameServerAddressUtils.getNameSrvAddrFromNamesrvEndpoint(endpoint3))
+            .isEqualTo("MQ_INST_123456789_BXXUzaee.xxx:80");
+        assertThat(NameServerAddressUtils.getNameSrvAddrFromNamesrvEndpoint(endpoint4))
+            .isEqualTo("MQ_INST_123456789_BXXUzaee.xxx:80");
+    }
+}