春天如何在@feignclient配置中设置自定义最大连接池大小

Ank*_*kar 2 java spring-cloud netflix-feign feign spring-cloud-netflix

@feignclient春季如何在配置中设置自定义最大连接池大小,

@FeignClient(name = "content-cms", configuration = ContentCmsServiceFeignConfig.class)
public interface FeignService {

@RequestMapping(value = "/test/", method = RequestMethod.GET)
String getSample(@RequestParam("token") String token, @RequestParam("cid") String cid,
        @RequestParam("ratio") String ratio, @RequestParam("s") String source);

}
Run Code Online (Sandbox Code Playgroud)

Kev*_*vis 5

您可以在所Client使用的特定实现中配置连接数。Feign提供了开箱即用的支持Apache HttpOkHttp并且Ribbon。使用时Spring Cloud Open Feign,默认客户端基于您的类路径中的内容。

这是一个使用的示例Apache Http,您可以CloseableHttpClient使用所需的设置来配置自己的bean。

@Configuration
public class HttpClientConfiguration {
    @Bean
    public CloseableHttpClient httpClient() {
       return HttpClients.custom()
                  .maxConnectionsPerRoute(200)
                  .maxConnections(200)
                  .build()
    }
} 
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Spring Boot,则还可以配置任何feign.httpclient.*属性。

feign:
   httpclient:
       maxConnections: 200
       maxConnectionsPerRoute: 200
Run Code Online (Sandbox Code Playgroud)

您可以在Spring Cloud OpenFeign文档中找到更多信息:覆盖Feign默认值

  • 为了完成这个答案,有一个让我苦苦挣扎了一整天的细节:需要在 Feign 和 Apache HttpClient 之间添加一座桥梁。例如:`<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency>` (3认同)