【问题标题】:Spring Cloud: Feign and Http Connection PoolingSpring Cloud:Feign 和 Http 连接池
【发布时间】:2016-07-09 06:23:32
【问题描述】:

谁能告诉我Spring Cloud Feign Client是否提供或支持Http连接池,如果是,如何配置池大小等设置?我似乎在官方文档中找不到这个。谢谢。

【问题讨论】:

    标签: connection-pooling netflix-feign spring-cloud-feign


    【解决方案1】:

    这是一个例子。

    @Bean
    public ServiceXFeignClient serviceXClient(Encoder encoder, Decoder decoder,
      Contract contract, ClientProperties properties, ProxyProperties proxyProperties) {
    
      OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder()
        .connectionPool(
          new ConnectionPool(properties.getPoolConnectionMaxIdle(),
          properties.getPoolConnectionKeepMinutesAlive(), TimeUnit.MINUTES))
        .build();
    
      return Feign.builder()
            .client(new feign.okhttp.OkHttpClient(okHttpClient))
        .encoder(encoder)
        .decoder(decoder)
        .contract(contract)
        .target(ServiceXFeignClient.class, properties.getUrl());
    }

    【讨论】:

      【解决方案2】:

      通过调查,我将尝试回答我自己的问题:

      Spring Cloud Feign 使用 Netflix Feign。 Netflix Feign 反过来使用 java.net.HttpURLConnection 创建连接,它使用“持久连接”而不是连接池。

      可以覆盖客户端,例如使用 Apache HttpClient 代替,Netflix 为此提供了一个库(feign-httpclient)。使用这种方法时,可以使用 SystemProperties 设置连接池大小。

      在 Spring Cloud Brixton 中,如果 Apache HttpClient 或 OkHttpClient 可用(通过@ConditionalOnClass),那么它们会被自动使用。

      【讨论】:

      • 你确定 Spring Cloud 在类路径中会自动使用 Apache HttpClient 吗?
      • 你需要添加io.github.openfeign:feign-httpclient,而不仅仅是apache http客户端
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2016-01-27
      相关资源
      最近更新 更多