http之常用客户端
程序员文章站
2022-03-30 19:48:57
...
1、常用客户端包括httpclient、restTemplate、okHttp,
feign是一层伪装,实现默认是httpClient,可以替换成okHttp
httpClient,较复杂,设计资源回收等。
apache提供的一个接口,HttpClient项目已经不维护,转移在httpcomponents组件下。
The Commons HttpClient project is now end of life, and is no longer being developed. It has been replaced by the Apache HttpComponents project in its HttpClient and HttpCore modules, which offer better performance and more flexibility.
restTemplate,spring提供,提供了较为便捷的http访问。
okHttp,允许复用同一个ip和端口的请求复用socket,可以降低网络连接的时间,降低服务器连接压力。高效的http客户端。
2、restTemplate
用户可以配置 restTemplate的实现底层,默认是java.net.HttpURLConnection,
/** * Create a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}. * @param requestFactory the HTTP request factory to use * @see org.springframework.http.client.SimpleClientHttpRequestFactory * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory */ public RestTemplate(ClientHttpRequestFactory requestFactory) { this(); setRequestFactory(requestFactory); }
可以通过定制http客户端工厂,实现改为CloseableHttpClient、okHttpClient等底层客户端实现。
参考资料:
https://github.com/OpenFeign/feign