Feign調(diào)用接口解決處理內(nèi)部異常的問題
問題描述:
當使用feign調(diào)用接口,出現(xiàn)400~500~的接口問題時。會出錯feign:FeignException。(因為是錯誤,只能用catch Throwable,不可使用catch Exception捕獲異常)導(dǎo)致程序無法繼續(xù)運行。
問題原因:
由于feign默認的錯誤處理類是FunFeignFallback會throw new AfsBaseExceptio導(dǎo)致外部無法捕獲異常。
package com.ruicar.afs.cloud.common.core.feign;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import feign.FeignException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.lang.Nullable;
import java.lang.reflect.Method;
import java.util.Objects;
@Data
@AllArgsConstructor
@Slf4j
public class FunFeignFallback<T> implements MethodInterceptor {
private final Class<T> targetType;
private final String targetName;
private final Throwable cause;
private static byte JSON_START = '{';
@Nullable
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
String errorMessage = cause.getMessage();
if (!(cause instanceof FeignException)) {
log.error("FunFeignFallback:[{}.{}] serviceId:[{}] message:[{}]", targetType.getName(), method.getName(), targetName, errorMessage);
log.error("feign調(diào)用失敗", cause);
return IResponse.fail("請求失敗,請稍后再試");
}
int status = ((FeignException.FeignClientException) this.cause).status();
boolean isAuthFail = (status==426||status==403||status==401)&&"afs-auth".equals(targetName);
FeignException exception = (FeignException) cause;
if(isAuthFail){
log.warn("授權(quán)失敗==========原始返回信息:[{}]",exception.contentUTF8());
}else {
log.error("FunFeignFallback:[{}.{}] serviceId:[{}] message:[{}]", targetType.getName(), method.getName(), targetName, errorMessage);
log.error("", cause);
log.error("原始返回信息{}",exception.contentUTF8());
}
if(method.getReturnType().equals(Void.class)){
throw new AfsBaseException("接口調(diào)用失敗");
}
if(method.getReturnType().equals(IResponse.class)){
if(exception instanceof FeignException.Forbidden){
return IResponse.fail("沒有權(quán)限").setCode("403");
}
if(exception instanceof FeignException.NotFound){
return IResponse.fail("請求路徑不存在").setCode("404");
}
if(exception instanceof FeignException.BadRequest){
return IResponse.fail("參數(shù)錯誤").setCode("400");
}
if(exception.content()==null||exception.content().length==0){
return IResponse.fail("請求失敗,請稍后再試");
}
if(JSON_START==exception.content()[0]){
return JSONObject.parseObject(exception.content(),IResponse.class);
}else{
return IResponse.fail(exception.contentUTF8());
}
}else{
try {
if(method.getReturnType().equals(String.class)){
return exception.contentUTF8();
}else if(method.getReturnType().equals(JSONObject.class)){
if(JSON_START==exception.content()[0]){
return JSONObject.parseObject(exception.content(), JSONObject.class);
}
}else if(!method.getReturnType().equals(Object.class)){
return JSONObject.parseObject(exception.content(), method.getReturnType());
}
if(JSON_START==exception.content()[0]){
JSONObject jsonObject = JSONObject.parseObject(exception.content(), JSONObject.class);
if(jsonObject.containsKey("code")&&jsonObject.containsKey("msg")) {
return jsonObject.toJavaObject(IResponse.class);
}
}
}catch (Throwable e){}
throw new AfsBaseException("接口調(diào)用失敗");
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FunFeignFallback<?> that = (FunFeignFallback<?>) o;
return targetType.equals(that.targetType);
}
@Override
public int hashCode() {
return Objects.hash(targetType);
}
}
問題解決:自定義feignFallback異常處理:
1.自定義異常處理 InvoiceApiFeignFallbackFactory
package com.ruicar.afs.cloud.invoice.factory;
import com.ruicar.afs.cloud.invoice.fallback.InvoiceApiFeignFallback;
import com.ruicar.afs.cloud.invoice.feign.InvoiceApiFeign;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class InvoiceApiFeignFallbackFactory implements FallbackFactory<InvoiceApiFeign> {
@Override
public InvoiceApiFeign create(Throwable throwable) {
InvoiceApiFeignFallback invoiceApiFeignFallback = new InvoiceApiFeignFallback();
invoiceApiFeignFallback.setCause(throwable);
return invoiceApiFeignFallback;
}
}
2.feign調(diào)用 InvoiceApiFeignFallbackFactory
package com.ruicar.afs.cloud.invoice.feign;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.common.core.feign.annotations.AfsFeignClear;
import com.ruicar.afs.cloud.invoice.dto.InvoiceCheckDto;
import com.ruicar.afs.cloud.invoice.factory.InvoiceApiFeignFallbackFactory;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import java.util.Map;
/**
* @description: 發(fā)票驗證接口
* @author: rongji.zhang
* @date: 2020/8/14 10:32
*/
@FeignClient(name = "invoice", url = "${com.greatwall.systems.invoice-system.url}" ,fallbackFactory = InvoiceApiFeignFallbackFactory.class)
public interface InvoiceApiFeign {
/**
*
* @param dto
* @return
*/
@ApiOperation("獲取業(yè)務(wù)數(shù)據(jù)API接口")
@PostMapping(value = "/vi/check")
@AfsFeignClear(true)//通過此注解防止添加內(nèi)部token
JSONObject InvoiceCheck(@RequestBody InvoiceCheckDto dto, @RequestHeader Map<String, String> headers);
}
3.實現(xiàn)自定義報錯處理
package com.ruicar.afs.cloud.invoice.fallback;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.invoice.dto.InvoiceCheckDto;
import com.ruicar.afs.cloud.invoice.feign.InvoiceApiFeign;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author Fzero
* @date 2019-01-24
*/
@Slf4j
@Component
public class InvoiceApiFeignFallback implements InvoiceApiFeign {
@Setter
private Throwable cause;
/**
* @param dto
* @param headers
* @return
*/
@Override
public JSONObject InvoiceCheck(InvoiceCheckDto dto, Map<String, String> headers) {
log.error("feign 接口調(diào)用失敗", cause);
return null;
}
}
Feign遠程調(diào)用失敗-----丟請求頭
@FeignClient("guli-cart")
public interface CartFenignService {
@GetMapping("/currentUserCartItems")
List<OrderItemVo> getCurrentUserCartItems();
}// 這樣去掉接口時其實Feign在底層是一個全新的requst所有請求頭就沒有了
解決辦法使用Feign遠程掉用攔截器,在遠程請求是先創(chuàng)建攔截器
@Bean("requestInterceptor")
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
/**
* 把以前的Cookie放到新請求中去 原理就是運用了同一線程數(shù)據(jù)共享 ThreadLocal
*/
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String cookie = request.getHeader("Cookie");
template.header("Cookie", cookie);
}
};
}
但是上面的辦法只能解決同意線程問題,在多線程下還是會丟失請求頭
多線程下解決辦法:
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
把請求單獨拿出來給每個線程單獨
RequestContextHolder.setRequestAttributes(requestAttributes);
這樣就可以了~
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實現(xiàn)
這篇文章主要介紹了Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java設(shè)計模式編程中的工廠方法模式和抽象工廠模式
這篇文章主要介紹了Java設(shè)計模式編程中的工廠方法模式和抽象工廠模式,設(shè)計模式的建立有利于團隊協(xié)作時代碼的共同維護,需要的朋友可以參考下2016-01-01
SpringBoot調(diào)用Poi-tl實現(xiàn)渲染數(shù)據(jù)并生成Word文檔
這篇文章主要為大家詳細介紹了SpringBoot如何調(diào)用Poi-tl實現(xiàn)渲染數(shù)據(jù)并生成Word文檔,文中的示例代碼講解詳細,有需要的小伙伴可以了解下2023-09-09
Java線程的start方法回調(diào)run方法的操作技巧
面試過程中經(jīng)常會被面試官問到為什么我們調(diào)用start()方法時會執(zhí)行run()方法,為什么不能直接調(diào)用run()方法,問的一頭霧水,今天小編給大家介紹下Java線程的start方法回調(diào)run方法的操作技巧,需要的朋友參考下吧2017-11-11
Springboot下RedisTemplate的兩種序列化方式實例詳解
這篇文章主要介紹了Springboot下RedisTemplate的兩種序列化方式,通過定義一個配置類,自定義RedisTemplate的序列化方式,結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
LinkedList學(xué)習(xí)示例模擬堆棧與隊列數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了LinkedList學(xué)習(xí)示例,模擬一個堆棧與隊列數(shù)據(jù)結(jié)構(gòu),大家參考使用吧2014-01-01
淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系
這篇文章主要介紹了淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java?GUI實現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了Java?GUI實現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

