pull/5/MERGE
tanghc 4 years ago
parent 70b8525ccc
commit 4c552199fb
  1. 6
      doc/docs/files/10100_提供restful接口.md
  2. 7
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java
  3. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java

@ -6,8 +6,6 @@
`http://ip:port/rest/服务id/your_path`,其中`http://ip:port/rest/`为固定部分,后面跟微服务请求路径。 `http://ip:port/rest/服务id/your_path`,其中`http://ip:port/rest/`为固定部分,后面跟微服务请求路径。
> 可在微服务端指定一个配置:`sop.restful.prefix=xxx`。请求路径将变成:`http://ip:port/rest/xxx/your_path`
下面是一个微服务的接口例子 下面是一个微服务的接口例子
```java ```java
@ -36,6 +34,10 @@ public class TraditionalWebappController {
微服务之间的调用可以使用dubbo或feign,有了版本号的管理,可以做到服务的平滑升级,对用户来说都是无感知的。结合SOP-Admin提供的上下线功能, 微服务之间的调用可以使用dubbo或feign,有了版本号的管理,可以做到服务的平滑升级,对用户来说都是无感知的。结合SOP-Admin提供的上下线功能,
可实现预发布环境功能。 可实现预发布环境功能。
默认情况下,`http://ip:port/rest/`为固定部分,如果想要更改`rest`,可在网关配置文件指定:`sop.restful.path=/aaa`
请求前缀将变成:`http://ip:port/aaa/`
- 封装请求工具【可选】 - 封装请求工具【可选】
封装请求,方便调用,针对vue的封装如下: 封装请求,方便调用,针对vue的封装如下:

@ -44,7 +44,6 @@ public class ServerWebExchangeUtil {
private static final String THROWABLE_KEY = "sop.throwable"; private static final String THROWABLE_KEY = "sop.throwable";
private static final String UNKNOWN_PATH = "/sop/unknown"; private static final String UNKNOWN_PATH = "/sop/unknown";
private static final String REST_PATH = "/rest";
private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
@ -77,10 +76,10 @@ public class ServerWebExchangeUtil {
return ServerRequest.create(exchange, messageReaders); return ServerRequest.create(exchange, messageReaders);
} }
public static String getRestfulPath(String path) { public static String getRestfulPath(String path, String prefix) {
int index = path.indexOf(REST_PATH); int index = path.indexOf(prefix);
// 取"/rest"的后面部分 // 取"/rest"的后面部分
return path.substring(index + REST_PATH.length()); return path.substring(index + prefix.length());
} }
/** /**

@ -70,7 +70,7 @@ public class IndexFilter implements WebFilter {
// 如果是restful请求,直接转发 // 如果是restful请求,直接转发
if (path.startsWith(EnvironmentKeys.SOP_RESTFUL_PATH.getValue())) { if (path.startsWith(EnvironmentKeys.SOP_RESTFUL_PATH.getValue())) {
exchange.getAttributes().put(SopConstants.RESTFUL_REQUEST, true); 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); ServerWebExchange newExchange = ServerWebExchangeUtil.getForwardExchange(exchange, restfulPath);
return chain.filter(newExchange); return chain.filter(newExchange);
} }

Loading…
Cancel
Save