4

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);
    }
}
prranay
  • 1,789
  • 5
  • 23
  • 33

7 Answers7

4

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.

David
  • 504
  • 5
  • 21
  • why separate class? – ses Dec 19 '18 at 20:33
  • @ses It is just a suggestion to separate the @ SpringBootApplication, @ Controller, and @ Service/@ Component, instead of all the code at one place. Just a coding best practice suggestion, as over the period of time there will lot of code at one place and difficult to manage. – David Jan 23 '19 at 21:13
1

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

Olivier Meurice
  • 554
  • 8
  • 17
1

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.

umbum
  • 31
  • 5
0

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.

Camilo
  • 439
  • 5
  • 13
0

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

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
emre avci
  • 11
  • 2
0

You can try this because of HystrixComman is aspect

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

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

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Sandhya
  • 98
  • 10