我必须使用基于路径的路由来开发Camel REST路由。 该场景如下:我们有一个业务合作伙伴,它提供了REST Web服务来显示文档。 REST Web服务根据地理位置部署在3台不同的服务器上。 所以我们基本上有3个这样的服务器:
http://north.acme.com/flowdocv2/rest/repository/attachment/{id}/findById
http://center.acme.com/flowdocv2/rest/repository/attachment/{id}/findById
http://south.acme.com/flowdocv2/rest/repository/attachment/{id}/findById
我的目的是开发一条单骆驼路线来映射这些服务器,并在路径中接受服务器的名称。像这样:
http://my.camel.com/center/repository/attachment/{id}/findById
http://my.camel.com/north/repository/attachment/{id}/findById
http://my.camel.com/south/repository/attachment/{id}/findById
我的(简化且不起作用的)blueprint.xml:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<cm:property-placeholder persistent-id="my.config.file"/>
<reference id="sharedNettyHttpServer" interface="org.apache.camel.component.netty4.http.NettySharedHttpServer"/>
<camelContext id="my_context" xmlns="http://camel.apache.org/schema/blueprint">
<restConfiguration component="netty4-http">
<endpointProperty key="nettySharedHttpServer" value="#sharedNettyHttpServer"/>
</restConfiguration>
<rest path="/center/repository">
<get uri="/attachment/{attachmentId}/findById">
<route streamCache="true" trace="true">
<to uri="http://center.acme.com/flowdocv2/rest?bridgeEndpoint=true"/>
</route>
</get>
</rest>
<rest path="/north/repository">
<get uri="/attachment/{attachmentId}/findById">
<route streamCache="true" trace="true">
<to uri="http://north.acme.com/flowdocv2/rest?bridgeEndpoint=true"/>
</route>
</get>
</rest>
</camelContext>
</blueprint>
问题是我不知道如何从路径中删除/ center,/ north或/ south,因此标头被转发到目标服务,而目标服务不知道如何处理。 调用:
http://my.camel.com/center/repository/attachment/{id}/findById
导致在目标服务器上调用以下URL:
http://center.acme.com/flowdocv2/rest/center/repository/attachment/{id}/findById
如何摆脱中心?我不想在不同的端口上部署3条骆驼路线。
谢谢
答案 0 :(得分:0)
我认为实际上要容易一些。只要您不依赖网络,并且使用的是Camel 2.11+,就可以使用camel-urlrewrite
基本上,您在配置中定义了一个重写规则,并将其添加到路由束中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
<name>Generic Proxy</name>
<note>
This rule completely rewrites the url to call.
Basically, in Camel's "to", you could write whatever you want
</note>
<from>^/(.*?)/(.*)</from>
<to>http://$1.acme.com/flowdocv2/rest/$2</to>
</rule>
</urlrewrite>
现在,您可以使用一条相当简单的路线:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<bean id="myRewrite" class="org.apache.camel.component.urlrewrite.HttpUrlRewrite">
<property name="configFile" value="class/path/to/proxyrewrite.xml" />
</bean>
<camelContext id="my_context" xmlns="http://camel.apache.org/schema/blueprint">
<route id="proxyRoute">
<from uri="jetty:http://localhost:9090/proxy" />
<to uri="jetty:http://somewhere/myapp2?bridgeEndpoint=true&throwExceptionOnFailure=false&urlRewrite=#myRewrite" />
</route>
</camelContext>
</blueprint>
但是,不支持netty,所以我选择了下一个最好的选择。
调用http://localhost:9090/proxy/north/foo
时,重写实际上会将URL更改为调用http://north.acme.com/flowdoc2/rest/foo
。
对此有一些警告。首先,您必须使用UrlRewrite支持的组件之一。其次,看来您必须在 classpath 中包含重写配置文件-因此,没有仅用于蓝图的路由。第三:我没有测试过,但是我想你的要点是。我将此设为社区Wiki答案,以便其他能力强于我的人可以扩展此答案。