FeignClient伺服器丟擲異常客戶端處理辦法
阿新 • • 發佈:2021-01-15
FeignClient伺服器丟擲異常客戶端處理辦法
在使用feign進行遠端方法呼叫時,如果遠端服務端方法出現異常,客戶端有時需要捕獲,並且把異常資訊返回給前端,而如果在開啟熔斷
之後,這個異常會被消化,所以說,如果希望拿到服務端異常,feign.hystrix.enable
需要設定為false,而當不開熔斷時,我們也有幾種方法把拿到服務端的異常資訊,下面總結一下。
- feign異常攔截器
註冊一個Bean物件,當feign調用出現異常的時候,會觸發這個方法:
import com.test.JsonUtils;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import io.test.BadRequestException;
import io.test.InternalServerErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static feign.FeignException.errorStatus;
/**
* @author 飄逝才子
* @date 2020/11/05
* @description
*/
@Configuration
public class FeignClientErrorDecoder implements ErrorDecoder {
private Logger logger = LoggerFactory.getLogger(FeignClientErrorDecoder.class);
@Override
public Exception decode(String methodKey, Response response) {
Map<String, Object> jsonBody = new HashMap<>();
jsonBody.put("message", "Internal server error");
try {
String body = Util.toString(response.body().asReader());
jsonBody = JsonUtils.toMap(body);
} catch (IOException e) {
logger.error("feign.IOException", e);
}
assert jsonBody != null;
if (response.status() >= 400 && response.status() < 500) {
throw new BadRequestException(jsonBody.get("message").toString());
}
if (response.status() >= 500) {
throw new InternalServerErrorException(jsonBody.get("message").toString());
}
return errorStatus(methodKey, response);
}
}
注意,使用這個方式,需要在熔斷器關閉時才起作用,因為它們的執行過程是,先走這個攔截器,再走熔斷的fallback,所以這個異常會被熔斷吞掉,返回狀態為200,返回值為你的fallback的預設值。