@Component public class HystrixClientFactory implements FallbackFactory<UserFeignClient> { private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFactory.class); @Override public UserFeignClient create(Throwable cause) { HystrixClientFactory.LOGGER.info("the provider error is: {}", cause.getMessage()); return new UserFeignClient() { @Override public User getUser(Long id) { User user = new User(); user.setName("王五"); return user; } }; } }
我们可以看到,在这个create的工厂方法中,它的入参就是服务提供者的异常,得到了这个异常以后才会去做实现。这样是不是更加灵活了呢?
GitHub:https://github.com/2388386839/spring-cloud-demo
码云:https://gitee.com/zhixiang_blog/spring-cloud-demo
如果对您有所帮助,请记得帮忙点一个star哦