关于OpenFeign添加拦截器导致的null指针异常

早上发布微服务的一个节点,上线后运营人员反馈页面崩溃了,赶紧回滚服务,通过后台日志查看到是因为有空指针抛出:

java.lang.NullPointerException
	at com.xxx.api.customer.config.FeignConfiguration.apply(FeignConfiguration.java:23)
	at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:176)
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:101)
	at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:80)
	at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:109)
	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(Executors.java:511)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

通过对异常日志的分析知道,这个是我在服务的一个节点上添加的Feign拦截器中抛出空指针异常,这个拦截器主要目的是为了将request的请求头进行转发到其他的服务节点:
关于OpenFeign添加拦截器导致的null指针异常_第1张图片
通过debug进去可以知道RequestContextHolder是通过ThreadLocal保存请求上下文信息的,但是feign与当前的请求并不在一个线程中,因此通过截图的方法获取的属性信息会为空,所以抛出空指针信息。
解决方案:
1、粗暴一点,再不修改任何配置的情况下,加一个null判断,为空就不向下一个节点传递头部信息了。
2、上面的解决方案并不是我们想要的,因为目的是为了向下一个节点传递头部信息才加的这个拦截器,而且目前看来是肯定获取不到头部信息。由于我在feign中开启了hystrix做服务降级和熔断处理,通过查阅资料可以知道,hystrix的资源隔离有两种方式:线程池隔离和信号量隔离,其中线程池隔离表示在发送请求到其他服务时会新开启一个线程,那么这种方式就获取不到当前线程的上下文信息;信号量隔离是请求其他服务与当前线程使用同一个线程,那么就可以获取到请求的上下文信息,我们可以通过配置修改模式:

# hystrix配置
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: SEMAPHORE
          thread:
            timeoutInMilliseconds: 6000

你可能感兴趣的:(随笔,java,spring,boot)