Apache Synapse

大城市里の小女人 提交于 2020-01-30 00:41:06

       Apache Synapse可以提供企业服务总线(ESB,Enterprise Service Bus)的许多功能。可以从很多厂商那里获得ESB,通过可扩展标记语言(EXML,Extensible Markup Language)Web服务接口以及基于规则的标准化路由,在企业系统之间提供安全的互用性。

       ESB有这么一个功能,把XML文件从一种数据格式转换为另一种,从而使得可以在不同的应用程序中共享这些文件。此外,ESB还保证可以在预设规则的基础上共享数据。

       下载:http://synapse.apache.org/download.html

Apache Synapse特性

1、基于Apache Axis2,最新1.2版本
2、支持服务注册以及寻址管理
3、支持WS-Addressing/WS-Security/WS-RM等规范
4、支持HTTP(S)/JMS/POP3/SMTP/IMAP/FIX等协议以及转换
5、消息处理:消息接收、转发、中介、路由、错误处理
6、服务质量
7、支持Load BalanceFailover,以及服务缓存
8、支持脚本中介,无需修改应用代码,通过JS等脚本来控制消息
9JMX,支持自治监控

消息仲裁框架

Apache Synapse 实现了通用的中介框架。可以指定一个对消息进行操作(基于它所到达的端点、或基于某种筛选)的中介序列Synapse 的目标是支持一组有价值的、即时可用的中介。

Synapse中介

1、记录日志到 log4j
2
、修改 Header,包括 WS-Addressing Header。 实现基于内容的路由功能。
3、运行脚本,包括 JavaScriptGroovyRuby Bean Scripting Framework (BSF) 所支持的其他脚本。E4X 支持允许 JavaScript 直接操纵 XML 消息,而不需要使用像 DOM 这样的 API。在 Ruby 中也存在类似的支持。
4、启用和终止 WS-Security WS-ReliableMessaging
可在
SOAP 版本和 XML/HTTP 之间进行切换。
5、协议转换,JMS/HTTP(S)
6
XML 模式验证。
7、发现消息中的错误或者丢弃它们。
8、用户自定义中介

Synapse配置架构

 Synapse代理模型

例一、Web Service代理示例

场景说明可以将不同应用上的Web Service发布到一台Synapse主机上

<definitions xmlns="http://ws.apache.org/ns/synapse">    <proxy name="DisableCitizen">        <target>            <endpoint>                <address uri="https://api.nciic.org.cn/nciic_ws/services/NciicServices"/>            </endpoint>            <outSequence>            <send/>            </outSequence>        </target>        <publishWSDL uri="https://api.nciic.org.cn/nciic_ws/services/NciicServices?wsdl"/>    </proxy></definitions>



例二、CBR示例

场景说明:基于内容的消息路由。匹配SOAP消息中的“To”属性值,将符合条件的请求转发到指定的端点处理。

<definitions xmlns="http://ws.apache.org/ns/synapse">    <!-- filtering of messages with XPath and regex matches -->  <filter source="get-property('To')" regex=".*/DisableCitizen.*">        <send>            <endpoint>                <address uri="https://api.nciic.org.cn/nciic_ws/services/NciicServices"/>           </endpoint>        </send>        <drop/>    </filter>    <send/></definitions> 

 

例三、负载均衡

场景说明:以round-robin的方式在三个服务端点之间实现负载均衡。

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main" onError="errorHandler"> <in> <send> <endpoint> <loadbalance> <endpoint> 
<address uri="http://localhost:9001/soap/LBService1"> <enableAddressing/> <suspendDurationOnFailure>60</suspendDurationOnFailure> </address> </endpoint> <endpoint> 
<address uri="http://localhost:9002/soap/LBService1"> <enableAddressing/> <suspendDurationOnFailure>60</suspendDurationOnFailure> </address> </endpoint> <endpoint> 
<address uri="http://localhost:9003/soap/LBService1"> <enableAddressing/> <suspendDurationOnFailure>60</suspendDurationOnFailure> </address> 
</endpoint> </loadbalance> </endpoint> </send> <drop/> </in> <out> <!-- Send the messages where they have been sent (i.e. implicit To EPR) --> <send/> </out> </sequence> </definitions> 



例四、Fail Over

场景说明:在三个服务端点之间实现失效备援。

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main" onError="errorHandler"> <in> <send> <endpoint> <failover > <endpoint> 
<address uri="http://localhost:9001/soap/LBService1"> <enableAddressing/> <suspendDurationOnFailure>60</suspendDurationOnFailure> </address> </endpoint> <endpoint> 
<address uri="http://localhost:9002/soap/LBService1"> <enableAddressing/> <suspendDurationOnFailure>60</suspendDurationOnFailure> </address> </endpoint> <endpoint> 
<address uri="http://localhost:9003/soap/LBService1"> <enableAddressing/> <suspendDurationOnFailure>60</suspendDurationOnFailure> </address> 
</endpoint> </failover > </endpoint> </send> <drop/> </in> <out> <!-- Send the messages where they have been sent (i.e. implicit To EPR) --> <send/> </out> </sequence> </definitions> 



例五、jms/http协议转换

场景说明:用jms客户端API跨协议访问远程Web Service

<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="StockQuoteProxy" transports="jms"> <target> <inSequence> <property action="set" name="OUT_ONLY" value="true"/> </inSequence> <endpoint> <address uri="http://localhost:9000/soap/SimpleStockQuoteService"/> 
</endpoint> <outSequence> <send/> </outSequence> </target> 
<publishWSDL uri="file:repository/conf/sample/resources/proxy/sample_proxy_1.wsdl"/> </proxy> </definitions>


 

例六、可靠消息交互

场景说明:通过WS-RMsynapse与服务提供者之间实现可靠消息交互。

<definitions xmlns="http://ws.apache.org/ns/synapse"> <in> <RMSequence single="true" version="1.0"/> <send> <endpoint name="reliable"> <address uri="http://localhost:9000/soap/ReliableStockQuoteService"> <enableRM/> <enableAddressing/> 
</address> </endpoint> </send> </in> <out> 
<header name="wsrm:SequenceAcknowledgement" action="remove" xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm"/> 
<header name="wsrm:Sequence" action="remove" xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm"/> <send/> </out> </definitions> 



例七、自定义中介

场景说明:通过Synapse API自定义中介(实现org.apache.synapse.Mediator 接口)来实现对传输消息的访问和控制。

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="fault"> 
  <send>
  <makefault> <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/> <reason value="Mediation failed."/> 
  </makefault> 
  <send/> 
</sequence> 
<sequence name="main" onError="fault"> 
  <in> 
  <send> 
  <endpoint name="stockquote"> 
  <address uri="http://localhost:9000/soap/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> 
  <class name="samples.mediators.DiscountQuoteMediator"> 
  <property name="discountFactor" value="10"/> 
  <property name="bonusFor" value="5"/> </class> <send/> </out> 
</sequence> </definitions> 




易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!