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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 1a4485bdf [SCB-2710]servicecomb.governance apiPath match problem 
(#3436)
1a4485bdf is described below

commit 1a4485bdf1989044e9fdeb9946d3c67306337aa1
Author: liubao68 <[email protected]>
AuthorDate: Sat Oct 29 18:45:08 2022 +0800

    [SCB-2710]servicecomb.governance apiPath match problem (#3436)
---
 .../common/rest/definition/RestOperationMeta.java  |  7 ++-
 .../servicecomb/core/governance/MatchType.java     | 19 +++++++-
 .../demo/zeroconfig/client/GovernanceEndpoint.java |  7 +++
 .../src/main/resources/application.yml             |  8 +++
 .../server/GovernanceNoPrefixEndpoint.java         | 57 ++++++++++++++++++++++
 .../demo/zeroconfig/tests/GovernanceTest.java      |  5 ++
 6 files changed, 100 insertions(+), 3 deletions(-)

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
index d8e91d8ee..45a2e2d06 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
@@ -165,7 +165,12 @@ public class RestOperationMeta {
     this.operationMeta = operationMeta;
   }
 
-  // 输出b/c/形式的url
+  /**
+   * Concat the two paths to an absolute path, end of '/' added.
+   *
+   * e.g. "/" + "/ope" = /ope/
+   * e.g. "/prefix" + "/ope" = /prefix/ope/
+   */
   private String concatPath(String basePath, String operationPath) {
     return ("/" + nonNullify(basePath) + "/" + nonNullify(operationPath) + "/")
         .replaceAll("/{2,}", "/");
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java 
b/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java
index a02a61bae..661931531 100644
--- a/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java
+++ b/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java
@@ -34,8 +34,8 @@ public final class MatchType {
 
     if 
(MatchType.REST.equalsIgnoreCase(invocation.getOperationMeta().getConfig().getGovernanceMatchType()))
 {
       if (invocation.isConsumer()) {
-        request.setUri(
-            invocation.getSchemaMeta().getSwagger().getBasePath() + 
invocation.getOperationMeta().getOperationPath());
+        request.setUri(concatAbsolutePath(
+            invocation.getSchemaMeta().getSwagger().getBasePath(), 
invocation.getOperationMeta().getOperationPath()));
         request.setMethod(invocation.getOperationMeta().getHttpMethod());
         request.setHeaders(getHeaderMap(invocation, true));
         return request;
@@ -63,6 +63,21 @@ public final class MatchType {
     return request;
   }
 
+  /**
+   * Concat the two paths to an absolute path, without end of '/'.
+   *
+   * e.g. "/" + "/ope" = /ope
+   * e.g. "/prefix" + "/ope" = /prefix/ope
+   */
+  private static String concatAbsolutePath(String basePath, String 
operationPath) {
+    return ("/" + nonNullify(basePath) + "/" + nonNullify(operationPath))
+        .replaceAll("/{2,}", "/");
+  }
+
+  private static String nonNullify(String path) {
+    return path == null ? "" : path;
+  }
+
   private static Map<String, String> getHeaderMap(Invocation invocation, 
boolean fromContext) {
     Map<String, String> headers = new HashMap<>();
     if (fromContext) {
diff --git 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/GovernanceEndpoint.java
 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/GovernanceEndpoint.java
index 3c4a6448a..9bb256d25 100644
--- 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/GovernanceEndpoint.java
+++ 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/GovernanceEndpoint.java
@@ -46,6 +46,13 @@ public class GovernanceEndpoint {
     return restTemplate.getForObject(SERVER + "/governance/hello", 
String.class);
   }
 
+  @GetMapping("/noPrefixRetry")
+  public String noPrefixRetry(@RequestParam(name = "invocationID") String 
invocationID) {
+    return restTemplate
+        .getForObject(SERVER + "/noPrefixRetry?invocationID={1}", String.class,
+            invocationID);
+  }
+
   @GetMapping("/retry")
   public String retry(@RequestParam(name = "invocationID") String 
invocationID) {
     return restTemplate
diff --git 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
index f89c65460..ca45a4a2f 100644
--- 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
+++ 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
@@ -61,6 +61,10 @@ servicecomb:
       matches:
         - apiPath:
             exact: "/governance/retry"
+    demo-retry-no-prefex: |
+      matches:
+        - apiPath:
+            exact: "/noPrefixRetry"
     demo-retry-rpc: |
       matches:
         - apiPath:
@@ -87,6 +91,10 @@ servicecomb:
       maxAttempts: 3
       ## services is optional in configuration file
       services: demo-zeroconfig-schemadiscovery-registry-client
+    demo-retry-no-prefex: |
+      maxAttempts: 3
+      ## services is optional in configuration file
+      services: demo-zeroconfig-schemadiscovery-registry-client
     demo-retry-rpc: |
       maxAttempts: 3
       ## services is optional in configuration file
diff --git 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-server/src/main/java/org/apache/servicecomb/demo/zeroconfig/server/GovernanceNoPrefixEndpoint.java
 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-server/src/main/java/org/apache/servicecomb/demo/zeroconfig/server/GovernanceNoPrefixEndpoint.java
new file mode 100644
index 000000000..4f08860c0
--- /dev/null
+++ 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-server/src/main/java/org/apache/servicecomb/demo/zeroconfig/server/GovernanceNoPrefixEndpoint.java
@@ -0,0 +1,57 @@
+/*
+ * 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.servicecomb.demo.zeroconfig.server;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+
+@RestSchema(schemaId = "GovernanceNoPrefixEndpoint")
+@RequestMapping("/")
+public class GovernanceNoPrefixEndpoint {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(GovernanceNoPrefixEndpoint.class);
+
+  private Map<String, Integer> retryTimes = new HashMap<>();
+
+  @GetMapping("/noPrefixRetry")
+  @ApiResponses({
+      @ApiResponse(code = 200, response = String.class, message = ""),
+      @ApiResponse(code = 502, response = String.class, message = "")})
+  public String noPrefixRetry(@RequestParam(name = "invocationID") String 
invocationID) {
+    LOGGER.info("invoke service: {}", invocationID);
+    retryTimes.putIfAbsent(invocationID, 0);
+    retryTimes.put(invocationID, retryTimes.get(invocationID) + 1);
+
+    int retry = retryTimes.get(invocationID);
+
+    if (retry == 3) {
+      return "try times: " + retry;
+    }
+    throw new InvocationException(502, "retry", "retry");
+  }
+}
diff --git 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/GovernanceTest.java
 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/GovernanceTest.java
index d72faa84a..83fd05f86 100644
--- 
a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/GovernanceTest.java
+++ 
b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/GovernanceTest.java
@@ -41,9 +41,14 @@ public class GovernanceTest implements CategorizedTestCase {
     testRateLimitingRest();
     testRateLimitingRpc();
     testRetryRest();
+    testRetryRestNoPrefix();
     testRetryRpc();
   }
 
+  private void testRetryRestNoPrefix() {
+    testRetry("/noPrefixRetry");
+  }
+
   private void testRetryRest() {
     testRetry("/retry");
   }

Reply via email to