英文:
REST API request timeout for services which are always slow
问题
我正在处理一个项目,在这个项目中我需要调用一个第三方的REST服务。目前的问题是,该服务的响应时间至少需要 16
秒,甚至可能超过这个时间。
为了避免线程在服务器上等待,我的服务设置了一个 16 秒的超时值。但是这个超时值并没有起到作用。我在这个问题上进行了搜索,并发现断路器模式会很有用。参考链接:spring-boot-rest-api-request-timeout。我认为这个模式在服务偶尔出现较慢响应的情况下很有用。但在我的情况下,这个服务总是很慢。
我该如何应对这种情况?
英文:
I am working on a project where I have to call a third-party REST service. The problem with the current setup is that service does not return in at least 16
seconds. This response time may exceed more than that.
To avoid the threads waiting on the server, my service has a timeout value of 16 seconds. But that value is not helping. I searched on this and found that the Circuit breaker pattern will be useful. Reference:- spring-boot-rest-api-request-timeout . I believe this pattern is useful when the service has a slow response a few times. In my case, it is always a slow service.
How can I tackle this scenario?
答案1
得分: 3
如果您希望从第三方REST服务获得响应,您别无选择,只能等待,但如果您的请求方法有其他事情要做,您应该使用可调用线程将请求发送到REST服务,然后让主线程先完成其他工作,然后等待可调用线程返回。
也许您可以尝试在这种情况下使用一些缓存,如@Cacheable或Redis。这可能会加速某些类似的请求。
或者,只需让您的请求方法首先将响应发送回客户端。之后,使用AJAX从客户端访问第三方REST服务。
英文:
If you want the response from the third party REST service, you have no choise but to wait, but if your request method have other thing to do. You should use Callable Thread to sent request to REST service and let Main Thread to complete the other work first then wait for the Callable to come back.
Maybe you can try to use some Cache like @Cacheable or Redis for this scenario. It may speed up some of the similar request.
Or, just let your request method sent the response back to client first. After that, use AJAX to access the third party REST service from the client side.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论