In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of RestTemplate request failure automatic restart mechanism, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on RestTemplate request failure automatic restart mechanism. Let's take a look at it.
A general exception handling mechanism: automatic retry. That is, when the RestTemplate sends the request to get a non-200 status result, the request is sent again at a certain interval. After n failed requests, the HttpClientErrorException is finally thrown. Before you start the code in this section, comment out the code for RestTemplate custom exception handling in the previous section, otherwise the automatic retry mechanism will not take effect. As follows (see the code in the previous section):
/ / restTemplate.setErrorHandler (new MyRestErrorHandler ()); I. Spring Retry configuration takes effect
The implementation of introducing spring-retry,spring-retry through maven coordinates depends on aspect-oriented programming, so aspectjweaver is introduced. The following configuration procedures are based on Spring Boot applications.
Org.springframework.retry spring-retry 1.2.5.RELEASE org.aspectj aspectjweaver
Add the @ SpringRetry annotation to the startup class of the Spring Boot application portal, that is, the configuration class, to make the retry mechanism effective.
Second, use cases
Write a mock business class RetryService and inject RestTemplate into it.
RestTemplate instantiates Bean configuration reference:
RestTemplate uses intensive speaking in Spring or non-Spring environments
RestTemplate implements the switching usage of a variety of underlying HTTP client class libraries.
Change the correct request service address from "/ posts/1" to "/ postss/1". The service does not exist, so a 404 exception is thrown to trigger the retry mechanism.
@ Servicepublic class RetryService {@ Resource private RestTemplate restTemplate; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"); @ Retryable (value = RestClientException.class, maxAttempts = 3, backoff = @ Backoff (delay = 5000L) public HttpStatus testEntity () {System.out.println ("initiate remote API request:" + DATE_TIME_FORMATTER.format (LocalDateTime.now () String url = "http://jsonplaceholder.typicode.com/postss/1"; ResponseEntity responseEntity = restTemplate.getForEntity (url, String.class); return responseEntity.getStatusCode (); / / get response code}}
The @ Retryable annotated method will be retried when an exception occurs. The parameter description:
Value: retries when a specified exception occurs, and HttpClientErrorException is a subclass of RestClientException.
Include: like value, the default is empty. If exclude is also empty, all exceptions are retried
Exclude: specifies that the exception will not be retried and is null by default. If include is also empty, all exceptions are retried
MaxAttemps: maximum number of retries. Default is 3.
Backoff: retry the wait policy. Default is empty.
@ Backoff is annotated as the policy of retry waiting. The parameters are described:
Delay: specifies the delay time for retry. Default is 1000 milliseconds.
Multiplier: specify a multiple of delay, such as when setting delay=5000,multiplier=2, 5 seconds after the first retry, 10 (5x2) seconds for the second time, and 20 (10x2) seconds for the third time.
Write a test RetryController to call the testEntity method of RetryService
@ RestControllerpublic class RetryController {@ Resource private RetryService retryService; @ GetMapping ("/ retry") public HttpStatus test () {return retryService.testEntity ();}} 3. Test results
Initiate a request to http://localhost:8080/retry, and the result is as follows:
As can be seen from the results:
After the first request fails, try again after a delay of 5 seconds
After the second request fails, try again after a delay of 10 seconds
After the third request fails, an exception is thrown
This is the end of the article on "what is the automatic restart mechanism for failed RestTemplate requests?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the automatic restart mechanism for failed RestTemplate requests". If you want to learn more, you are welcome to follow the industry information channel.
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.