prranay
prranay

Reputation: 1889

Hystrix fallback method is not invoked

I'm trying hystrix fallback method. On localhost:8082, customer-service is running which returns name of the customer.

If the customer-service is down, fallback method should invoke. But it is not happening.

Below is the code snippet.

Please suggest.

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
public class DemoHystrixApplication {

    @GetMapping("/")
    public String name() {
        String str = getCustomerName();
        return str;
    }

    @HystrixCommand(fallbackMethod = "getFallbackCustomerName")
    private String getCustomerName() {
        RestTemplate restTemplate = new RestTemplate();
        URI uri = URI.create("http://localhost:8082");
        return restTemplate.getForObject(uri, String.class);
    }

    private String getFallbackCustomerName() {
        System.out.println("coming inside fallback method");
        return "Resillient Customer";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoHystrixApplication.class, args);
    }
}

Upvotes: 5

Views: 7158

Answers (8)

Anurag
Anurag

Reputation: 21

Adding to @David ' s answer. Reason for moving these methods to separate class and it works is, "Proxy class". When we add fallback method, then Spring container creates a proxy class over our class. Spring has control over what to do in case of a fault. So when we break down our code into different methods, Spring lose the control, and hence the solution is to move that method into another bean.

Upvotes: 0

Sandhya
Sandhya

Reputation: 118

The @HystrixCommand annotated method should be public. The fallback method can be private as well.

Upvotes: 0

Bexzod Xayrullayev
Bexzod Xayrullayev

Reputation: 121

You can try this because of HystrixComman is aspect

@Bean
@Primary
@Order(value= Ordered.HIGHEST_PRECEDENCE)
public HystrixCommandAspect hystrixAspect() {
    return new HystrixCommandAspect();
}

Upvotes: 0

emre avci
emre avci

Reputation: 11

Fallback metod should be called from another bean. The problem is you are calling fallback method from RestController.

Upvotes: 0

umbum
umbum

Reputation: 31

This is because of AOP.

The Spring container injects aspect-aware bean when injecting a bean.

When the name() function is called at the user's request, the method of the aspect-aware bean is called so annotation works.

However, calling this.getCustomerName() directly within the name() calls getCustomerName() on the raw bean before it is wrapped in proxy. It doesn't know aspect. Therefore, annotation does not work.

Upvotes: 1

Camilo
Camilo

Reputation: 479

You can also try stopping and starting the service, if you added the dependency of netflix-hystrix and have dev-tools to pick up changes while executing the service.

Upvotes: 0

David
David

Reputation: 514

Both the methods i.e. the actual and fallback methods should be public and move these methods to a separate class and annotate it with @Component.

Give a try, hope this helps.

Upvotes: 5

Olivier Meurice
Olivier Meurice

Reputation: 562

Your @HystrixCommand annotated method should be public. Not sure about the fallback method but I would set it public as well.

Upvotes: 1

Related Questions