Spring Cloud - 如何解决Feign/Ribbon第一次请求失败的问题
2019-07-13 / 1 min read
造成该问题的原因?
Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(由于Ribbon是懒加载的,在首次请求时,才会开始初始化相关类),这个响应时间可能就大于1秒了。
1. 将Hystrix的超时设长
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
2. 禁用Hystrix超时
hystrix.command.default.execution.timeout.enabled: false
3. 为Feign禁用Hystrix
全局禁用
feign.hystrix.enabled: false
局部禁用
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@GetMapping("/users/{id}")
User findById(@PathVariable("id") Long id);
}
class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder(){
return Feign.builder();
}
}
4. Ribbon配置饥饿加载
ribbon:
eager-load:
enabled: true
clients: client1, client2, client3