In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "Springboot RestTemplate how to set timeout" in addition to programmers, most people do not understand, today, in order to make you better understand "Springboot RestTemplate how to set timeout", summed up the following content, with a certain reference value, the detailed steps are clear, the details are handled properly, I hope you can get something through this article, let's take a look at the specific content.
The timeout is not set by RestTemplate, which results in massive accumulation of RabbitMQ queues, fake death of consumers and non-consumption, similar to thread congestion.
Troubleshooting:
From the log to troubleshoot the problem, during the period from entering the consumer to the end, the business service will be called. However, from the start time to the end time, it takes up to more than 100 seconds. No exception was found when viewing the business service. The cause of this problem may also be caused by network fluctuations during consumption, which may lead to problems with HTTP connectivity and have been waiting. It is concluded that the problem may be caused by the HTTP timeout is not set.
In this paper, the production environment Springboot version 2.1.5.RELEASE is configured with Java config.
Java Config configuration @ Configurationpublic class RestTemplateConfig {@ Bean public RestTemplate getRestTemplate () {/ / configure HTTP timeout HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory (); httpRequestFactory.setConnectionRequestTimeout (6000); httpRequestFactory.setConnectTimeout (6000); httpRequestFactory.setReadTimeout (6000); return new RestTemplate (httpRequestFactory) }} configuration file specifies custom.rest.connection.connection-request-timeout=3000custom.rest.connection.connect-timeout=3000custom.rest.connection.read-timeout=3000@Configurationpublic class AppConfig {@ Bean @ ConfigurationProperties (prefix = "custom.rest.connection") public HttpComponentsClientHttpRequestFactory customHttpRequestFactory () {return new HttpComponentsClientHttpRequestFactory ();} @ Bean public RestTemplate customRestTemplate () {return new RestTemplate (customHttpRequestFactory ());}}
After the redeployment, consumers quickly began to spend, and the accumulated news has been consumed.
RestTemplate set timeout points
1. Make sure there is only one RestTemplate configuration in the system; otherwise, it may not be consistent with your expectations.
2. Never trust the code you write; only by printing more logs can you really know the call time.
Long s = System.currentTimeMillis (); try {responseEntity = restTemplate.exchange ();} catch (Exception e) {long costTime = System.currentTimeMillis ()-s; log.error ("call * * service exception, time spent: {}, error: {}", costTime, e.getMessage (), e) } Thank you for your reading. I hope you have a certain understanding of the key issue of "how to set the timeout of Springboot RestTemplate". The specific usage still needs to be understood by hands-on experiments. Go and try it. If you want to read more articles about relevant knowledge points, 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.