【问题标题】:How add transformation to Camel http-balancer in Spring xml?如何在 Spring xml 中向 Camel http-balancer 添加转换?
【发布时间】:2017-04-01 06:51:36
【问题描述】:

我通过 Camel 制作 http 平衡器 (安装 JBoss Fuse 之前 http://www.jboss.org/products/fuse/download/

%fuse_dir%=c:\temp\jboss-fuse-6.2.1.redhat-084
%path%=%path%;%fuse_dir%\bin;

)

git clone https://github.com/mishin/http-balancer-camel.git
cd http-balancer-camel/smx-ws-examples-jboss-fuse-6.2.1
mvn clean install
fuse console

我们在 fuse 控制台中编写

JBossFuse:karaf@root> features:addurl mvn:com.fusesource.examples/ws-features/1.0-SNAPSHOT/xml/features
JBossFuse:karaf@root> features:install smx-ws-examples
JBossFuse:karaf@root> list | grep Examples
JBossFuse:karaf@root> log:Display

启动我们的测试服务 现在我们有 3 个服务:
http://localhost:9091/greeterProxy?wsdl
http://localhost:9090/greeter?wsdl
http://localhost:9090/greeterImpl?wsdl

所以我们建立平衡器

git clone https://github.com/mishin/http-balancer-camel.git
cd http-balancer-camel/camel-gateway
mvn -Djava.net.preferIPv4Stack=true camel:run

这么短的代码是
https://github.com/mishin/http-balancer-camel/blob/master/camel-gateway/src/main/resources/META-INF/spring/applicationContext.xml

    <camelContext trace="false" id="greeterGateway" xmlns="http://camel.apache.org/schema/spring">
  <route id="proxyRoute">
    <from uri="jetty:http://localhost:9092/greeterProxy?matchOnUriPrefix=true"/>
    <loadBalance>
      <failover>
        <exception>java.io.IOException</exception>
      </failover>
      <to uri="jetty:http://localhost:9090/greeterImpl?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
      <to uri="jetty:http://localhost:9090/greeter?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
      <convertBodyTo type="java.lang.String"/>
    </loadBalance>
  </route>
</camelContext>

我进行故障转移 http-balancer
所以如果我从网络浏览器调用
http://localhost:9092/greeterProxy?wsdl
比我得到的

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.greeter.examples.fusesource.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns3="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://examples.fusesource.com/greeter" name="ConcreteGreeterService" targetNamespace="http://impl.greeter.examples.fusesource.com/">
<wsdl:import location="http://localhost:9090/greeterImpl?wsdl=Greeter.wsdl" namespace="http://examples.fusesource.com/greeter"></wsdl:import>
<wsdl:binding name="ConcreteGreeterServiceSoapBinding" type="ns1:Greeter">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="greetMe">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="greetMe">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="greetMeResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="pingMe">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="pingMe">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="pingMeResponse">
<soap:body use="literal"/>
</wsdl:output>
<wsdl:fault name="PingMeFault">
<soap:fault name="PingMeFault" use="literal"/>
</wsdl:fault>
</wsdl:operation>
<wsdl:operation name="greetMeOneWay">

... 我需要转型 改变

<wsdl:output name="pingMeResponse">

<wsdl:output name="pingAnotherResponse">

我把它玩透了 简单的转换,例如

    <convertBodyTo type="java.lang.String" />
    <transform>
        <simple>${in.body.replaceAll("greet([A-Z])Response", "bar$1foo")}</simple>
    </transform>

完整代码为:

<camelContext trace="false" id="greeterGateway" xmlns="http://camel.apache.org/schema/spring">
    <route id="proxyRoute">
        <from uri="jetty:http://localhost:9092/greeterProxy?matchOnUriPrefix=true" />
        <loadBalance>
            <failover>
                <exception>java.io.IOException</exception>
            </failover>
            <to uri="jetty:http://localhost:9090/greeterImpl?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
            <to uri="jetty:http://localhost:9090/greeter?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
            <convertBodyTo type="java.lang.String" />
        </loadBalance>

        <convertBodyTo type="java.lang.String" />
        <transform>
            <simple>${in.body.replaceAll("greet([A-Z])Response", "bar$1foo")}</simple>
        </transform>

    </route>
</camelContext>

但它根本不起作用
当我调用http://localhost:9092/greeterProxy?wsdl
它不会取代

<wsdl:output name="pingMeResponse">

为什么?

【问题讨论】:

    标签: java spring http apache-camel load-balancing


    【解决方案1】:

    FailOverLoadBalancer 使用异步路由引擎,这意味着您的转换块不会命中响应。尝试使用直接端点并将转换代码放在单独的路由中(从直接-> 到码头-> 转换)。这应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多