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);
}
}
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.