选择目标主机后伪装+功能区请求拦截 [英] Feign + Ribbon request interception AFTER target host is choosen
问题描述
我目前正在做什么(这是非常简单方便的方式):
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?
推荐答案
我找到了针对此问题的以下解决方案.而不是使用Feign拦截器,而是使用RibbonClient委托:
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
扩展了feign.Client.Default
类并覆盖了public Response execute(Request request, Request.Options options)
方法.
MyDelegate
extends feign.Client.Default
class and overrides public Response execute(Request request, Request.Options options)
method.
通过这种方式,我可以通过URI.create(request.url()).getHost()
访问目标主机.
In this way I can access target host by URI.create(request.url()).getHost()
.
然后我创建新的请求,添加标题并运行super.execute(newRequest, options)
作为最后一条指令.
Then I create new Request, add my header and run super.execute(newRequest, options)
as the last instruction.
这篇关于选择目标主机后伪装+功能区请求拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!