通过 WSO2 ESB 代理在多个系统中搜索用户

Search a user in multiple systems through WSO2 ESB proxy

我只需要发送一个请求(可以是代理 url),它以用户 ID 作为输入并调用多个端点,return 聚合所有端点后的响应。

目前我正在使用代理服务实现来实现它,但 return 只是一个响应(可以来自任何端点)并且无法组合来自其他 systems/endpoints 的响应。虽然我可以在我的服务器控制台中看到来自另一个端点的响应。

下面是我目前实现的代码:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="ProxyTestTwoW"
       transports="http,https"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log/>
         <clone>
            <target>
               <sequence>
                  <property name="Application" value="Application1"/>
                  <property name="messageType"
                            value="application/xacml+json"
                            scope="axis2"
                            type="STRING"/>
                  <property name="Authorization"
                            expression="fn:concat('Basic ', base64Encode('username:password'))"
                            scope="transport"/>
                  <send>
                     <endpoint>
                        <address uri="localhost:8080/iiq/rest/identities/9000070"/>
                     </endpoint>
                  </send>
               </sequence>
            </target>
            <target>
               <sequence>
                  <property name="Application" value="Application2"/>
                  <property name="messageType"
                            value="application/xacml+json"
                            scope="axis2"
                            type="STRING"/>
                  <property name="Authorization"
                            expression="fn:concat('Basic ', base64Encode('username:password'))"
                            scope="transport"/>
                  <send>
                     <endpoint>
                        <address uri="http://localhost:8080/iiq/rest/identities/9000071"/>
                     </endpoint>
                  </send>
               </sequence>
            </target>
         </clone>
      </inSequence>
      <outSequence>
         <log level="full" description="">
            <property name="Component" expression="get-property('Application')"/>
         </log>
         <aggregate>
            <completeCondition>
               <messageCount min="2"/>
            </completeCondition>
            <onComplete expression="$body/*[1]">
               <property name="messageType"
                         value="application/xacml+json"
                         scope="axis2"
                         type="STRING"
                         description="messageType"/>
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
      <faultSequence>
         <log level="full" category="WARN"/>
      </faultSequence>
   </target>
   <description/>
</proxy>

结果: 在浏览器中点击代理 URL 后: - 只有一个响应如下:

<jsonObject><viewableIdentityAttributes><Email>Kevin.Mollo@companyb.com</Email><cn>Kevin Mollo</cn><Last Name>Mollo</Last Name><First Name>Kevin</First Name></viewableIdentityAttributes><listAttributes>First Name</listAttributes><listAttributes>Last Name</listAttributes><listAttributes>Email</listAttributes><listAttributes>cn</listAttributes></jsonObject>

在服务器日志中:

[2016-07-14 11:46:35,057]  INFO - LogMediator To: http://www.w3.org/2005/08/addr
essing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:3beaaf16-7b94-4e
08-a3e0-7605869572c7, Direction: response, Component = Application1, Payload: {"vi
ewableIdentityAttributes":{"Email":"Kevin.Mollo@companyb.com","cn":"Kevin Mollo"
,"Last Name":"Mollo","First Name":"Kevin"},"assignedRoles":[],"listAttributes":[
"First Name","Last Name","Email","cn"]}
[2016-07-14 11:46:35,057]  INFO - LogMediator To: http://www.w3.org/2005/08/addr
essing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:c878c7db-ad4c-49
46-94d6-75aebc75ad8e, Direction: response, Component = Application1, Payload: {"vi
ewableIdentityAttributes":{"Email":"Michelle.Lassauze@companyb.com","cn":"Michel
le Lassauze","Last Name":"Lassauze","First Name":"Michelle"},"assignedRoles":[],
"listAttributes":["First Name","Last Name","Email","cn"]}

正如我们所见,我从服务器日志中的两个端点都获得了响应。

根据分析,我觉得要使其正常工作并从两个端点获得响应,我们需要更改以下内容: 1. 如果我们有多个端点要调用,则使用调用中介而不是发送。 2. 使用 enrich mediator 存储响应。

我尝试了上述方法,但我无法获得预期的结果,因为我不确定在这种情况下如何使用 enrich mediator。

请帮我解决以上问题。

  • 克隆中介将用于将相同的消息副本发送到不同的端点。
  • 迭代中介器将用于将一条特定消息的消息块发送到不同的端点。
  • Enrich mediator 将用于更改消息(例如:删除内容、添加等)[1]

检查您的端点并检查它们是否提供正确的响应以使用聚合中介。

我在此处添加示例场景。您可以使用 ESB 包对其进行测试,以了解上述三个中介的用法。您只需启动 2 个 SimpleStockQuote 服务实例 [2] 并通过 SOAPUI 将消息发布到代理。

<proxy name="ScatterGatherProxy" startOnLoad="true" trace="disable" transports="https http">
    <description/>
    <target>
        <inSequence>
            <clone>
                <target>
                    <sequence>
                        <send>
                            <endpoint name="vendorA">
                                <address uri="http://localhost:9001/services/SimpleStockQuoteService/"/>
                            </endpoint>
                        </send>
                    </sequence>
                </target>
                <target>
                    <sequence>
                        <send>
                            <endpoint name="vendorB">
                                <address uri="http://localhost:9002/services/SimpleStockQuoteService/"/>
                            </endpoint>
                        </send>
                    </sequence>
                </target>
            </clone>
        </inSequence>
        <outSequence>
            <log level="full"/>
            <aggregate>
                <completeCondition>
                    <messageCount min="2"/>
                </completeCondition>
                <onComplete expression="//m0:return"
                    xmlns:m0="http://services.samples" xmlns:m1="http://services.samples/xsd">
                    <enrich>
                        <source clone="true" xpath="//m0:return[not(preceding-sibling::m0:return/m1:last &lt;= m1:last) and not(following-sibling::m0:return/m1:last &lt; m1:last)]"/>
                        <target type="body"/>
                    </enrich>
                    <send/>
                </onComplete>
            </aggregate>
        </outSequence>
    </target>
</proxy>

如果您需要进一步的帮助,请附上更多详细信息,例如您的服务回复。

[1] http://dilshanilive.blogspot.com/2016/03/enrich-mediator-wso2-esb.html

[2] https://docs.wso2.com/display/ESB490/Setting+Up+the+ESB+Samples#Starting%20the%20Axis2%20server