Files
SOP/doc/docs/files/10090_网关拦截器.md
2020-02-18 19:51:26 +08:00

58 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 网关拦截器
从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` 默认实现的拦截器,用于收集监控数据