Spring Cloud zuul custom unified Exception Handling implementation method, cloudzuul
Zuul provides filer and router functions in the springcloud microservice System, which is an indispensable part of microservices. In addition to the default implementation, filer can also customize authorization, throttling, and security verification. The router can completely replace Nginx reverse proxy. The Zuul exception is handled by SendErrorFilter.
During our application process, we found that the default exception filter has two problems:
1. Unable to quickly identify whether the request route service times out or does not have any available nodes. If an error occurs, you can only view the log and locate it through the stack;
2. Unable to be compatible with custom features such{code:500,msg:”xx error”}
Response package format.
Next we will discuss how to customize Exception Handling and custom exception prompts.
First, we must disable the default SendErrorFilter. The official website has provided the switch configuration, which can be directly configured.
zuul.SendErrorFilter.post.disable=true
Custom ErrorFilter. I will not talk about it here. paste the Code directly.
Public class ErrorFilter extends ZuulFilter {private static final String ERROR_STATUS_CODE_KEY = "error. status_code "; private Logger log = LoggerFactory. getLogger (ErrorFilter. class); public static final String DEFAULT_ERR_MSG = "the system is busy. Please try again later"; @ Override public String filterType () {return "post" ;}@ Override public int filterOrder () {return 0 ;}@ Override public boolean shouldFilter () {RequestCont Ext ctx = RequestContext. getCurrentContext (); return ctx. containsKey (ERROR_STATUS_CODE_KEY);} @ Override public Object run () {RequestContext ctx = RequestContext. getCurrentContext (); try {HttpServletRequest request = ctx. getRequest (); int statusCode = (Integer) ctx. get (ERROR_STATUS_CODE_KEY); String message = (String) ctx. get ("error. message "); if (ctx. containsKey ("error. exception ") {Throwable E = (Exception) ctx. get ("error. exception "); Throwable re = getOriginException (e); if (re instanceof java.net. connectException) {message = "Real Service Connection refused"; log. warn ("uri :{}, error :{}", request. getRequestURI (), re. getMessage ();} else if (re instanceof java.net. socketTimeoutException) {message = "Real Service Timeout"; log. warn ("uri :{}, error :{}", request. getRequestURI (), re. getMessage () );} Else if (re instanceof com. netflix. client. clientException) {message = re. getMessage (); log. warn ("uri :{}, error :{}", request. getRequestURI (), re. getMessage ();} else {log. warn ("Error during filtering", e) ;}} if (StringUtils. isBlank (message) message = DEFAULT_ERR_MSG; request. setAttribute ("javax. servlet. error. status_code ", statusCode); request. setAttribute ("javax. servlet. error. message ", message); W EbUtils. responseOutJson (ctx. getResponse (), JsonUtils. toJson (new WrapperResponse <> (statusCode, message);} catch (Exception e) {String error = "Error during filtering [ErrorFilter]"; log. error (error, e); WebUtils. responseOutJson (ctx. getResponse (), JsonUtils. toJson (new WrapperResponse <> (500, error);} return null;} private Throwable getOriginException (Throwable e) {e = e. getCause (); while (e. getC Ause ()! = Null) {e = e. getCause () ;}return e ;}}
Finally, register our custom ErrorFilter
@Bean public ErrorFilter errorFilter(){ return new ErrorFilter();}
Summary
The above is a simple example of Spring Cloud zuul custom unified Exception Handling implementation method. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!