说明
当使用feign远程调用接口是,如果接口返回异常或者超时时,我们可以统一返回异常信息,这样调用者就不用再显式的try-catch处理异常了
开启接口fallback处理
想要使用fallback 处理接口异常须在配置文件开启
feign:
hystrix:
enabled: true
定义feign接口
在@FeignClient上加属性fallback,并将自定义的fallback类赋值给fallback
@FeignClient(name = "stockFeign", url = "http://stock-center.qd-aliyun-test-internal.haier.net", fallback = StockFeignFallback.class)
public interface StockFeign {
@PostMapping("/api/v1/transferorder/queryBatchImportTransferOrder11")
FeignResultDto<Page<TransferImportListOutDto>> queryBatchTransferOrder(@RequestBody PageQueryDto<TransferImportListInDto> pageQueryDto);
}
定义fallback类
说明:定义类需要被spring管理,需要实现feign接口
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.boot.param.FeignResultDto;
import com.test.boot.param.PageQueryDto;
import com.test.boot.param.TransferImportListInDto;
import com.test.boot.param.TransferImportListOutDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class StockFeignFallback implements StockFeign {
public FeignResultDto<Page<TransferImportListOutDto>> queryBatchTransferOrder(PageQueryDto<TransferImportListInDto> pageQueryDto) {
log.info("接口异常=========");
return new FeignResultDto<>(500, "接口异常", null);
}
}
标签:FeignClient,异常,接口,test,import,fallback,com
From: https://www.cnblogs.com/shareToAll/p/18070528