ESB初步配置文件认识

每个项目的都有各自的场景,但是其实往小处说,场景的处理基本都是很相似,之前做copy文件的程序,其实就是一种很常见的ETL的过程(转移文件,异构系统通过文件系统交换数据,存在数据同步)。

了解一下ETL:就是数据转移的一个处理过程(A库与B库之间进行数据抽取)---最重要就是格式的转换。

了解一下ESB,专门的数据处理中心的平台系统(建立一个数据中心,对外提高数据服务)。

开源ESB平台:Mule,Spring Intergation

       数据源:DB,FTP,File,Socket,HTTP,JMS

==============================================================================================

mule例子:拷贝文件

File数据源:
<?
xml version="1.0" encoding="UTF-8"?> <mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd"> <file:connector name="FileConnector" streaming="true" pollingFrequency="5000"> <file:expression-filename-parser /> </file:connector> <model name="FileDataChange"> <service name="fileService"> <inbound> <file:inbound-endpoint path="c:/data/snapshot" /> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="c:/data/archive" outputPattern="#[message.inboundProperties['originalFilename']]"/> </pass-through-router> </outbound> </service> </model> </mule>
概念:连接器、端点、轮询器(队列轮询机制)
控制台数据源
---------------------------------------------------------------------------------------------------------------------------------------------------
Spring integration配置文件例子(借鉴cafe例子写)
<?
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-file="http://www.springframework.org/schema/integration/file" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd"> <!-- Spring 框架组件的挂节点(gateway,spliter,fliter,transformer,router,service-activator,aggregator,adapter,channel) --> <bean id="tools" class="com.yyb.test.ite.OrderTools"></bean> <!-- 轮询器 --> <int:poller id="poller" default="true" fixed-rate="1000"/> <!-- 数据采集的源端 --> <int:gateway id="cafe" service-interface="com.yyb.test.ite.IOrderService" /> <!-- 数据采集的通道 --> <int:channel id="orders" /> <int:header-enricher input-channel="orders" output-channel="addHeader_orders" > <int:header name="type" value="cofe_china"/> </int:header-enricher> <int:channel id="addHeader_orders" /> <int:filter input-channel="addHeader_orders" output-channel="after_orders" ref="tools" method="filterOrder"/> <int:channel id="after_orders" /> <!-- 通道数据的切割器 --> <int:splitter input-channel="after_orders" ref="tools" method="spliteOrder" output-channel="drinks" /> <!-- 第二个数据通道 --> <int:channel id="drinks"/> <!-- 通道数据路由器 --> <int:router input-channel="drinks" ref="tools" method="routerOrder" /> <!-- 通道一 --> <int:channel id="coldDrinks"> <int:queue capacity="10" /> </int:channel> <!-- 挂接在通道上的服务激活器 --> <int:service-activator input-channel="coldDrinks" ref="tools" method="service_01" output-channel="preparedDrinks" /> <!-- 通道二 --> <int:channel id="hotDrinks"> <int:queue capacity="10" /> </int:channel> <!-- 挂接在通道上的服务激活器 --> <int:service-activator input-channel="hotDrinks" ref="tools" method="service_02" output-channel="preparedDrinks" /> <!-- 通道 --> <int:channel id="preparedDrinks" /> <!-- 数据聚合器 --> <int:aggregator input-channel="preparedDrinks" ref="tools" method="completeOrder" output-channel="deliveries" /> <!-- 数据转换器 --> <int:transformer input-channel="deliveries" output-channel="successOrder" ref="tools" method="orderTransString"/> <!-- 输出通道 --> <int:channel id="successOrder" /> <!-- 数据源目的端 采用文件讲数据保存 形成数据落地 --> <int-file:outbound-channel-adapter channel="successOrder" directory="c:/b"></int-file:outbound-channel-adapter> </beans>

//数据接收端
public interface IOrderService {
    @Gateway(requestChannel="orders")
    public void orderFood(Order order);
}
package com.yyb.test.ite;

import java.util.List;

import org.springframework.integration.annotation.Aggregator;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Headers;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.annotation.Transformer;

//组件挂接点(扩张点)--实现自己的业务对其组件扩展
public class OrderTools { @Transformer public String orderTransString(@Payload Order order) { return order.toString(); } @Filter public boolean filterOrder(@Header String type) { System.out.println(type); return true; } @Router public String routerOrder( @Payload OrderItem order) { System.out.println(order); return order.getType(); } @Splitter public List spliteOrder(Order order) { return order.getItems(); } @Aggregator public Order completeOrder(List<OrderItem> items) { Order order = new Order(); order.setUserName("tty"); order.setOrderName("ppi"); order.setItems(items); return order; }
  //挂接自己的业务实现 @ServiceActivator
public OrderItem service_01(@Payload OrderItem orderItem) { System.out.println("service_01=" + orderItem.toString()); return orderItem; }
   //挂接自己的业务实现 @ServiceActivator
public OrderItem service_02(@Payload OrderItem orderItem) { System.out.println("service_02=" + orderItem.toString()); return orderItem; } @Transformer public OrderItem transStringToObject(@Payload OrderItem order) { return null; } }

public class Order {

    private String orderName;
    private String orderId;
    private String userName;

    private List<OrderItem> items;
}
public class OrderItem {

    private String name;
    private String type;
}
//省略get set

public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-cafe.xml");
        IOrderService service = (IOrderService) context.getBean("cafe");
        
        
        Order order = new Order();
        order.setUserName("yyg");
        order.setOrderName("一份辣子鸡");
        order.setOrderId("001");
        
        OrderItem item01 = new OrderItem();
        item01.setName("鸡汤一碗");
        item01.setType("coldDrinks");
        
        OrderItem item02 = new OrderItem();
        item02.setName("一知排骨");
        item02.setType("hotDrinks");
        
        List<OrderItem> items = new ArrayList<OrderItem>();
        
        items.add(item01);
        items.add(item02);
        
        order.setItems(items);
        
        service.orderFood(order );//添加数据到通道
        
    }
 
原文地址:https://www.cnblogs.com/gstsyyb/p/3864339.html