Java Spring Boot 应用程序 - 任何其余 API 端点的默认超时时间或控制所有端点超时的简单配置是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44274982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Application - what is default timeout for any rest API endpoint or a easy config to control all endpoint timeout
提问by Gunjan Kumar
I am using current Spring boot version (1.4.x) and wondering if it has any default timeout for api calls. I have tested it by putting breakpoints but it was keep waiting and didn't time-out. I was also trying to configure default timeout for all my spring-boot apps by using some annotation or yml settings.
我正在使用当前的 Spring 引导版本 (1.4.x),并想知道它是否有任何默认的 api 调用超时。我已经通过设置断点对其进行了测试,但它一直在等待并且没有超时。我还尝试使用一些注释或 yml 设置为我所有的 spring-boot 应用程序配置默认超时。
I found couple of alternatives (one of them here) but using callable actually adding extra non-business logic code where setting something in xml bean is out of fashion in latest spring boot applications.
我找到了几个替代方案(其中一个在这里)但是使用 callable 实际上添加了额外的非业务逻辑代码,在最新的 Spring Boot 应用程序中,在 xml bean 中设置一些东西已经过时了。
采纳答案by Gunjan Kumar
I agree all above options and tried below option in my spring boot application. It works perfectly fine now. Below is the code sample as a bean. Now just need to @Autowire
RestTemplate
wherever(java class
) I need it.
我同意上述所有选项,并在我的 Spring Boot 应用程序中尝试了以下选项。它现在工作得很好。下面是作为 bean 的代码示例。现在只需要到@Autowire
RestTemplate
任何java class
我需要的地方()。
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setConnectTimeout(15000);
((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setReadTimeout(15000);
return restTemplate;
}
回答by tryingToLearn
There are a couple of ways to do this:
有几种方法可以做到这一点:
1) Using ClientHttpRequestFactory
with RestTemplate
:
1)使用ClientHttpRequestFactory
具有RestTemplate
:
public RestTemplate restTemplate() {
return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(timeinMillis);
factory.setConnectTimeout(timeinMillis);
return factory;
}
2) Second way is to use callable but I guess you have already explored that solution.
2)第二种方法是使用 callable ,但我想您已经探索过该解决方案。
回答by zeagord
The timeout can be set using the connectionTimeout
property of Tomcat.
可以使用connectionTimeout
Tomcat的属性设置超时时间。
Please refer this answer how to set it for Tomcat.
请参阅此答案如何为 Tomcat 设置它。
Configuring maxKeepAliveRequests in Spring Boot embedded Tomcat
回答by Danylo Zatorsky
You can try server.connection-timeout=5000
in your application.properties.From the official documentation:
你可以试试server.connection-timeout=5000
你的 application.properties.From官方文档:
server.connection-timeout= # Time in milliseconds that connectors will wait for another HTTP request before closing the connection. When not set, the connector's container-specific default will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.
server.connection-timeout= # 连接器在关闭连接之前等待另一个 HTTP 请求的时间(以毫秒为单位)。如果未设置,将使用连接器特定于容器的默认值。使用值 -1 表示没有(即无限)超时。
UPDATE: Just noticed that you use microservice architecture, so in case you need to handle timeouts when communicating between microservices, I would suggest handling it on the client side instead of the server side. If the microservice you are trying to call is overloaded and its performance degrades to the point where it drastically affects the user experience sometimes it's better to return some fallback data than just drop the request.
更新:刚刚注意到您使用微服务架构,所以如果您在微服务之间通信时需要处理超时,我建议在客户端而不是服务器端处理它。如果您尝试调用的微服务过载并且其性能下降到严重影响用户体验的程度,有时最好返回一些回退数据而不是仅仅删除请求。
Imagine we have an e-commerce web-site that has microservice architecture and one of its microservices that gives recommendations to the user becomes extremely slow. In this case, the preferred solution would be to return some fallback data which could be top 10 popular products this month rather than showing 5xx error page to the customer. Moreover, in case subsequent requests fail with a timeout, we can make a decision to avoid sending requests to the 'recommendation-service' and return fallback data immediately. After some time we can try sending a request to the 'recommendation-service' again, and if it became healthy - just use it instead of the fallback data.
想象一下,我们有一个电子商务网站,它具有微服务架构,并且其中一个向用户提供推荐的微服务变得非常缓慢。在这种情况下,首选的解决方案是返回一些后备数据,这些数据可能是本月前 10 名热门产品,而不是向客户显示 5xx 错误页面。此外,如果后续请求因超时而失败,我们可以决定避免向“推荐服务”发送请求并立即返回回退数据。一段时间后,我们可以尝试再次向“推荐服务”发送请求,如果它变得健康 - 只需使用它而不是回退数据。
This is called Circuit Breakerpattern and there is already an implementation of it in a framework called Hystrix. Here is a nice article explaining it in depth: http://www.baeldung.com/spring-cloud-netflix-hystrix. Spring Cloud Feign + Spring Cloud Hystrix looks really nice especially taking into account that they work with Discovery services out-of-the-box (e.g. Spring Cloud Eureka).
这称为断路器模式,并且已经在称为 Hystrix 的框架中实现了它。这是一篇深入解释它的好文章:http: //www.baeldung.com/spring-cloud-netflix-hystrix。Spring Cloud Feign + Spring Cloud Hystrix 看起来非常好,特别是考虑到它们与开箱即用的 Discovery 服务(例如 Spring Cloud Eureka)一起工作。