From 4c552199fbeb6c810f292b80722e2a5915c3357f Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 27 Oct 2020 15:48:47 +0800 Subject: [PATCH] 4.0.3 --- doc/docs/files/10100_提供restful接口.md | 6 ++++-- .../sop/gatewaycommon/gateway/ServerWebExchangeUtil.java | 7 +++---- .../sop/gatewaycommon/gateway/filter/IndexFilter.java | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/doc/docs/files/10100_提供restful接口.md b/doc/docs/files/10100_提供restful接口.md index 1be388a3..c1218423 100644 --- a/doc/docs/files/10100_提供restful接口.md +++ b/doc/docs/files/10100_提供restful接口.md @@ -6,8 +6,6 @@ `http://ip:port/rest/服务id/your_path`,其中`http://ip:port/rest/`为固定部分,后面跟微服务请求路径。 -> 可在微服务端指定一个配置:`sop.restful.prefix=xxx`。请求路径将变成:`http://ip:port/rest/xxx/your_path` - 下面是一个微服务的接口例子 ```java @@ -36,6 +34,10 @@ public class TraditionalWebappController { 微服务之间的调用可以使用dubbo或feign,有了版本号的管理,可以做到服务的平滑升级,对用户来说都是无感知的。结合SOP-Admin提供的上下线功能, 可实现预发布环境功能。 +默认情况下,`http://ip:port/rest/`为固定部分,如果想要更改`rest`,可在网关配置文件指定:`sop.restful.path=/aaa` + +请求前缀将变成:`http://ip:port/aaa/` + - 封装请求工具【可选】 封装请求,方便调用,针对vue的封装如下: diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java index 8fb92151..e73688bf 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java @@ -44,7 +44,6 @@ public class ServerWebExchangeUtil { private static final String THROWABLE_KEY = "sop.throwable"; private static final String UNKNOWN_PATH = "/sop/unknown"; - private static final String REST_PATH = "/rest"; private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); @@ -77,10 +76,10 @@ public class ServerWebExchangeUtil { return ServerRequest.create(exchange, messageReaders); } - public static String getRestfulPath(String path) { - int index = path.indexOf(REST_PATH); + public static String getRestfulPath(String path, String prefix) { + int index = path.indexOf(prefix); // 取"/rest"的后面部分 - return path.substring(index + REST_PATH.length()); + return path.substring(index + prefix.length()); } /** diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java index 84e4018c..6903c71a 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java @@ -70,7 +70,7 @@ public class IndexFilter implements WebFilter { // 如果是restful请求,直接转发 if (path.startsWith(EnvironmentKeys.SOP_RESTFUL_PATH.getValue())) { exchange.getAttributes().put(SopConstants.RESTFUL_REQUEST, true); - String restfulPath = ServerWebExchangeUtil.getRestfulPath(path); + String restfulPath = ServerWebExchangeUtil.getRestfulPath(path, EnvironmentKeys.SOP_RESTFUL_PATH.getValue()); ServerWebExchange newExchange = ServerWebExchangeUtil.getForwardExchange(exchange, restfulPath); return chain.filter(newExchange); }