bug: 在路由上配置了一个前置插件ext-plugin-pre-req和后置插件ext-plugin-post-resp,在前置插件中获取请求信息,在后置插件中获取响应信息,在多线程环境下,如何将请求数据串联起来
forget99 opened this issue · 3 comments
Issue description
在路由上配置了一个前置插件ext-plugin-pre-req和后置插件ext-plugin-post-resp,在前置插件中获取请求信息,在后置插件中获取响应信息,在多线程环境下,如何将请求数据串联起来。使用线程id和requestId都会出现错乱。
code
@Override
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
long requestId = request.getRequestId();
String uuid = UuidUtil.create2();
Thread thread = Thread.currentThread();
thread.setName(uuid);
logger.info("【前置拦截过滤器线程名称】:" + uuid);
String path = request.getPath();
requestPathMaps.put(uuid, path);
chain.filter(request, response);
}
@Override
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
Thread thread = Thread.currentThread();
String uuid = thread.getName();
logger.info("【后置拦截过滤器线程名称】:" + uuid);
long requestId = request.getRequestId() - 1;
String responseBody = formatBody(request.getBody());
logger.info("【后置拦截过滤器接口路径】:" + requestPathMaps.get(uuid));
logger.info("【后置拦截过滤器接口响应结果】:" + responseBody);
response.setBody(responseBody);
}
请问找到解决办法了吗?
请问找到解决办法了吗?
@xiaolajiyhy 找到了,apisix内置nginx,在java插件中可以通过requiredVars方法获取nginx内置变量,requestId就是请求唯一id,下面是解决办法:
@Override public List<String> requiredVars() { List<String> vars = new ArrayList<>(); vars.add("request_id"); return vars; }
@Override public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) { String requestId = request.getVars("request_id"); }
@Override public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) { String requestId = request.getVars("request_id"); }
请问找到解决办法了吗?
@xiaolajiyhy 找到了,apisix内置nginx,在java插件中可以通过requiredVars方法获取nginx内置变量,requestId就是请求唯一id,下面是解决办法:
@Override public List<String> requiredVars() { List<String> vars = new ArrayList<>(); vars.add("request_id"); return vars; }
@Override public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) { String requestId = request.getVars("request_id"); }
@Override public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) { String requestId = request.getVars("request_id"); }
感谢,我生成了一个uuid ,然后让上游在请求头里回传给我