一.Synapse介绍
Synapse 是一个简单的 XML 和 Web 服务管理与集成代理,可用于构成 SOA 和企业服务总线(ESB)的基础。Synapse是 Web 服务项目中一项成熟的 Apache 活动,并且是非常成功的 Apache Axis2 项目的一个分支。它提供了中介、管理、以及在各种不同的应用程序之间转换 XML 消息的能力
看看官方给出的架构图
二.官方示例
1,环境准备
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 properties
as 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/synapse
2,sequence
这个是所有消息中介的入口点,有一个非常重要的属性
name="main"
这个是所有程序的入口点,相当于java中的main函数
3,log
指定日志配置用的级别
4,in
标明执行请求要执行哪些中介