You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.7 KiB
58 lines
1.7 KiB
5 years ago
|
# 网关拦截器
|
||
|
|
||
|
从3.1.0开始新增了网关拦截器,使用该拦截器可做一些数据统计,日志记录等工作。
|
||
|
|
||
|
使用方法如下:
|
||
|
|
||
|
- 在sop-gateway工程下新增一个类,实现`RouteInterceptor`接口,实现接口中的方法。别忘了加`@Component`
|
||
|
|
||
|
```java
|
||
|
@Component
|
||
|
public class MyRouteInterceptor implements RouteInterceptor {
|
||
|
@Override
|
||
|
public void preRoute(RouteInterceptorContext context) {
|
||
|
ApiParam apiParam = context.getApiParam();
|
||
|
System.out.println("请求接口:" + apiParam.fetchNameVersion());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void afterRoute(RouteInterceptorContext context) {
|
||
|
System.out.println("请求成功,微服务返回结果:" + context.getServiceResult());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getOrder() {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
RouteInterceptor接口方法说明:
|
||
|
|
||
|
- `public void preRoute(RouteInterceptorContext context)`
|
||
|
|
||
|
路由转发前执行,在签名验证通过之后会立即执行这个方法。
|
||
|
|
||
|
- `public void afterRoute(RouteInterceptorContext context)`
|
||
|
|
||
|
路由转发完成后,即拿到微服务返回结果后执行这个方法
|
||
|
|
||
|
- `public int getOrder()`
|
||
|
|
||
|
指定拦截执行顺序,数字小的优先执行,建议从0开始。
|
||
|
|
||
|
- `default boolean match(RouteInterceptorContext context)`
|
||
|
|
||
|
是否匹配,返回true执行拦截器,默认true
|
||
|
|
||
|
RouteInterceptorContext参数存放了各类参数信息。
|
||
|
|
||
|
参考类:
|
||
|
|
||
|
- `com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor` 拦截器接口
|
||
|
- `com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext` 拦截器上下文
|
||
|
- `com.gitee.sop.gatewaycommon.interceptor.MonitorRouteInterceptor` 默认实现的拦截器,用于收集监控数据
|
||
|
|
||
|
|
||
|
|