• 首先两个参数都可以作为断容器使用,在抛出exception时,返回默认的配置数据
  • 其次两个参数不能同时使用
  • 区别:fallbackFactory可以识别到cause,也就是说如果需要得到错误原因,可以使用fallbackFactory,如下

fallbackFactory的使用

  • FeignClient
1
2
3
4
5
6
remote接口标注fallbackFactory,
/**
* @author 小五
*/
@FeignClient(contextId = "productFeign", value = ServiceNameConstants.PRODUCT_SERVICE,
fallbackFactory = ProductFeignFallbackFactory.class)
  • FeignClient实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
@Slf4j
@Component
public class ProductFeignFallbackImpl implements ProductFeign {

@Setter
Throwable cause;

@Override
public Response xxx(xxx) {
log.error("ProductFeign xxx Error: {}",xxx.toString(),cause);
return Response.failed(cause);
}
}
  • FeignClient的FallbackFactory
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @author 小五
*/
@Component
public class ProductFeignFallbackFactory implements FallbackFactory<ProductFeign> {

@Override
public ProductFeign create(Throwable cause) {
ProductFeignFallbackImpl productFeignFallback = new ProductFeignFallbackImpl();
productFeignFallback.setCause(cause);
return productFeignFallback;
}
}

fallback的使用就方便了,不需要加FallbackFactory,将参数fallbackFactory改成fallback