apache synapse使用(2)

简介:

接着上面看官方的示例

消息中介示例

1,本地注册项,可重复使用的端点和序列

<!-- Local Registry entry definitions, reusable endpoints and sequences -->
< definitions  xmlns="http://ws.apache.org/ns/synapse"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     <!-- define a string resource entry to the local registry -->
     < localEntry  key="version">0.1</ localEntry >
     <!-- define a reuseable endpoint definition -->
     < endpoint  name="simple">
         < address  uri="http://localhost:9000/services/SimpleStockQuoteService"/>
     </ endpoint >
 
     <!-- define a reusable sequence -->
     < sequence  name="stockquote">
         <!-- log the message using the custom log level. illustrates custom properties for log -->
         < log  level="custom">
             < property  name="Text" value="Sending quote request"/>
             < property  name="version" expression="get-property('version')"/>
             < property  name="direction" expression="get-property('direction')"/>
         </ log >
         <!-- send message to real endpoint referenced by key "simple" endpoint definition -->
         < send >
             < endpoint  key="simple"/>
         </ send >
     </ sequence >
 
     < sequence  name="main">
         < in >
             < property  name="direction" value="incoming"/>
             < sequence  key="stockquote"/>
         </ in >
         < out >
             < send />
         </ out >
     </ sequence >
</ definitions >

客户端执行

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/

 可以看到输出的结果

 Standard :: Stock price = $80.1611906447455

过程是先进入main然后直接进入可重用序列stockqnote,最后将请求的信息发送到http://localhost:9000/services/SimpleStockQuoteService

使用http://localhost:9000/services/SimpleStockQuoteService?wsdl可以看到显示的结果

 

2,错误处理

< definitions  xmlns="http://ws.apache.org/ns/synapse"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
     <!-- the default fault handling sequence used by Synapse - named 'fault' -->
     < sequence  name="fault">
         < log  level="custom">
             < property  name="text" value="An unexpected error occured"/>
             < property  name="message" expression="get-property('ERROR_MESSAGE')"/>
         </ log >
         < drop />
     </ sequence >
 
     < sequence  name="sunErrorHandler">
         < log  level="custom">
             < property  name="text" value="An unexpected error occured for stock SUN"/>
             < property  name="message" expression="get-property('ERROR_MESSAGE')"/>
             <!--<property name="detail" expression="get-property('ERROR_DETAIL')"/>-->
         </ log >
         < drop />
     </ sequence >
 
     < sequence  name="main">
         < in >
             < switch  xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol">
                 < case  regex="IBM">
                     < send >
                         < endpoint >
                             < address  uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                         </ endpoint >
                     </ send >
                 </ case >
                 < case  regex="MSFT">
                     < send >
                         < endpoint  key="bogus"/>
                     </ send >
                 </ case >
                 < case  regex="SUN">
                     < sequence  key="sunSequence"/>
                 </ case >
             </ switch >
             < drop />
         </ in >
 
         < out >
             < send />
         </ out >
     </ sequence >
 
     < sequence  name="sunSequence" onError="sunErrorHandler">
         < send >
             < endpoint  key="sunPort"/>
         </ send >
     </ sequence >
 
</ definitions >

 客户端执行

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

执行查询MSFT的股价,因为没有对应的端点查找最接近的错误处理,服务端看到提示

INFO LogMediator text = An unexpected error occured, message = Couldn't find the endpoint with the key : bogus

执行查看sun的股价

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

最后打印出信息

INFO LogMediator text = An unexpected error occured for stock SUN, message = Couldn't find the endpoint with the key : sunPort

这个是在sunSeqence这个序列里执行的。

 

3,创建错误的SOAP信息并且变化消息的方向

<!-- Creating SOAP fault messages and changing the direction of a message -->
< definitions  xmlns="http://ws.apache.org/ns/synapse"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
     < sequence  name="myFaultHandler">
         < makefault  response="true">
             < code  xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
             < reason  expression="get-property('ERROR_MESSAGE')"/>
         </ makefault >
         < send />
     </ sequence >
 
     < sequence  name="main" onError="myFaultHandler">
         < in >
             < switch  xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol">
                 < case  regex="MSFT">
                     < send >
                         < endpoint >
                             < address  uri="http://bogus:9000/services/NonExistentStockQuoteService"/>
                         </ endpoint >
                     </ send >
                 </ case >
                 < case  regex="SUN">
                     < send >
                         < endpoint >
                             < address  uri="http://localhost:9009/services/NonExistentStockQuoteService"/>
                         </ endpoint >
                     </ send >
                 </ case >
             </ switch >
             < drop />
         </ in >
 
         < out >
             < send />
         </ out >
     </ sequence >
 
</ definitions >

 客户端调用

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

 返回

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Client</faultcode> <faultstring>java.net.UnknownHostException: bogus</faultstring><detail /></soapenv:Fault>

执行

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

返回

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Client</faultcode> <faultstring>java.net.ConnectException: Connection refused</faultstring><detail /></soapenv:Fault>

 

4,操纵SOAP协议头,修改传入或传出的消息

<!-- Manipulating SOAP headers, and filtering incoming and outgoing messages -->
< definitions  xmlns="http://ws.apache.org/ns/synapse"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
     < sequence  name="main">
         < in >
             < header  name="To" value="http://localhost:9000/services/SimpleStockQuoteService"/>
         </ in >
         < send />
     </ sequence >
 
</ definitions >

 修改协议头

客户端调用

ant stockquote -Dtrpurl=http://localhost:8280/

直接指向

目录
相关文章
|
监控 Java Apache
apache synapse使用(1)
一.Synapse介绍 Synapse 是一个简单的 XML 和 Web 服务管理与集成代理,可用于构成 SOA 和企业服务总线(ESB)的基础。Synapse是 Web 服务项目中一项成熟的 Apache 活动,并且是非常成功的 Apache Axis2 项目的一个分支。
796 0
|
25天前
|
存储 消息中间件 Java
Apache Flink 实践问题之原生TM UI日志问题如何解决
Apache Flink 实践问题之原生TM UI日志问题如何解决
31 1
|
9天前
|
SQL 消息中间件 关系型数据库
Apache Doris Flink Connector 24.0.0 版本正式发布
该版本新增了对 Flink 1.20 的支持,并支持通过 Arrow Flight SQL 高速读取 Doris 中数据。
|
23天前
|
消息中间件 监控 数据挖掘
基于RabbitMQ与Apache Flink构建实时分析系统
【8月更文第28天】本文将介绍如何利用RabbitMQ作为数据源,结合Apache Flink进行实时数据分析。我们将构建一个简单的实时分析系统,该系统能够接收来自不同来源的数据,对数据进行实时处理,并将结果输出到另一个队列或存储系统中。
90 2
|
25天前
|
消息中间件 分布式计算 Hadoop
Apache Flink 实践问题之Flume与Hadoop之间的物理墙问题如何解决
Apache Flink 实践问题之Flume与Hadoop之间的物理墙问题如何解决
35 3
|
25天前
|
消息中间件 运维 Kafka
Apache Flink 实践问题之达到网卡的最大速度如何解决
Apache Flink 实践问题之达到网卡的最大速度如何解决
34 2
|
27天前
|
消息中间件 前端开发 Kafka
【Azure 事件中心】使用Apache Flink 连接 Event Hubs 出错 Kafka error: No resolvable bootstrap urls
【Azure 事件中心】使用Apache Flink 连接 Event Hubs 出错 Kafka error: No resolvable bootstrap urls
|
1天前
|
消息中间件 资源调度 API
Apache Flink 流批融合技术介绍
本文源自阿里云高级研发工程师周云峰在Apache Asia Community OverCode 2024的分享,内容涵盖从“流批一体”到“流批融合”的演进、技术解决方案及社区进展。流批一体已在API、算子和引擎层面实现统一,但用户仍需手动配置作业模式。流批融合旨在通过动态调整优化策略,自动适应不同场景需求。文章详细介绍了如何通过量化指标(如isProcessingBacklog和isInsertOnly)实现这一目标,并展示了针对不同场景的具体优化措施。此外,还概述了社区当前进展及未来规划,包括将优化方案推向Flink社区、动态调整算子流程结构等。
136 31
Apache Flink 流批融合技术介绍
|
25天前
|
SQL 运维 分布式计算
Apache Flink 实践问题之避免用户作业包中包含Flink的core包如何解决
Apache Flink 实践问题之避免用户作业包中包含Flink的core包如何解决
37 1
Apache Flink 实践问题之避免用户作业包中包含Flink的core包如何解决
|
1月前
|
SQL API Apache
官宣|Apache Flink 1.20 发布公告
Apache Flink 1.20.0 已发布,这是迈向 Flink 2.0 的最后一个小版本,后者预计年底发布。此版本包含多项改进和新功能,涉及 13 个 FLIPs 和 300 多个问题解决。亮点包括引入物化表简化 ETL 管道开发,统一检查点文件合并机制减轻文件系统压力,以及 SQL 语法增强如支持 `DISTRIBUTED BY` 语句。此外,还进行了大量的配置项清理工作,为 Flink 2.0 铺平道路。这一版本得益于 142 位贡献者的共同努力,其中包括来自中国多家知名企业的开发者。
762 7
官宣|Apache Flink 1.20 发布公告