There is an error in the call between springcloud services, HystrixRuntimeException: api timed-out and no fallback available

During the big data platform project, due to the micro-service architecture, there is a call problem between services. When doing a stress test, when the concurrency is about 50 per second, when service A calls service B, service A will have the following error:

After searching for the reason, it is because when the service is called, the interface annotated by FeignClient does not have an implementation class, that is, the callback logic cannot be found, so this error will be reported. Under normal circumstances, it should be specified

@FeignClient(value = "auth",fallback = AuthLoginClientFallback.class)
该写法:
/** * @ClassName: AuthLoginClientFallback 
    * @Description: Hystrix回调接口 
*/
 @Component 
public class AuthLoginClientFallback implements AuthLoginClient { 
@Override 
public ResponseResult<Object> getCorporationSecretKey(UserAndCorporationDTO request) { 
return ResponseResult.failure(ServiceResultConstant.OPERATION_FAILURE.getCode(),ServiceResultConstant.OPERATION_FAILURE.getMsg()); 
  } 
};
或者@FeignClient(value = "auth",fallbackFactory = AuthLoginClientFallback.class)

The writing method:

/**
* @ClassName: AuthLoginClientFallback
* @Description: Hystrix回调接口
*/
@Component
public class AuthLoginClientFallback implements FallbackFactory<AuthLoginClient> {

@Override
public AuthLoginClient create(Throwable throwable) {
return new AuthLoginClient() {
@Override
public ResponseResult<Object> getCorporationSecretKey(UserAndCorporationDTO request) {
return ResponseResult.failure(ServiceResultConstant.OPERATION_FAILURE.getCode(),ServiceResultConstant.OPERATION_FAILURE.getMsg());
}
};
}
}

  • fallback: Define a fault-tolerant processing class. When calling a remote interface fails or times out, the fault-tolerant logic of the corresponding interface will be invoked. The class specified by fallback must implement the interface marked by @FeignClient
  • fallbackFactory: factory class, used to generate fallback class examples, through this property we can implement common fault-tolerant logic for each interface and reduce repetitive code
  • ​​​​​​​

Of course, on the premise of these problems, the spring cloud circuit breaker must be open.

Guess you like

Origin blog.csdn.net/selectgoodboy/article/details/86602782