今天在调试微服务程序通过Feign调用外部服务时出现一个异常:


feign.FeignException: status 401 reading xxxxxx
 at feign.FeignException.errorStatus(FeignException.java:60)
 at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:89)
 at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:143)
 at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:77)
 at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:107)
 at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
 at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
 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.unsafeSubscribe(Observable.java:10327)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
 at rx.Observable.unsafeSubscribe(Observable.java:10327)
 at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
 at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
 at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
 at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
 at rx.Observable.unsafeSubscribe(Observable.java:10327)
 at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
 at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56)
 at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47)
 at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69)
 at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
 at java.util.concurrent.FutureTask.run(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)

经过搜索相关信息后,发现一篇文章中讲到这个问题:
Spring Cloud项目中通过Feign进行内部服务调用发生401\407错误无返回信息的问题,文章中给出了解决问题的方法:
添加如下依赖:

<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>8.18.0</version>
</dependency>

<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>8.18.0</version>
</dependency>

在application.properties配置文件中增加

feign.okhttp.enabled=true

或者

feign.httpclient.enabled=true

按照上述的方法,增加配置后,这个问题解决了。
但在运行之后又产生了另外一个异常:

feign.FeignException: status 401 reading xxxxxx; content:
{"message":"未认证,请在前端系统进行认证"}
 at feign.FeignException.errorStatus(FeignException.java:60)
 at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:89)
 at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:143)
 at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:77)
 at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:107)
 at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
 at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
 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.unsafeSubscribe(Observable.java:10327)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51)
 at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
 at rx.Observable.unsafeSubscribe(Observable.java:10327)
 at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41)
 at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
 at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
 at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
 at rx.Observable.unsafeSubscribe(Observable.java:10327)
 at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
 at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56)
 at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47)
 at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69)
 at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
 at java.util.concurrent.FutureTask.run(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)

我并不知道对端的微服务添加了安全认证的相关配置,导致我没有调用成功,这个得问对端服务安全配置才能解决问题。这里先记录一下遇到的问题。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐