Search code examples
microservicesnetflix-eurekanetflix-feignspring-cloud-feignfeign

Fallback method is not being called when rest call is failed by using feign client


I am trying to implement fallback by using Feign client but not getting success.Its a simplest code Please find below.

Main Class

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class EurekaClient1Application {

    @Autowired
    public DiscoveryClient discoveryClient;

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

    @Autowired 
    FeingInterface feingInterface;




    @GetMapping("/hi/{name}")
    public String test(@PathVariable String name)
    {
        String h = feingInterface.test(name);

        return h;
    }
}

Feign interface

@FeignClient(name="client22",fallback=FallBack.class)
public interface FeingInterface {

    @GetMapping("/hiname/{name}")
    public String test(@PathVariable("name") String name);

}

fallback class

@Component
class FallBack implements FeingInterface{

    @Override
    public String test(String name) {
        // TODO Auto-generated method stub
        return "fall back methord being called";
    }

}

Getting Error in rest client but not from fallback method

"timestamp": 1501950134118, "status": 500, "error": "Internal Server Error", "exception": "java.lang.RuntimeException", "message": "com.netflix.client.ClientException: Load balancer does not have available server for client: client22",

To get the fallback method message I passed client22 eureka id which is not there in eureka server. I have stater-feign in pom. Can someone look into this.


Solution

  • Fallbacks are actually not handled by Feign itself, but by a circuit breaker. So, you need to put Hystrix (which is the Netflix circuit breaker) on your classpath and enable it in your application.yml file like this:

    feign:
      hystrix:
        enabled: true
    

    If you're using 'cloud:spring-cloud-starter-openfeign' in your build.gradle or pom.xml file, Hystrix should be automatically on your classpath.