Spring Cloud, how to solve the problem Feign integration Hystrix first request failed

Spring Cloud in, Feign Ribbon and after integration Hystrix, problems may arise failure of the first call, how to solve this problem?

The cause of the problem

Hystrix default timeout is 1 second, if more than this time has not yet responded, will enter fallback code. The first request tend to be slower ( due to the Ribbon is lazy loaded on the first request, will start initialization related classes ), the response time may be more than 1 seconds. After know the reason, we summarize the solution. To feign an example, the following four solutions.

A method, set the timeout length Hystrix

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

  This configuration is changed to allow timeout Hystrix 5 seconds, this is the easiest way to think of, but a little palliative.

Second method, the timeout is disabled Hystrix

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

  The configuration for the disabled Hystrix timeout, is generally not recommended.

Method three, as Feign disabled Hystrix

Global disabled

feign.hystrix.enabled: false

  Simply disable feign a hystrix, which is a more extreme, unless some special scenes, not recommended.

Local disabled

Named microservice-provider-userFeign Client disabled Hystrix

@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();
  }
}

Method IV, Ribbon configuration hunger load (best)

  Dalston from the beginning, Ribbon support configuration eager load when you start to realize initialization Ribbon related classes.

ribbon:
  eager-load:
    enabled: true
    clients: client1, client2, client3

   Dalson previous version by a number of mechanisms to achieve Hack eager load, but the cost is slightly higher, is not recommended, there is not posted.

 
 

Guess you like

Origin www.cnblogs.com/wwct/p/12463116.html