Ashok Koyi
Ashok Koyi

Reputation: 5417

Issue with hystrix fallback in spring cloud

When I try to fallback using HystrixCommand in spring cloud, the method proxy is not working. The exception thrown in the in the method is not caught by the hystrix command aspect.

I am facing this issue with Spring Cloud Hystrix integration. I'm using spring cloud 1.0.0.RC2 release. Here is a sample project to replicate the issue.

Is this an issue with the way I'm using the library?

https://github.com/thekalinga/hystrix-fallback.git

Upvotes: 0

Views: 2704

Answers (3)

Diego Rojas
Diego Rojas

Reputation: 244

Remember that the signature of your fallback method must be the same that your method annotated with @HystrixCommand. Optionally you can put an exception as parameter to add some business logic, but if you try to throw an exception in this method maybe your log will say something like "fallback method nameMethod failed". Its because Hystrys fallback is meant for get some default data or data stored in cache to improve a default behavior when your remote service is down.

Here you can read an awesome documentation to understand how Hystrix "think".

Hope it helps.

Upvotes: 0

Oreste
Oreste

Reputation: 456

If you are creating a fallback method just to take care of the error then probably it should be private, unless you want to expose it to the outside world.

Upvotes: 1

Dave Syer
Dave Syer

Reputation: 58124

I found 2 problems in your FallbackableService.

1) the fallbackMethod referred to a non-existent method;

2) the @HystrixCommand method (and the fallback) was not public.

This works:

@Service
class FallbackableService {

   @Autowired
   FallbackClient fallbackClient;

   @HystrixCommand(fallbackMethod = "fallback")
   public String ping() {
    return fallbackClient.ping();
   }

   public String fallback() {
    return "PONG: from fallback";
   }

}

Upvotes: 3

Related Questions