In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what is the method of Hystrix service degradation and exception handling in Spring Cloud", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of Hystrix service degradation and exception handling in Spring Cloud?"
Service degradation
In the previous two articles, the function described by fallbackMethod is actually a spare, which is used to implement service degradation. In the comments, we can specify the name of the method for degradation through the fallbackMethod attribute, and we can handle the logic after service degradation by overriding the getFallback function when customizing the Hystrix request command. When using annotations to define service degradation logic, the service degradation function and @ HystrixCommand annotation should be in the same class. At the same time, exceptions may occur during the execution of the service degradation function, so you can also add a "backup" to the service degradation function, as shown below:
@ HystrixCommand (fallbackMethod = "error1") public Book test2 () {return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);} @ HystrixCommand (fallbackMethod =" error2 ") public Book error1 () {/ / initiate a network request (which may fail) return null;} public Book error2 () {return new Book ();}
In actual development, not all requests have to prepare the service degradation in advance. If I just want to show the user the information about the failure of the service call, then there is no need to add a circuit breaker at this time.
Exception handling
We may throw an exception when we call the service provider. By default, if the method throws an exception, the service will automatically be degraded and left to the method in the service degradation to handle. In the way of custom Hystrix request commands, we can call the getExecutionException method in the getFallback method to get the thrown exception, to give a simple example:
@ Overrideprotected Book getFallback () {Throwable executionException = getExecutionException (); System.out.println (executionException.getMessage ()); return new Book (selected Notes of Song Poems, 88, Qian Zhongshu, Sanlian Bookstore);} @ Overrideprotected Book run () throws Exception {int I = 1 / 0; return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);}
The results of the visit are as follows:
The log printed by the console is as follows:
The service was degraded automatically.
If we use annotations, we only need to add a parameter of type Throwable to the service degradation method to get the type of exception thrown, as follows:
HystrixCommand (fallbackMethod = "error1") public Book test2 () {int I = 1 / 0; return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);} public Book error1 (Throwable throwable) {System.out.println (throwable.getMessage ()); return new Book (" one hundred years of Solitude ", 33," M á rquez "," people's Literature Publishing House ");}
The log printed by the console is also / by zero, so I won't post any more pictures. At this point, if an exception is thrown and I do not want to enter the service degradation method to handle it, but throw the exception directly to the user, we can add the ignore exception in the @ HystrixCommand annotation, as follows:
HystrixCommand (fallbackMethod = "error1", ignoreExceptions = ArithmeticException.class) public Book test2 () {int I = 1 / 0; return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);} public Book error1 (Throwable throwable) {System.out.println (throwable.getMessage ()); return new Book (" A hundred years of Solitude ", 33," M á rquez "," people's Literature Publishing House ");}
The execution result is as follows:
The implementation principle here is very simple, because there is an exception called HystrixBadRequestException that will not go into the service degradation method. When we define ignoreExceptions as ArithmeticException.class, when the ArithmeticException exception is thrown, Hystrix will wrap the exception information in HystrixBadRequestException and then throw it. At this time, the service degradation method will not be triggered.
At this point, I believe you have a deeper understanding of "what is the method of Hystrix service degradation and exception handling in Spring Cloud". 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.