springCloud入门学习(十四):Hystrix查看回退原因

1、新增UserFeignClientFallbackFactory类,该类实现了FallbackFactory接口,重写create接口:

@Component
public class UserFeignClientFallackFactory implements FallbackFactory<UserFeignClient> {

    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<String, String> 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属性设置回退逻辑。

3、测试结果上同。




猜你喜欢

转载自blog.51cto.com/13593129/2470881