Feign + Ribbon request interception AFTER target host is choosen

核能气质少年 提交于 2019-12-24 02:07:22

问题


What I'm currently doing (which is very simple and convenient way):

Feign.builder()
.client(RibbonClient.create())
...
.requestInterceptor(new MyInterceptor())

But interception occur before ribbon actually resolve target host. Problem is, that one header that I want to add, have to be created based on the name of the target host.

Is there anyway I can manipulate headers after host is resolved?


回答1:


I have found following solution for this problem. Instead of using Feign interceptor I use RibbonClient delegate:

Feign.builder()
.client(RibbonClient.builder().delegate(new MyDelegate())
...

MyDelegate extends feign.Client.Default class and overrides public Response execute(Request request, Request.Options options) method.

In this way I can access target host by URI.create(request.url()).getHost().

Then I create new Request, add my header and run super.execute(newRequest, options) as the last instruction.



来源:https://stackoverflow.com/questions/45962233/feign-ribbon-request-interception-after-target-host-is-choosen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!