If there are several threads call http service with Feign, will Feign create http connection for each of them? Or Feign will only create one connection, and these threads can only call service one by one?
It is my understanding that connections are managed by the Client objects, which themselves are expected to be thread safe. How connections are managed is delegated to the Client implementations. For example, using an ApacheHttpClient, you can configure a custom HttpClient as you see fit. Here is an example from the Apache Documenation around Multithreaded request execution.
/* create a pooling connection manager */
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager();
/* set our connection manager to support 50 simultaneous connections
* total and limit the number of connections to each host to 5
*/
connectionManager.setMaxTotal(50);
connectionManager.setDefaultMaxPerRoute(5);
/* create a new HttpClient using our Pooled Connection Manager */
CloseableHttpClient httpClient = HttpClients.custom().
setConnectionManager(connectionManager).build();
/* use this client in our Feign Builder */
GitHub gitHub = Feign.Builder()
.client(new ApacheHttpClient(httpClient))
.target(GitHub.class, "https://api.github.com");
Feign's default implementation uses Native UrlConnection instances. I highly recommend using one of the other implementations, Ribbon, OkHttp, or HttpClient instead for real-world use. See feign-httpclient, feign-okhttp and feign-ribbon and the README for more information.
Most helpful comment
It is my understanding that connections are managed by the
Clientobjects, which themselves are expected to be thread safe. How connections are managed is delegated to theClientimplementations. For example, using anApacheHttpClient, you can configure a customHttpClientas you see fit. Here is an example from the Apache Documenation around Multithreaded request execution.Feign's default implementation uses Native
UrlConnectioninstances. I highly recommend using one of the other implementations, Ribbon, OkHttp, or HttpClient instead for real-world use. Seefeign-httpclient,feign-okhttpandfeign-ribbonand the README for more information.