FallbackDefinitionException: fallback method wasn’t found: error handling
I encountered a problem today when I was learning hystrix: when I used feign to call other services that were not started, there was a problem of fallback method not found. The code is as follows
@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET) @HystrixCommand(fallbackMethod = "error") public Product findById(@PathVariable Long id) { Product product = feignService.findById(id); return product; } public String error() { return "Service is starting, please wait"; } //FallbackDefinitionException: fallback method wasn't found: error
Finally, it was found that the method-level fuse processing using @HystrixCommand requires the return type and parameters of the current method and the callback method to be consistent. The following is the modified version.
@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET) @HystrixCommand(fallbackMethod = "error")//Fusing downgrade based on method, the error callback method must be consistent with the current method (return value and parameters) public Product findById(@PathVariable Long id) { Product product = feignService.findById(id); return product; } public Product error(Long id) { Product product=new Product(); return product; } //The output content is {"id":null,"productName":null,"status":null,"price":null,"productDesc":null,"caption\ ":null,"inventory":null}
The annotation is also explained, the ctrl key looks like this
/** * Specifies a method to process fallback logic. * A fallback method should be defined in the same class where is HystrixCommand. * Also a fallback method should have same signature to a method which was invoked as hystrix command. * for example: * <code> * @HystrixCommand(fallbackMethod = "getByIdFallback") * public String getById(String id) {...} * * private String getByIdFallback(String id) {...} * </code> * Also a fallback method can be annotated with {@link HystrixCommand} * <p/> * default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()} * * @return method name */ String fallbackMethod() default "";
so,
go to bed