In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to do log processing on zuul". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to do log processing on zuul.
Since zuul acts as an api gateway, all requests pass through here, so log processing related to requests can be done on the gateway.
My requirement is that I need to record the url,ip address of the request, the parameters, the time when the request occurred, the time spent on the whole request, and the response status of the request.
Even the result of the request response and so on.
Obviously, you need to implement such a function, you need to write a ZuulFliter, which should be processed before the request is sent to the client, and in the
After the route filter is routed, by default, the filter's order should be between 500 and 1000. So how do I get what I need?
What about the log information? Look for RequestContext, which stores all the information for the entire request during the life cycle of the request.
Now the coding is described in detail in the comments of the code. The code is as follows:
@ Componentpublic class LoggerFilter extends ZuulFilter {@ Override public String filterType () {return FilterConstants.POST_TYPE;} @ Override public int filterOrder () {return FilterConstants.SEND_RESPONSE_FILTER_ORDER-1;} @ Override public boolean shouldFilter () {return true;} @ Override public Object run () {RequestContext context = RequestContext.getCurrentContext (); HttpServletRequest request = context.getRequest () String method = request.getMethod (); / / Type of hydrogen, post get. Map params = HttpUtils.getParams (request); String paramsStr = params.toString (); / / request parameter long statrtTime = (long) context.get ("startTime"); / / request start time Throwable throwable = context.getThrowable (); / / request exception, if any, request.getRequestURI () / / request uri HttpUtils.getIpAddress (request); / / request iP address context.getResponseStatusCode (); / / request status long duration=System.currentTimeMillis ()-statrtTime); / / request time spent return null;}}
Now readers may have questions about how to get the statrtTime, that is, the start time of the request. In fact, this requires another filter. Before the network request route (most of the time is spent in the route step), in the filter, you can store a time in RequestContext and write another filter. The code is as follows:
@ Componentpublic class AccessFilter extends ZuulFilter {@ Override public String filterType () {return "pre";} @ Override public int filterOrder () {return 0;} @ Override public boolean shouldFilter () {return true;} @ Override public Object run () {RequestContext ctx = RequestContext.getCurrentContext (); ctx.set ("startTime", System.currentTimeMillis ()); return null }}
There may be such a requirement that I need to store the response result in log, which has been analyzed before, and will be obtained from a specific service after the end of route.
The response stream is stored in RequestContext, written in HttpServletResponse in the SendResponseFilter filter, and eventually returned to the client. Then I just need to write the response stream to the log log before SendResponseFilter writes the response stream, which will lead to another problem, because after the response stream is written to log, RequestContext will have no response stream, and there will be no stream input into HttpServletResponse in SendResponseFilter, resulting in no return data from the client. The solution is as follows:
InputStream inputStream = RequestContext.getCurrentContext () .getResponseDataStream (); InputStream newInputStream= copy (inputStream); transerferTolog (inputStream); RequestContext.getCurrentContext () .setResponseDataStream (newInputStream)
After obtaining the stream from RequestContext, first copy the stream, convert it into a string, store it in the log, and then set it to RequestContext.
This allows SendResponseFilter to return the response to the client. This has a bit of a performance impact, and more processing may be needed if it is not a character stream.
At this point, I believe you have a deeper understanding of "how to do log processing on zuul". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.