ESB学习笔记(Spring Integration实战)

6 篇文章 0 订阅
http://wangleifire.iteye.com/blog/351749
  • 介绍
    Spring Integration是Spring公司的一套ESB框架。
    前面ESB介绍中我也做了一定了解。我们来看一下它主要做什么的。
    Spring Integration is motivated by the following goals:
    • Provide a simple model for implementing complex enterprise integration solutions.(暂时相信它吧,谁让它搞个Spring框架,的确给人方便一把。)
    • Facilitate asynchronous, message-driven behavior within a Spring-based application.(这个不谈,Spring框架就是它玩的。再说这一点与它竞争只有Mule啦。)
    • Promote intuitive, incremental adoption for existing Spring users. (也暂时相信它,别人都只说给用户提升。)

    Spring Integration is guided by the following principles:
    • Components should be loosely coupled for modularity and testability.(松耦合,好像很早很早就听说过。像做梦一样)
    • The framework should enforce separation of concerns between business logic and integration logic.(分开程度要取决业务吧。)
    • Extension points should be abstract in nature but within well-defined boundaries to promote reuse and portability.(美妙现实世界产品)

  • 源码下载打开它的网页,http://www.springsource.org/spring-integration
    主页上也没有东东,但有个下源代码的地方,svn开工啦。
    Java代码 复制代码
    1. svn co https://src.springframework.org/svn/spring-integration/trunk  springintegration   
    1. 下载完后,进入build-spring-integration目录执行ant.完成后,导入到Eclipse中。
      导入项目会有很多,先添加时会有报错。这里需要添加一个变量。
      IVY_CACHE=<checkout-dir>/ivy-cache/repository

      这里要注意的事,也是我遇到问题。执行ant时,他会去下载lvy,如果你本身在%ANT_HOME%\lib里有lvy.jar包,由于我暂时找不到如何处理,我就直接将Ant中的jar删除掉后就没有问题。
      另外在ant过程中,测试步骤可能会在file模块中出现问题,可以将相关test类中代码注释掉。

    2. HelloWorld源码分析在samples项目中,打开helloworld包里面有三个文件。
      Java代码 复制代码
      1. package org.springframework.integration.samples.helloworld;   
      2.   
      3. /**  
      4.  * @author Mark Fisher  
      5.  */  
      6. public class HelloService {   
      7.   
      8.     public String sayHello(String name) {   
      9.         return "Hello " + name;   
      10.     }   
      11.   
      12. }  

      helloworldDemo.xml

      Xml代码 复制代码
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans:beans xmlns="http://www.springframework.org/schema/integration"  
      3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      4.     xmlns:beans="http://www.springframework.org/schema/beans"  
      5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      6.             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
      7.             http://www.springframework.org/schema/integration   
      8.             http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">  
      9.   
      10.     <channel id="inputChannel"/>  
      11.   
      12.     <channel id="outputChannel">  
      13.         <queue capacity="10"/>  
      14.     </channel>  
      15.   
      16.     <service-activator input-channel="inputChannel"  
      17.                        output-channel="outputChannel"  
      18.                        ref="helloService"  
      19.                        method="sayHello"/>  
      20.   
      21.     <beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>  
      22. </beans:beans>  

        HelloWorldDemo.java

        Java代码 复制代码
        1. package org.springframework.integration.samples.helloworld;   
        2.   
        3. import org.springframework.context.support.AbstractApplicationContext;   
        4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
        5. import org.springframework.integration.channel.BeanFactoryChannelResolver;   
        6. import org.springframework.integration.channel.ChannelResolver;   
        7. import org.springframework.integration.channel.PollableChannel;   
        8. import org.springframework.integration.core.MessageChannel;   
        9. import org.springframework.integration.message.StringMessage;   
        10.   
        11. /**  
        12.  * Demonstrates a basic message endpoint.   
        13.  *   
        14.  * @author Mark Fisher  
        15.  */  
        16. public class HelloWorldDemo {   
        17.   
        18.     public static void main(String[] args) {   
        19.         AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloWorldDemo.xml", HelloWorldDemo.class);   
        20.         ChannelResolver channelResolver = new BeanFactoryChannelResolver(context);   
        21.         MessageChannel inputChannel = channelResolver.resolveChannelName("inputChannel");   
        22.         PollableChannel outputChannel = (PollableChannel) channelResolver.resolveChannelName("outputChannel");   
        23.         inputChannel.send(new StringMessage("World"));   
        24.         System.out.println(outputChannel.receive(0).getPayload());   
        25.         context.stop();   
        26.     }   
        27.   
        28. }  

          Cafe源码分析 Cafe示例描述的是星巴克的订单处理故事。
          其示例描述在:http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html
          这里简单描述一下,以免大家看英文太累
          文章讲在星巴克喝咖啡时,收银员可能只有一个,而冲咖啡员工会有多个,如何让收银员产生订单异步发送给冲咖啡员工。并且冲咖啡员工可能是竞争上岗的,就当他们是计件工吧。
          这里要考虑问题:
          1,冲咖啡员工使用不同设备,不同咖啡冲调时间可能不同。 
          2,冲咖啡员工可能会将相同类型的咖啡同时一起冲调。
          星巴克如何处理这个问题?
          就当他解决了这个问题,它是如何把每个咖啡又送回给每个客户呢?当然,星巴克采用“标识关系模式”,将每个咖啡杯上标上名称,并通过叫喊方式。
          但并不是每天都是美好的,总有出错的时候。例如,收银员无法支付?冲调一杯你不喜欢的咖啡,你要换一杯?冲咖啡的设备坏了,星巴克要退你钱...这些异常情况如何处理。
          因此就会有以下三种方式异常处理:
          1,关闭交易,什么都不做。
          2,重做,重新发起行为。
          3,修正行为,相当于退钱这种行为。
          因此,这里这篇文章后面讨论一下两阶段提交为什么不适合星巴克,如果你让收银员、冲咖啡员工,买单的人需要在一个“事务”中,交易所有完成后,再进行下一个业务。估计星巴克会马上倒闭啦。因此星巴克采用“Conversation pattern”模式。

          好啦,业务了解清楚,我们再来看一下完整XML文件。在这里我没有采用示例详细的xml方式,而没有采用annotation方式。

          Xml代码 复制代码
          1. <?xml version="1.0" encoding="UTF-8"?>  
          2. <beans:beans xmlns="http://www.springframework.org/schema/integration"  
          3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
          4.     xmlns:beans="http://www.springframework.org/schema/beans"  
          5.     xmlns:stream="http://www.springframework.org/schema/integration/stream"  
          6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
          7.             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
          8.             http://www.springframework.org/schema/integration   
          9.             http://www.springframework.org/schema/integration/spring-integration-1.0.xsd   
          10.             http://www.springframework.org/schema/integration/stream   
          11.             http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">  
          12.   
          13.     <gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>  
          14.   
          15.     <channel id="orders"/>  
          16.     <splitter input-channel="orders" ref="orderSplitter" method="split" output-channel="drinks"/>  
          17.   
          18.     <channel id="drinks"/>  
          19.     <router input-channel="drinks" ref="drinkRouter" method="resolveOrderItemChannel"/>  
          20.   
          21.     <channel id="coldDrinks">  
          22.         <queue capacity="10"/>  
          23.     </channel>  
          24.     <service-activator input-channel="coldDrinks" ref="barista"  
          25.                        method="prepareColdDrink" output-channel="preparedDrinks"/>  
          26.   
          27.     <channel id="hotDrinks">  
          28.         <queue capacity="10"/>  
          29.     </channel>  
          30.     <service-activator input-channel="hotDrinks" ref="barista"  
          31.                        method="prepareHotDrink" output-channel="preparedDrinks"/>  
          32.   
          33.     <channel id="preparedDrinks"/>  
          34.     <aggregator input-channel="preparedDrinks" ref="waiter"  
          35.                 method="prepareDelivery" output-channel="deliveries"/>  
          36.   
          37.     <stream:stdout-channel-adapter id="deliveries"/>  
          38.   
          39.     <beans:bean id="orderSplitter"  
          40.                 class="org.springframework.integration.samples.cafe.xml.OrderSplitter"/>  
          41.   
          42.     <beans:bean id="drinkRouter"  
          43.                 class="org.springframework.integration.samples.cafe.xml.DrinkRouter"/>  
          44.   
          45.     <beans:bean id="barista" class="org.springframework.integration.samples.cafe.xml.Barista"/>  
          46.   
          47.     <beans:bean id="waiter" class="org.springframework.integration.samples.cafe.xml.Waiter"/>  
          48.   
          49.     <poller id="poller" default="true">  
          50.         <interval-trigger interval="1000"/>  
          51.     </poller>  
          52.   
          53. </beans:beans>  

            以下是参考文档中的示例描述图:



            CafeDemo代码创建了订单。这家咖啡店有两种饮料,一种是热的,一种是冷的,消息将这订单包装到一个"orders"的channel(频道)。一个endpoint侦听到订单频道并根据订单情况进行分开处理。

            完成分开处理后,程序交给DrinksRouter经过drink频道。而DrinkRouter一个职责就是将订单内容中的热咖啡和冷咖啡交给不同的channel处理。

            Xml代码 复制代码
            1. <gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>  

              这里Gateway主要是根据接口生成代理类。

              Java代码 复制代码
              1. Cafe cafe = (Cafe) context.getBean("cafe");   
              2.                 DrinkOrder order = new DrinkOrder();   
              3.                 Drink hotDoubleLatte = new Drink(DrinkType.LATTE, 2false);   
              4.                 Drink icedTripleMocha = new Drink(DrinkType.MOCHA, 3true);   
              5.                 order.addDrink(hotDoubleLatte);   
              6.                 order.addDrink(icedTripleMocha);   
              7.                 for (int i = 0; i < 100; i++) {   
              8.                         cafe.placeOrder(order);   
              9.                 } 
                Java代码 复制代码
                1. @MessageEndpoint(input="orders", output="drinks")   
                2. public class OrderSplitter {   
                3.   
                4.         @Splitter  
                5.         public List<Drink> split(Message<DrinkOrder> orderMessage) {   
                6.                 return orderMessage.getPayload().getDrinks();   
                7.         }   
                8.   
                9. }  
                  Java代码 复制代码
                  1. @MessageEndpoint(input="drinks")   
                  2. public class DrinkRouter {   
                  3.   
                  4.         @Router  
                  5.         public String resolveDrinkChannel(Drink drink) {   
                  6.                 return (drink.isIced()) ? "coldDrinks" : "hotDrinks";   
                  7.         }   
                  8.   
                  9. }  
                    Xml代码 复制代码
                    1. <handler-endpoint handler="coldBarista" input-channel="coldDrinks"  
                    2.         method="prepareColdDrink">  
                    3. </handler-endpoint>  
                    4.   
                    5. <handler-endpoint handler="hotBarista" input-channel="hotDrinks"  
                    6.         method="prepareHotDrink">  
                    7. </handler-endpoint>  
                      Java代码 复制代码
                      1. public void prepareColdDrink(Message<Drink> drinkMessage) {   
                      2.                 Drink drink = drinkMessage.getPayload();   
                      3.                 //no changes to the rest of the code   
                      4.         }  

                       

                    • 1
                      点赞
                    • 7
                      收藏
                      觉得还不错? 一键收藏
                    • 0
                      评论
                    Zato 是一个 Python 编写的开源企业服务总线(ESB),用于构建和集成微服务应用程序。下面是在 Ubuntu 系统上安装 Zato 的步骤: 1. 确保你的系统上已经安装了 Python 2.7 或 Python 3.4+。 2. 安装 Zato 的依赖项。在终端中运行以下命令: ``` $ sudo apt-get update $ sudo apt-get install gcc python-dev libxml2-dev libxslt1-dev zlib1g-dev ``` 3. 下载 Zato 安装程序。在终端中运行以下命令: ``` $ wget https://zato.io/downloads/3.1/zato-3.1.0.tar.gz ``` 4. 解压缩 Zato 安装程序。在终端中运行以下命令: ``` $ tar xvfz zato-3.1.0.tar.gz ``` 5. 进入解压缩后的目录。在终端中运行以下命令: ``` $ cd zato-3.1.0 ``` 6. 运行 Zato 安装程序。在终端中运行以下命令: ``` $ ./install.sh ``` 7. 按照提示进行安装。在安装过程中,你需要设置 Zato 的管理员密码和 Web 管理界面的端口号等信息。 8. 安装完成后,启动 Zato 服务器。在终端中运行以下命令: ``` $ zato start ``` 9. 检查 Zato 服务器是否启动成功。在终端中运行以下命令: ``` $ zato status ``` 如果看到类似以下输出,则表示 Zato 服务器已经启动成功: ``` Zato services status: * Load balancer is running (/opt/zato/3.1.0/zato-lb1.pid) * Server1 is running (/opt/zato/3.1.0/server1.pid) * Server2 is running (/opt/zato/3.1.0/server2.pid) * Scheduler is running (/opt/zato/3.1.0/zato-scheduler.pid) ``` 恭喜你,现在你已经成功安装了 Zato!

                    “相关推荐”对你有帮助么?

                    • 非常没帮助
                    • 没帮助
                    • 一般
                    • 有帮助
                    • 非常有帮助
                    提交
                    评论
                    添加红包

                    请填写红包祝福语或标题

                    红包个数最小为10个

                    红包金额最低5元

                    当前余额3.43前往充值 >
                    需支付:10.00
                    成就一亿技术人!
                    领取后你会自动成为博主和红包主的粉丝 规则
                    hope_wisdom
                    发出的红包
                    实付
                    使用余额支付
                    点击重新获取
                    扫码支付
                    钱包余额 0

                    抵扣说明:

                    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
                    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

                    余额充值