【问题标题】:Does Feign client have native implementation of bottlenecking?Feign 客户端是否具有瓶颈的本机实现?
【发布时间】:2018-04-16 10:36:08
【问题描述】:

我面临以下情况,令我惊讶的是,我找不到太多文档: 有一项服务仅提供对项目详细信息的休息调用,方法是逐个获取。 总共有 1k+ 项。

出于响应性原因,我希望将这些数据持久化,而不是懒惰地获取它。

为了不锁定我的 API 密钥,我想将我的调用限制为 X 次调用/秒。

我在 Feign 文档中找不到对此的任何支持。 有人知道是否有吗?或者您对如何进行此实施有任何建议?

【问题讨论】:

    标签: java spring-boot netflix-feign feign


    【解决方案1】:

    Feign 中没有内置的限制功能,它被委托给底层的Client 实现。话虽如此,您可以定义自己的客户端,从提供的Apache HttpOkHttpRibbon 之一扩展而来。

    一种解决方案是扩展 Client 以使用此答案中概述的 ScheduledThreadPoolExecutorApache HttpClient: Limit total calls per second

    要将其与Feign 中提供的ApacheHttpClient 一起使用,您可以扩展它,提供您自己的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()
    }
    

    设置适当的线程池大小、延迟和固定等待以达到您想要的吞吐量。

    【讨论】:

    • 谢谢,我会检查一下,并将其标记为正确答案,一旦我对上述资源进行研究
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2019-11-04
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多