概述
在正常状况下Feign有三种客户端实现:java
-
Client.Default
类:默认的 feign.Client 客户端实现类,内部使用HttpURLConnnection
完成HTTP URL请求处理;git -
ApacheHttpClient
类:内部使用Apache httpclient
开源组件完成HTTP URL请求处理的feign.Client 客户端实现类;github -
OkHttpClient
类:内部使用OkHttp3
开源组件完成HTTP URL请求处理的feign.Client 客户端实现类。web
@ConditionalOnClass({ ILoadBalancer.class, Feign.class }) @ConditionalOnProperty(value = "spring.cloud.loadbalancer.ribbon.enabled", matchIfMissing = true) @Configuration(proxyBeanMethods = false) @AutoConfigureBefore(FeignAutoConfiguration.class) @EnableConfigurationProperties({ FeignHttpClientProperties.class }) @Import({ HttpClientFeignLoadBalancedConfiguration.class, OkHttpFeignLoadBalancedConfiguration.class, DefaultFeignLoadBalancedConfiguration.class }) public class FeignRibbonClientAutoConfiguration { ... }
在前面一节内容中咱们看到Feign默认客户端实现 HttpURLConnnection
性能不是很好,与Dubbo RPC的性能相差很大。基于 HttpURLConnnection
的测试结果以下:spring

聚合报告apache
平均响应时间 | 吞吐量 | 最小响应时间 | 最大响应时间 |
---|---|---|---|
6866ms | 59.5/sec | 3056ms | 12232ms |
本章内容咱们须要对Feign的全部客户端进行性能测试,以此来肯定选择一个最优的客户端调用工具。tomcat
测试工具
测试服务器:Intel Core i5-7200U CPU @ 2.50GHz 2.70GHz 6核 16G内存服务器
测试工具:JMeter5.1微信
线程数:1000架构
Ramp-Up : 10

JMeter测试工具
「ps : 本文中出现的全部性能测试结果我都至少测试过10遍以上,最后选取的是一个相对平均的结果,测试结果相对仍是比较准确的。」
HttpClient
首先咱们先将客户端工具切换到HttpClient,查看HttpClientFeignLoadBalancedConfiguration配置类,源码以下
@Configuration(proxyBeanMethods = false) @ConditionalOnClass(ApacheHttpClient.class) @ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true) @Import(HttpClientFeignConfiguration.class) class HttpClientFeignLoadBalancedConfiguration { @Bean @ConditionalOnMissingBean(Client.class) public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory, HttpClient httpClient) { ApacheHttpClient delegate = new ApacheHttpClient(httpClient); return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory); } }
从代码 @ConditionalOnClass({ApacheHttpClient.class})
注解可知,只须要在pom文件上加上 HttpClient
依赖便可。另外须要在配置文件中配置 feign.httpclient.enabled
为 true
,从@ConditionalOnProperty
注解可知,这个配置能够不写,由于在默认状况下就为true。
因此要使用HttpClient咱们只须要在消费者模块 order-service
引入httpclient依赖便可:
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency>
测试结果

聚合报告
平均响应时间 | 吞吐量 | 最小响应时间 | 最大响应时间 |
---|---|---|---|
8390ms | 48.5/sec | 2691ms | 20371ms |
在高并发下性能测试竟然比不过原生的 HttpURLConnnection
,有点点失望!
OkHttp
一样先查看okhttp的配置类 OkHttpFeignLoadBalancedConfiguration
@Configuration(proxyBeanMethods = false) @ConditionalOnClass(OkHttpClient.class) @ConditionalOnProperty("feign.okhttp.enabled") @Import(OkHttpFeignConfiguration.class) class OkHttpFeignLoadBalancedConfiguration { @Bean @ConditionalOnMissingBean(Client.class) public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory, okhttp3.OkHttpClient okHttpClient) { OkHttpClient delegate = new OkHttpClient(okHttpClient); return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory); } }
查看注解咱们知道要使用OkHttp必须知足两个条件:
-
必须知足
OkHttpClient.class
在当前类路径中存在,即引入相应依赖 -
必需要配置
feign.okhttp.enabled
配置项的值为true
因此这里我先引入OkHttp的依赖:
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency>
而后修改配置文件,开启OkHttp:
feign: ... okhttp: enabled: true
测试结果

聚合报告
平均响应时间 | 吞吐量 | 最小响应时间 | 最大响应时间 |
---|---|---|---|
5335ms | 66.3/sec | 1874ms | 9011ms |
三个客户端中性能最好的!
测试结果分析
经过上面测试结果(默认配置)咱们很明显能够得出如下两个结论:
-
OKHttp性能优于其余两种,推荐使用!
-
在高并发状况下
Apache httpclient
效率甚至尚未默认的HttpURLConnection
效率高。且在压测过程当中,发现使用链接池请求卡顿现象很容易出现,apache httpclient
甚至还出现请求卡死状况。httpclient最不推荐使用。
容器优化
Undertow
是一个用java编写的灵活的高性能Web服务器,提供基于NIO的阻塞和非阻塞API。相比于 tomcat
,Undertow
的性能更高,更轻量。借此机会咱们恰好看看 Undertow
加 OkHttp
的测试效果。
首先咱们须要引入undertow的依赖并排除tomcat的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
测试效果

聚合报告
平均响应时间 | 吞吐量 | 最小响应时间 | 最大响应时间 |
---|---|---|---|
3817ms | 71.7/sec | 1830ms | 6174ms |
经过测试效果能够看出,使用了undertow容器后,性能又有了小小的提高。
总结
本文中的全部测试结论都是基于组件的默认配置,对于那些追求性能而又不想对参数进行调优的能够考虑 OkHttp + Undertow
的组合,虽然没办法与dubbo等rpc协议性能相比,可是在springcloud架构中的http调用,这二者之间的组合性能最高。
最后为了方便你们直观比较,将Feign原生 HttpURLConnnection
的测试结果和基于 OkHttp + Undertow
的测试结果放一块儿供你们参考:
-
HttpURLConnnection
平均响应时间 | 吞吐量 | 最小响应时间 | 最大响应时间 |
---|---|---|---|
6866ms | 59.5/sec | 3056ms | 12232ms |
-
OkHttp + Undertow
平均响应时间 | 吞吐量 | 最小响应时间 | 最大响应时间 |
---|---|---|---|
3817ms | 71.7/sec | 1830ms | 6174ms |
若是本文对你有帮助,别忘记来个三连:点赞,转发,评论。我们下期见!
本文分享自微信公众号 - JAVA日知录(javadaily)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。