Feignレポート異常IOException Incomplete output stream

8009 ワード

今日、Feignクライアントからサービスサービス側へのPOSTリクエストを行いました.サービス側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が異常だと思っていたが、サービス側が正しくデータを返すことができたので、フェイルオーバはFeignクライアントサービスにロックされていた.異常呼び出しチェーンによると、最終的に交通事故が発生したのはsun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1523) HttpURLConnectionであることが分かった.getInputStream 0メソッド1523行.検索資料によると、元のfeignはjdkオリジナルHttpURLConnectionを使用して下流サービスにhttpリクエストを開始した(ソースコードはfeign.Client.Defaultを参照).まずgetInputStream 0メソッドを見てみましょう.
    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
        

そしてアプリケーションで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を使用すると問題が発生します