[英]Can I get the request's response time using only a Zuul post filter?
I'm currently using Zuul as a reverse proxy, Ribbon as a load balancer and Eureka as service discovery. 我当前使用Zuul作为反向代理,使用Ribbon作为负载平衡器,并使用Eureka作为服务发现。
Is it possible to get the request's response time using only a Zuul post filter? 是否可以仅使用Zuul帖子过滤器来获取请求的响应时间? Can I also get the timestamps of a request entering and leaving the gateway? 我还能获得进入和离开网关的请求的时间戳吗?
i guess thats possible, but it would involve combination of filters, restTemplate interceptor and servlet Filter classes 我想那是可能的,但它将涉及过滤器,restTemplate拦截器和Servlet过滤器类的组合
i wud have given the diagram had their been a plantuml plugin for stackoverflow 我会给图,如果他们是一个plantuml的stackoverflow插件
idea is , create a preFilter lets say TimeTrackerFilter that would add entry point time stamp "time-start-id" in the request header, a userContextFilter which will intercept all incoming HTTP request and map HTTP request to a userContext class , UserContextClass consists of a getter/setter method that retrieves and stores values from java.lang.ThreadLocal 想法是,创建一个preFilter,让它说TimeTrackerFilter,它将在请求标头中添加入口点时间戳“ time-start-id”;一个userContextFilter,它将拦截所有传入的HTTP请求并将HTTP请求映射到userContext类,UserContextClass由一个用于从java.lang.ThreadLocal检索和存储值的getter / setter方法
UserContextHolder.java to store the UserContext in a ThreadLocal variable that is accessible in any method being invoked by the thread processing the user's request UserContextHolder.java将UserContext存储在ThreadLocal变量中,该变量可以在处理用户请求的线程调用的任何方法中访问
UserContextINterceptor class to inject the "time-start-id" to any outgoing http based service request executed from a rest template UserContextINterceptor类,用于将“ time-start-id”注入到从rest模板执行的所有基于HTTP的传出服务请求中
a post filter that will actually get the time-start-id from request context, calculate time diff and add a new header "time taken " with value being time diff to the response 一个后置过滤器,该过滤器实际上将从请求上下文中获取time-start-id,计算时间差异,并在响应中添加一个新的标头“ time take”,其值为time diff
in case you dont want to see the start time id in other services being called, using only pre and post filter should suffice (ignore userContext, and interceptors in that case) 如果您不想看到正在调用的其他服务中的开始时间ID,则仅使用前置和后置过滤器就足够了(在这种情况下,请忽略userContext和拦截器)
note : when you want to add a value to the HTTP request headers, you use the RequestContext's addZuulRequestHeader() method. 注意 :当您想向HTTP请求标头添加值时,可以使用RequestContext的addZuulRequestHeader()方法。 This method will maintain a separate map of HTTP headers that were added while a request was flowing through the filters with your Zuul server. 此方法将维护一个单独的HTTP标头映射,该映射在请求通过Zuul服务器流经过滤器时添加。 The data con- tained within the ZuulRequestHeader map will be merged when the target service is invoked by your Zuul server. 当您的Zuul服务器调用目标服务时,ZuulRequestHeader映射中包含的数据将被合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.