apache synapse使用(1)
一.Synapse介紹
Synapse 是一個簡單的 XML 和 Web 服務管理與集成代理,可用于構成 SOA 和企業服務總線(ESB)的基礎。Synapse是 Web 服務項目中一項成熟的 Apache 活動,并且是非常成功的 Apache Axis2 項目的一個分支。它提供了中介、管理、以及在各種不同的應用程序之間轉換 XML 消息的能力
看看官方給出的架構圖
二.官方示例
1,環境準備
- A Java 2 SE - JDK or JRE of version 1.5.x or higher (JDK 1.6.0_21 recommended)
- Apache Ant http://ant.apache.org
2,日志記錄示例
在這個示例中,只是將執行過的調用執行日志記錄功能
首先下載synapse: http://synapse.apache.org/download.html
解壓,下文中提到的<synapse-home>就是解壓后的根目錄,進入到<synapse-home>/samples/axis2Server/src/SimpleStockQuoteService
運行ant
執行成功之后,到<synapse-home>/samples/axis2Server目錄啟動服務
axis2Server.bat
啟動
啟動Synapse
到目錄<synapse-home>/repository/conf/sample目錄下,看一下文件synapse_sample_0.xml
<!-- Introduction to Synapse --> <definitions xmlns="http://ws.apache.org/ns/synapse"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd"><sequence name="main"><!-- log all attributes of messages passing through --><log level="full"/><!-- Send the message to implicit destination --><send/></sequence></definitions>?定義記錄所有通過的日志消息
然后到<synapse-home>/bin目錄下,啟動Synapse
執行 synapse.bat -sample 0
如上圖所示表示啟動成功
運行客戶端
進行<synapse-home>/samples/axis2Client目錄
運行 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280 -Dmode=quote -Dsymbol=IBM
查看發布的結果
http://localhost:9000/services/SimpleStockQuoteService
運行其它的示例程序如上。
使用代理服務發布
<!-- Introduction to proxy services --> <definitions xmlns="http://ws.apache.org/ns/synapse"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd"><proxy name="StockQuoteProxy"><target><endpoint><address uri="http://localhost:9000/services/SimpleStockQuoteService"/></endpoint><outSequence><send/></outSequence></target><publishWSDL uri="file:repository/conf/sample/resources/proxy/sample_proxy_1.wsdl"/></proxy></definitions>?啟動synapse服務
synapse.bat -sample 150
運行客戶端
ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuoteProxy -Dmode=quote -Dsymbol=IBM
控制臺顯示如下信息:
Standard :: Stock price = $165.32687331383468
3,改變日志級別
假如你在調試模式下運行,那么可以修改對應的配置文件的日志級別
可以在<synapse-home>/lib目錄下log4j.properties,修改
"log4j.category.org.apache.synapse=INFO" as "log4j.category.org.apache.synapse=DEBUG"
就可以看到調試的信息
?
三.消息中介示例
1,簡單介紹
在這個示例中
<!-- Introduction to Synapse --> <definitions xmlns="http://ws.apache.org/ns/synapse"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd"><sequence name="main"><!-- log all attributes of messages passing through --><log level="full"/><!-- Send the message to implicit destination --><send/></sequence></definitions>?使用示例有兩種模式在客戶端調用
1)智能客戶端模式:ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/
2)使用synapse作為http代理
執行成功行服務端能看到
Thu Nov 03 16:47:22 CST 2011 samples.services.SimpleStockQuoteService :: Generat
ing quote for : IBM
客戶端能看到
?Standard :: Stock price = $80.1611906447455
2,執行代理客戶端
<!-- Simple content based routing (CBR) of messages --> <definitions xmlns="http://ws.apache.org/ns/synapse"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd"><sequence name="main"><!-- filtering of messages with XPath and regex matches --><filter source="get-property('To')" regex=".*/StockQuote.*"><then><send><endpoint><address uri="http://localhost:9000/services/SimpleStockQuoteService"/></endpoint></send><drop/></then></filter><send/></sequence></definitions>?執行結果同上
可以在9000端口上看到執行的結果
打開 http://localhost:9000/services/SimpleStockQuoteService?wsdl 可以看到接口的定義
3,使用CBR選擇屬性
配置文件如下:
<!-- CBR with the Switch-case mediator, using message properties --> <definitions xmlns="http://ws.apache.org/ns/synapse"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd"><sequence name="main"><switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples"><case regex="IBM"><!-- the property mediator sets a local property on the *current* message --><property name="symbol" value="Great stock - IBM"/></case><case regex="MSFT"><property name="symbol" value="Are you sure? - MSFT"/></case><default><!-- it is possible to assign the result of an XPath expression as well --><property name="symbol" expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"/></default></switch><log level="custom"><!-- the get-property() XPath extension function allows the lookup of local message propertiesas well as properties from the Axis2 or Transport contexts (i.e. transport headers) --><property name="symbol" expression="get-property('symbol')"/><!-- the get-property() function supports the implicit message headers To/From/Action/FaultTo/ReplyTo --><property name="epr" expression="get-property('To')"/></log><!-- Send the messages where they are destined to (i.e. the 'To' EPR of the message) --><send/></sequence></definitions>?服務端執行synapse.bat -sample 2
客戶端執行
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM或者執行 ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT
后面的例子在下面的文章中依次列出
四.屬性說明
1,definitions
Synapse配置的根元素,有默認的命名空間
http://ws.apache.org/ns/synapse2,sequence
這個是所有消息中介的入口點,有一個非常重要的屬性
name="main"這個是所有程序的入口點,相當于java中的main函數
3,log
指定日志配置用的級別
4,in
標明執行請求要執行哪些中介
?
?
總結
以上是生活随笔為你收集整理的apache synapse使用(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js实现双向链表互联网机顶盒实战应用
- 下一篇: 适用于 IT 专业人员的基本 Windo