1.话题一:Feign中Hystrix-Factory的使用
- 1.Hystrix-Factory类比断路器的一种日志打印,但是和fallback=ConsumeFeignHystrix.class冲突
- 2.创建一个类实现FallbackFactory,范型是Fegin,然后在实现的时候,返回我们的默认值
@FeignClient(name="server-provider",fallbackFactory=ConsumeFeignHystrixFactory.class)
public interface ConsumeFeign {
@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id); // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
}
@Component
// 进行HystrixFactory的实现,返回的参数中,自己对自己的方法进行了实现
class ConsumeFeignHystrixFactory implements FallbackFactory<ConsumeFeign>{
private static Log log = LogFactory.getLog(ConsumeFeignHystrixFactory.class);
@Override
public ConsumeFeign create(Throwable cause) {
log.info("---------------------进入日志的打印方法:"+cause.getMessage());
ConsumeFeign consumeFeign = new ConsumeFeign() {
@Override
public User findById(Long id) {
User user = new User(id, "DEFAULT_NAME");
log.info("---------------------返回错误的默认数据:"+user.toString());
return user;
}
};
return consumeFeign;
}
}
- 3.fallbackFactory和fallBack对比:
- 3.1 fallBack只能返回默认的值
- 3.2 可以结合日志打印,具体化日志的报错原因,功能更强大一点