Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the details of exception handling in Spring Cloud Zuul

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what are the details of exception handling in Spring Cloud Zuul". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the details of exception handling in Spring Cloud Zuul"?

First, let's take a look at an official life cycle diagram of Zuul requests, as follows:

With regard to this picture, I would like to say the following:

1. Normally, all requests are executed in the order of pre, route and post, and then post returns response

two。 During the pre phase, if there is a custom filter, the custom filter is executed.

If an exception is thrown at any stage of 3.pre, routing or post, the error filter is executed, and then post is executed to give a response.

This is the information given to us by this diagram. Let's take a look at the service method in the source code com.netflix.zuul.http.ZuulServlet class, which is the core of the whole calling process, as follows:

Try {init ((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse); / / Marks this request as having passed through the "Zuul engine", as opposed to servlets / / explicitly bound in web.xml, for which requests will not have the same data attached RequestContext context = RequestContext.getCurrentContext (); context.setZuulEngineRan (); try {preRoute ();} catch (ZuulException e) {error (e); postRoute (); return } try {route ();} catch (ZuulException e) {error (e); postRoute (); return;} try {postRoute ();} catch (ZuulException e) {error (e); return;}} catch (Throwable e) {error (new ZuulException (e500, "UNHANDLED_EXCEPTION_" + e.getClass (). GetName () } finally {RequestContext.getCurrentContext () .unset ();}

We see a big try here. Catch, big try... There are three small try in the catch. Catch, little try... Catch is only responsible for catching ZuulException exceptions, and other exceptions are handed over to the big try. Catch to capture. Both pre and route execute error before post after an execution error, while post executes error instead of post after an error.

ZuulFilter will eventually be called in the processZuulFilter method of com.netflix.zuul.FilterProcessor, where it will determine whether the runFilter was executed successfully. If the execution fails, the exception information is extracted and then an exception is thrown. If the exception is an instance of ZuulException, an exception of type ZuulException is thrown. If it is not an instance of ZuulException, an exception of type ZuulException with a status code of 500 is thrown, so in any case What we end up seeing are exceptions of type ZuulException. Here I post part of the core code of the processZuulFilter method, as follows:

Public Object processZuulFilter (ZuulFilter filter) throws ZuulException {try {ZuulFilterResult result = filter.runFilter (); ExecutionStatus s = result.getStatus (); execTime = System.currentTimeMillis ()-ltime; switch (s) {case FAILED: t = result.getException (); ctx.addFilterExecutionSummary (filterName, ExecutionStatus.FAILED.name (), execTime); break Case SUCCESS: break; default: break;} if (t! = null) throw t; usageNotifier.notify (filter, s); return o;} catch (Throwable e) {usageNotifier.notify (filter, ExecutionStatus.FAILED); if (e instanceof ZuulException) {throw (ZuulException) e } else {ZuulException ex = new ZuulException (e, "Filter threw Exception", 500, filter.filterType () + ":" + filterName); ctx.addFilterExecutionSummary (filterName, ExecutionStatus.FAILED.name (), execTime); throw ex;}

In Zuul, all error problems are ultimately handled by the SendErrorFilter class. In the early version, this class was a post-type filter,post-type filter with a defect that it could not handle the exceptions thrown in post, which needs to be improved manually. The version I am using (Dalston.SR3) has fixed this problem. SendErrorFilter is now an error-type filter, and whenever there is an exception in RequestContext, it will enter the SendErrorFilter. Error messages are also extracted from the exception object. The core code is as follows:

@ Overridepublic boolean shouldFilter () {RequestContext ctx = RequestContext.getCurrentContext (); / / only forward to errorPath if it hasn't been forwarded to already return ctx.getThrowable ()! = null & &! ctx.getBoolean (SEND_ERROR_FILTER_RAN, false);} @ Overridepublic Object run () {try {RequestContext ctx = RequestContext.getCurrentContext (); ZuulException exception = findZuulException (ctx.getThrowable ()); HttpServletRequest request = ctx.getRequest () Request.setAttribute ("javax.servlet.error.status_code", exception.nStatusCode); log.warn ("Error during filtering", exception); request.setAttribute ("javax.servlet.error.exception", exception); if (StringUtils.hasText (exception.errorCause)) {request.setAttribute ("javax.servlet.error.message", exception.errorCause) } RequestDispatcher dispatcher = request.getRequestDispatcher (this.errorPath); if (dispatcher! = null) {ctx.set (SEND_ERROR_FILTER_RAN, true); if (! ctx.getResponse (). IsCommitted ()) {dispatcher.forward (request, ctx.getResponse ()) } catch (Exception ex) {ReflectionUtils.rethrowRuntimeException (ex);} return null;}

It is also convenient if we want to customize the exception information, as follows:

@ Componentpublic class MyErrorAttribute extends DefaultErrorAttributes {@ Override public Map getErrorAttributes (RequestAttributes requestAttributes, boolean includeStackTrace) {Map result = super.getErrorAttributes (requestAttributes, includeStackTrace); result.put ("status", 2222); result.put ("error", "error"); result.put ("exception", "exception"); result.put ("message", "message"); return result }} at this point, I believe you have a deeper understanding of "what are the details of exception handling in Spring Cloud 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report