1、新增UserFeignClientFallbackFactory类,该类实现了FallbackFactory接口,重写create接口:
创新互联公司专注于做网站、网站制作、网页设计、网站制作、网站开发。公司秉持“客户至上,用心服务”的宗旨,从客户的利益和观点出发,让客户在网络营销中找到自己的驻足之地。尊重和关怀每一位客户,用严谨的态度对待客户,用专业的服务创造价值,成为客户值得信赖的朋友,为客户解除后顾之忧。
@Component public class UserFeignClientFallackFactory implements FallbackFactory{ private static final Logger LOGGER = LoggerFactory.getLogger(UserFeignClientFallackFactory.class); @Override public UserFeignClient create(Throwable throwable) { return new UserFeignClient() { @Override public Map findById(Integer userId) { UserFeignClientFallackFactory.LOGGER.info("===================fallback;reason was:",throwable); Map user = new HashMap<>(3); user.put("name","默认用户"); user.put("id","-1"); user.put("age","18"); return user; } }; } }
2、修改FeignClient类:
@FeignClient(name = "user",fallbackFactory = UserFeignClientFallackFactory.class) public interface UserFeignClient { @RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET) Map findById(@RequestParam("userId") Integer userId); }
使用@FeignClient的fallbackFactory属性设置回退逻辑。