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 项目的一个分支。
1122 0
|
11月前
|
人工智能 数据处理 API
阿里云、Ververica、Confluent 与 LinkedIn 携手推进流式创新,共筑基于 Apache Flink Agents 的智能体 AI 未来
Apache Flink Agents 是由阿里云、Ververica、Confluent 与 LinkedIn 联合推出的开源子项目,旨在基于 Flink 构建可扩展、事件驱动的生产级 AI 智能体框架,实现数据与智能的实时融合。
1606 6
阿里云、Ververica、Confluent 与 LinkedIn 携手推进流式创新,共筑基于 Apache Flink Agents 的智能体 AI 未来
|
存储 Cloud Native 数据处理
从嵌入式状态管理到云原生架构:Apache Flink 的演进与下一代增量计算范式
本文整理自阿里云资深技术专家、Apache Flink PMC 成员梅源在 Flink Forward Asia 新加坡 2025上的分享,深入解析 Flink 状态管理系统的发展历程,从核心设计到 Flink 2.0 存算分离架构,并展望未来基于流批一体的通用增量计算方向。
774 0
从嵌入式状态管理到云原生架构:Apache Flink 的演进与下一代增量计算范式
|
SQL 人工智能 数据挖掘
Apache Flink:从实时数据分析到实时AI
Apache Flink 是实时数据处理领域的核心技术,历经十年发展,已从学术项目成长为实时计算的事实标准。它在现代数据架构中发挥着关键作用,支持实时数据分析、湖仓集成及实时 AI 应用。随着 Flink 2.0 的发布,其在流式湖仓、AI 驱动决策等方面展现出强大潜力,正推动企业迈向智能化、实时化的新阶段。
1346 9
Apache Flink:从实时数据分析到实时AI
|
SQL 人工智能 API
Apache Flink 2.1.0: 面向实时 Data + AI 全面升级,开启智能流处理新纪元
Apache Flink 2.1.0 正式发布,标志着实时数据处理引擎向统一 Data + AI 平台迈进。新版本强化了实时 AI 能力,支持通过 Flink SQL 和 Table API 创建及调用 AI 模型,新增 Model DDL、ML_PREDICT 表值函数等功能,实现端到端的实时 AI 工作流。同时增强了 Flink SQL 的流处理能力,引入 Process Table Functions(PTFs)、Variant 数据类型,优化流式 Join 及状态管理,显著提升作业稳定性与资源利用率。
1031 0
|
存储 人工智能 大数据
The Past, Present and Future of Apache Flink
本文整理自阿里云开源大数据负责人王峰(莫问)在 Flink Forward Asia 2024 上海站主论坛开场的分享,今年正值 Flink 开源项目诞生的第 10 周年,借此时机,王峰回顾了 Flink 在过去 10 年的发展历程以及 Flink社区当前最新的技术成果,最后展望下一个十年 Flink 路向何方。
1386 33
The Past, Present and Future of Apache Flink
|
人工智能 运维 Java
Flink Agents:基于Apache Flink的事件驱动AI智能体框架
本文基于Apache Flink PMC成员宋辛童在Community Over Code Asia 2025的演讲,深入解析Flink Agents项目的技术背景、架构设计与应用场景。该项目聚焦事件驱动型AI智能体,结合Flink的实时处理能力,推动AI在工业场景中的工程化落地,涵盖智能运维、直播分析等典型应用,展现其在AI发展第四层次——智能体AI中的重要意义。
3654 27
Flink Agents:基于Apache Flink的事件驱动AI智能体框架
|
SQL Java API
Apache Flink 2.0-preview released
Apache Flink 社区正积极筹备 Flink 2.0 的发布,这是自 Flink 1.0 发布以来的首个重大更新。Flink 2.0 将引入多项激动人心的功能和改进,包括存算分离状态管理、物化表、批作业自适应执行等,同时也包含了一些不兼容的变更。目前提供的预览版旨在让用户提前尝试新功能并收集反馈,但不建议在生产环境中使用。
1966 13
Apache Flink 2.0-preview released
|
SQL 消息中间件 关系型数据库
Apache Doris Flink Connector 24.0.0 版本正式发布
该版本新增了对 Flink 1.20 的支持,并支持通过 Arrow Flight SQL 高速读取 Doris 中数据。
778 21
|
存储 人工智能 数据处理
对话王峰:Apache Flink 在 AI 时代的“剑锋”所向
Flink 2.0 架构升级实现存算分离,迈向彻底云原生化,支持更大规模状态管理、提升资源效率、增强容灾能力。通过流批一体与 AI 场景融合,推动实时计算向智能化演进。生态项目如 Paimon、Fluss 和 Flink CDC 构建湖流一体架构,实现分钟级时效性与低成本平衡。未来,Flink 将深化 AI Agents 框架,引领事件驱动的智能数据处理新方向。
1181 6

热门文章

最新文章

推荐镜像

更多