【问题标题】:Camel REST - Path based routingCamel REST - 基于路径的路由
【发布时间】:2018-12-12 09:50:58
【问题描述】:

我必须使用基于路径的路由开发 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

如何摆脱center?我不想在不同的端口上部署 3 条骆驼路线。

谢谢

【问题讨论】:

    标签: rest apache-camel apache-servicemix


    【解决方案1】:

    认为这实际上更容易一些。只要你不挂在netty上并且你使用的是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&amp;throwExceptionOnFailure=false&amp;urlRewrite=#myRewrite" />
            </route>
        </camelContext>
    </blueprint>
    

    但是不支持netty,所以我选择了下一个最好的东西。

    当你调用http://localhost:9090/proxy/north/foo时,重写实际上会将调用的url更改为http://north.acme.com/flowdoc2/rest/foo

    对此有一些注意事项。首先,您必须使用 UrlRewrite 支持的组件之一。其次,您似乎必须在 classpath 中有重写配置文件 - 所以没有仅蓝图的路线。第三:我没有测试它,但我认为你明白了要点。我将此作为社区 wiki 答案,以便比我更有能力的其他人可以扩展此答案。

    【讨论】:

    • 我找到了一个类似于你的解决方案,虽然不优雅,但它操纵标题以便从 CamelHttpUri 和 CamelHttpPath 中删除不相关的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    相关资源
    最近更新 更多