問題描述:
最近項目進行springboot1.x 升級到2.x;升級之後,服務之間的調用出現fallback異常;仔細查看調用過程,feign客戶端請求正常,服務提供方接收和響應正常;feign客戶端響應接收異常(直接進入fallback具體異常:
Caused by: java.io.IOException: Incomplete output stream
);查看源碼發現,openfeign底層使用的是rt.jar的HTTPUrlConnection進行的請求,
//異常:
//sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1523)
// 源碼:
private synchronized InputStream getInputStream0() throws IOException {
if(!this.doInput) {
throw new ProtocolException("Cannot read from URLConnection if doInput=false (call setDoInput(true))");
} else if(this.rememberedException != null) {
if(this.rememberedException instanceof RuntimeException) {
throw new RuntimeException(this.rememberedException);
} else {
throw this.getChainedException((IOException)this.rememberedException);
}
} else if(this.inputStream != null) {
return this.inputStream;
} else {
if(this.streaming()) {
if(this.strOutputStream == null) {
this.getOutputStream();
}
this.strOutputStream.close();
if(!this.strOutputStream.writtenOK()) {
throw new IOException("Incomplete output stream"); // 1523行
}
}..........
// this.strOutputStream.writtenOK()方法
boolean writtenOK() {
return this.closed && !this.error;
}
根據源碼看,是最後的this.error = false 以及流關閉導致的。
我們的解決方案:
1. pom依賴:
<!-- 使用Apache HttpClient替換Feign原生httpclient -->
<!-- feign-httpclient內含Apache HttpClient -->
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-httpclient -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>11.0</version>
</dependency>
2:application.yml:
#feign客戶端配置
feign:
hystrix:
enabled: true
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
httpclient:
enabled: true
如果問題沒有得到解決, 可以加企鵝,
981233589留言一起研究討論學習!