问题描述
是否可以将所有请求直接路由到另一台服务器?例如,将所有此项目特定的休息端点 localhost:8080/get-something 路由到另一个项目端点,如下所示: someIp:8081/get-something2 .像这样:
Is it possible to route all of request to another server directly? For example route all of this project specific rest endpoint localhost:8080/get-something to another project endpoint like this: someIp:8081/get-something2 . something like this:
from("localhost:8080/get-something")
.to("someIp:8081/get-something2")
或者这个:
rest()
.path("/get-something")
.get()
.route()
.to("someIp:8081/get-something2")
我尝试了太多方法,但我做不到!
I've tried too many ways but I cant!
推荐答案
您可以使用基于 http 的组件(jetty 或 undertow)作为消费者(来自)并带有 matchOnUriPrefix=true
选项,然后发送它使用 bridgeEndpoint=true
选项到 http 组件.
You could use a http based component (jetty or undertow) as consumer (from) with matchOnUriPrefix=true
option and then send it to an http component using bridgeEndpoint=true
option.
示例:
from("undertow:http://localhost:8080/?matchOnUriPrefix=true")
.to("http4://google.com/?bridgeEndpoint=true");
这样任何发送到 localhost:8080/的请求都会被转发到 google.com/
That way any request sent to localhost:8080/ will be forwarded to google.com/
在浏览器中尝试 http://localhost:8080/,您将获得 google 网页.
Try http://localhost:8080/ in your browser and you will get google web page.
在浏览器中尝试 http://localhost:8080/search?q=camel ,您将收到camel"的响应;搜索.
Try http://localhost:8080/search?q=camel in your browser and you will get response for "camel" search.
在你的情况下,你可以这样做:
In your case you could do:
from("undertow:http://localhost:8080/?matchOnUriPrefix=true")
.to("http4://localhost:8081/?bridgeEndpoint=true");
文档:
- https://camel.apache.org/components/latest/http-component.html
- https://camel.apache.org/components/latest/undertow-component.html
- https://camel.apache.org/manual/latest/faq/how-do-i-let-jetty-match-wildcards.html
这篇关于Apache camel 将一个 URI 路由到另一个 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!