【问题标题】:Making call to Rest services in mule ESB在 mule ESB 中调用 Rest 服务
【发布时间】:2014-11-06 14:49:48
【问题描述】:

我制作了一个 web 应用程序,它调用 mule 服务器,它正在 8081 处监听 http 入站点以访问数据库。我正在成功访问数据库并收到一条消息。但是我想在从数据库中获取对象并运行一些操作后访问其他的休息服务。 我不知道如何实现这一点。

请举个例子。我正在发布我的流程。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:endpoint exchange-pattern="request-response" host="localhost" port="8081" method="POST" name="HTTP" doc:name="HTTP"/>
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" password="123456" database="Customer" doc:name="MySQL Configuration"/>
    <flow name="muleesbintegrationFlow1" doc:name="muleesbintegrationFlow1">
        <http:inbound-endpoint exchange-pattern="request-response"   path="crud" doc:name="HTTP" ref="HTTP"/>
        <set-session-variable variableName="input" value="#[message.inboundProperties.'http.query.params'.input]" doc:name="input"/>
        <set-session-variable variableName="cid" value="#[message.inboundProperties.'http.query.params'.cid]" doc:name="cid"/>
        <set-session-variable variableName="fname" value="#[message.inboundProperties.'http.query.params'.fname]" doc:name="fname"/>
        <set-session-variable variableName="lname" value="#[message.inboundProperties.'http.query.params'.fname]" doc:name="lname"/>
        <choice doc:name="ChoicerOfCrud">
            <when expression="#[sessionVars['input']== &quot;insert&quot;]">
                <db:insert config-ref="MySQL_Configuration" doc:name="insert">
                    <db:parameterized-query><![CDATA[INSERT INTO `Customer`.`customer` (`customer_id`, `fname`, `lname`) VALUES (#[sessionVars['cid']], #[sessionVars['fname']],  #[sessionVars['lname']]);]]></db:parameterized-query>
                </db:insert>
            </when>
            <when expression="#[sessionVars['input']]==&quot;update&quot;]">
                <db:update config-ref="MySQL_Configuration" doc:name="update">
                    <db:parameterized-query><![CDATA[UPDATE `Customer`.`customer`  set 
fname =  #[sessionVars['fname']],
lname=  #[sessionVars['lname']]
where customer_id = #[sessionVars['cid']];]]></db:parameterized-query>
                </db:update>
            </when>
            <when expression="#[sessionVars['input']==&quot;select&quot;]">
                <db:select config-ref="MySQL_Configuration" doc:name="selected">
                    <db:parameterized-query><![CDATA[SELECT * FROM Customer.customer where customer_id =#[sessionVars['cid']];]]></db:parameterized-query>
                </db:select>
            </when>
            <otherwise>
                <db:delete config-ref="MySQL_Configuration" doc:name="delete">
                    <db:dynamic-query><![CDATA[DELETE FROM `Customer`.`customer` WHERE `customer_id`=#[sessionVars['cid']];]]></db:dynamic-query>
                </db:delete>
            </otherwise>
        </choice>
    </flow>
</mule>

【问题讨论】:

  • 你的意思是你想在你的 crud 操作之后调用一个出站 http 端点吗?
  • 您的 Web 应用程序期望从您的 Mule 服务获得什么类型的结果? JSON? XML?

标签: mule mule-studio mule-component mule-module-jpa mule-cluster


【解决方案1】:

我建议使用 http:outbound-endpoint 来使用 REST 服务(GET、POST、PUT 或 DELETE)。在此示例中,您可以看到调用服务 GET 的流程:

<flow name="invoke-ws" doc:name="invoke-ws">
    <vm:inbound-endpoint exchange-pattern="request-response" path="vm-ws" doc:name="VM"/>
    <logger message="payload is: #[payload]" level="INFO" doc:name="Logger"/>
    <http:outbound-endpoint exchange-pattern="request-response" method="GET" address="http://ip:port/service/#[payload]" doc:name="HTTP">
        <set-property propertyName="Accept" value="application/json"/>
    </http:outbound-endpoint>
    <byte-array-to-string-transformer doc:name="Byte Array to String"/>
    <logger message="payload is: #[payload]" level="INFO" doc:name="Logger"/>        
</flow> 

如果服务是 SOAP over HTTP,我建议使用 http://www.mulesoft.org/documentation/display/current/Web+Service+Consumer

【讨论】:

  • 嘿,对不起@julio你没有得到我的问题。你看到我的流程了吗。我正在数据库中进行crud操作。在那次crud操作之后数据库返回对象。之后我想要我的数据库反对访问某些服务。但问题是我无法理解如何执行。我的第一个请求是针对使用 String mvc 制作的 Web 应用程序,我希望我的结果应该返回到该应用程序。请帮助我。
  • 当您在数据库中运行该操作时,您可以操作对象以转换为 json 或 xml 文档,从而更改有效负载。然后,您可以包含一个 vm transport : request-reponse 以调用提供的示例中的流程。
  • @Manoj 您上面的评论很难理解,您能否进一步澄清一下?您是否尝试使用数据库调用的结果发送给 Web 服务使用者?您不应该将数据库结果作为Payload 返回吗?
猜你喜欢
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
相关资源
最近更新 更多