配置的fallback class也必须在FeignClient Configuration中实例化,否则会报java.lang.IllegalStateException: No fallback instance of type class异常。
@FeignClient(name = "hello", fallback = HystrixClientFallback.class) public interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes(); } public class HystrixClientFallback implements HystrixClient { @Override public Hello iFailSometimes() { return new Hello("fallback"); } } @Configuration public class FooConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } @Bean public HystrixClientFallback fb(){ return new HystrixClientFallback(); } …… }
feign:
hystrix:
enabled: true
# 说明:请务必注意,从Spring Cloud Dalston开始,Feign默认是不开启Hystrix的。否则断路器不会生效。
# 而,Spring Cloud Angel/Brixton/Camden中,Feign默认都是开启Hystrix的。无需设置该属性。