您的位置:首页 > 编程语言 > Java开发

spring cloud feign 常见问题解决办法

2017-09-07 00:00 836 查看

1、第一次访问失败原因

一般首次访问都会比较慢,因为spring的懒加载机制,需要实例化一些类。hystrix默认的超时时间为1秒,超过后,就会调用fallback代码。解决方案有一下三种:

1.1、加大hystrix的超时时间

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 100000


1.2、禁用hystrix超时时间

hystrix.command.default.execution.timeout.enabled: false


1.3、禁用feign的hystrix(不推荐)

feign.hystrix.enabled: false


2、client(消费者)启动失败原因

消费者一般和api接口定义不在一个package下,导致启动失败。解决办法引入api的package:

@SpringBootApplication(scanBasePackages= {"com.api"})
@EnableFeignClients(basePackages={"com.api"})


3、使用@RequestParam, @RequestBody 时需要注意的

请参考https://my.oschina.net/u/182501/blog/1532081。

4、Feign使用http client

Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP请求,没有连接池,但是对每个地址会保持一个长连接,即利用HTTP的persistence connection 。我们可以用Apache的HTTP Client替换Feign原始的http client, 从而获取连接池、超时时间等与性能息息相关的控制能力。Spring Cloud从Brixtion.SR5版本开始支持这种替换,首先在项目中声明Apache HTTP Client和feign-httpclient依赖:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>${feign-httpclient.version}</version>
</dependency>

配置application.yml

feign:
httpclient:
enabled: true #启用httpclient
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Cloud Feign Hystrix