我面临以下情况,令我惊讶的是,我找不到太多文件: 有一项服务仅通过1比1获得项目详细信息的休息呼叫。 总共有1k +项目。
出于回应的原因,我想将这些数据保留在我的最后,而不是懒散地获取它。
为了不锁定我的API密钥,我想将我的呼叫限制为X次呼叫/秒。
我在Feign文档中找不到任何支持。 有人知道是否有吗?或者你对如何实施这个实施有任何建议吗?
答案 0 :(得分:1)
Feign
中没有内置限制功能,它被委托给基础Client
实现。话虽如此,您可以定义自己的客户端,从提供的客户端Apache Http
,OkHttp
和Ribbon
扩展。
一种解决方案是将Client
扩展为使用此答案中概述的ScheduledThreadPoolExecutor
。
Apache HttpClient: Limit total calls per second
要将其与ApacheHttpClient
中提供的Feign
一起使用,您可以扩展它,提供您自己的execute
方法实现以使用执行程序。
public class ThrottledHttpClient extends ApacheHttpClient {
// create a pool with one thread, you'll control the flow later.
private final ExecutorService throttledQueue = Executors.newScheduledThreadPool(1);
@Override
public Response execute(Request request, Request.Options options) throws IOException {
// use the executor
ScheduledFuture<Response> future = throttledQueue.scheduleAtFixedRate(super.execute(), ....);
return future.get()
}
设置适当的线程池大小,延迟和固定等待以实现所需的吞吐量。