SpringCloudAlibaba - Feign 的性能调优
前言
记录下Feign的性能调优方式,在微服务内部调用接口主要有RestTemplate和Feign方式来实现,其都是Http的
-
RestTemplate在微服务当中与注解@LoadBanlance注解结合使用, 可以通过服务名进行模块之间的接口调用 Feign默认集成了ribbon,用它也可以通过服务名进行模块之间的接口调用
RestTemplate 和 Feign
-
RestTemplate和 Feign的比较
Feign 的性能调优方式
配置连接池
默认情况下,feign使用urlconnection去请求,而urlconnection是没有连接池的,feign除了可以使用urlconnection去发送请求还支持使用apache的httpclient以及okhttp去发送请求
httpclient 连接池配置
-
pom.xml
<!-- httpclient -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
-
application.yml
# 自定义配置 feign 日志级别
feign:
httpclient:
# 使用 apache httpclient做请求,而不是默认的 urlconnection
enabled: true
# feign的最大连接数
max-connections: 200
# feign单个路径的最大连接数
max-connections-per-route: 50
okhttp 连接池配置
-
pom.xml
<!-- okhttp -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>10.1.0</version>
</dependency>
-
application.yml
feign:
httpclient:
# feign的最大连接数
max-connections: 200
# feign单个路径的最大连接数
max-connections-per-route: 50
okhttp:
# 使用 okhttp 做请求,
enabled: true
-
连接池的配置对性能的提升大概在15%左右
设置合适日志级别
-
feign的日志级别
-
feign默认不打印任何日志,生产环境建议设置日志级别为BASIC,仅记录请求方法、URL、响应状态代码及执行时间
