今天在做一个从Feign客户端到service服务端的POST请求,service服务端Controller类中方法测试正常,但却在Feign客户端返回时报错,错误信息如下:
Caused by: feign.RetryableException: Incomplete output stream executing POST http://服务名/路径/方法名
at feign.FeignException.errorExecuting(FeignException.java:67)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104)
at
............异常太多省略...........
Caused by: java.io.IOException: Incomplete output stream
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1523)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at feign.Client$Default.convertResponse(Client.java:152)
at feign.Client$Default.execute(Client.java:74)
at org.springframework.cloud.sleuth.instrument.web.client.feign.TracingFeignClient.execute(TracingFeignClient.java:88)
at org.springframework.cloud.sleuth.instrument.web.client.feign.LazyTracingFeignClient.execute(LazyTracingFeignClient.java:54)
at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:89)
at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:55)
at com.netflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:104)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287)
at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:231)
at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:228)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286)
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber$1.call(OperatorRetryWithPredicate.java:127)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:79)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:45)
at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276)
at rx.Subscriber.setProducer(Subscriber.java:209)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.subscribe(Observable.java:10423)
at rx.Observable.subscribe(Observable.java:10390)
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443)
at rx.observables.BlockingObservable.single(BlockingObservable.java:340)
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112)
at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:63)
at org.springframework.cloud.sleuth.instrument.web.client.feign.TraceLoadBalancerFeignClient.access$001(TraceLoadBalancerFeignClient.java:36)
at org.springframework.cloud.sleuth.instrument.web.client.feign.TraceLoadBalancerFeignClient.lambda$execute$0(TraceLoadBalancerFeignClient.java:50)
at org.springframework.cloud.sleuth.instrument.web.client.feign.TracingFeignClient.execute(TracingFeignClient.java:88)
at org.springframework.cloud.sleuth.instrument.web.client.feign.LazyTracingFeignClient.execute(LazyTracingFeignClient.java:54)
at org.springframework.cloud.sleuth.instrument.web.client.feign.TraceLoadBalancerFeignClient.execute(TraceLoadBalancerFeignClient.java:50)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:97)
... 77 common frames omitted
心想居然IO异常啦,但service端能正确返回数据啊,所以故障先锁定在Feign客户端服务上。根据异常调用链得知最终发生车祸的是sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1523)
HttpURLConnection.getInputStream0方法1523行。经过搜索资料,原来feign采用jdk原生HttpURLConnection向下游服务发起http请求(源码详见feign.Client.Default),先到getInputStream0方法看看:
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
是true导致的,也就是说在OutputStream写入流时出错。
没办法,只能用Apache HttpClient替换掉原生HttpURLConnection。加入依赖:
com.netflix.feign
feign-httpclient
8.17.0
然后在application.yml中添加如下:
feign:
httpclient:
enabled: true
重新调用,一切恢复正常。噢耶!
另外补充一点:
引入Apache HttpClient包时,需要注意指定Content-Type,不然会产生以下异常:
Caused by: java.lang.IllegalArgumentException: MIME type may not contain reserved characters
解决办法 :
@RequestMapping(value = "/getUser", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
注解中指定: Content-Type 即 指定 consumes 的属性值
feign通过jdk中的HttpURLConnection向下游服务发起http请求
更换Feign默认使用的HTTP Client
spring cloud 使用feign 遇到问题