Spring5学习(二)-spring projects之Spring Integration

来源:互联网 发布:arcgis mac版 编辑:程序博客网 时间:2024/06/08 04:56

Spring Integration

Extends(扩展) the Spring programming model(编程模型) to support the well-known(众所周知的) Enterprise Integration Patterns(企业集成模式). Spring Integration enables lightweight(轻量级) messaging within(内, 中, 里边) Spring-based applications and supports integration with external(外部) systems via declarative adapters(声明适配器). Those adapters provide a higher-level of abstraction over Spring's support for remoting, messaging, and scheduling. Spring Integration's primary goal is to provide a simple model for building enterprise integration solutions while maintaining the separation( 分割, 拆开) of concerns that is essential for(是必不可少的) producing maintainable(生产可维护), testable(可测试) code.




Introduction

Using the Spring Framework encourages developers to code using interfaces and use dependency injection (DI) to provide a Plain Old Java Object (POJO) with the dependencies it needs to perform its tasks. Spring Integration takes this concept(概念) one step further(更进一步), where POJOs are wired together using a messaging paradigm(消息范例) and individual components may not be aware of(意识到) other components in the application. Such an application is built by assembling fine-grained reusable components(组装细粒度的可重复使用的组件) to form a higher level of functionality. WIth careful design, these flows can be modularized(模块化) and also reused(重用) at an even higher level(一个更高的水平).

In addition to(此外) wiring together fine-grained components(将精细的组件连接在一起), Spring Integration provides a wide selection of channel adapters(通道适配器) and gateways(网关) to communicate with external systems. Channel Adapters are used for one-way integration (单向集成)(send or receive); gateways are used for request/reply scenarios(场景) (inbound or outbound). For a full list of adapters and gateways, refer to the reference documentation.

The Spring Cloud Stream project builds on Spring Integration, where Spring Integration is used as an engine(发动机, 引擎, 机, 机车) for message-driven microservices.




Features

  • Implementation of most of the Enterprise Integration Patterns
  • Endpoint(端点)
  • Channel (Point-to-point and Publish/Subscribe)(频道(点对点和发布/订阅))
  • Aggregator(聚合)
  • Filter
  • Transformer(变压器)
  • Control Bus(控制总线)
  • Integration with External Systems
  • ReST/HTTP
  • FTP/SFTP
  • Twitter
  • WebServices (SOAP and ReST)
  • TCP/UDP
  • JMS
  • RabbitMQ
  • Email
  • The framework has extensive(广泛的) JMX support
  • Exposing(暴露, 露出) framework components as MBeans
  • Adapters(适配器) to obtain(获得, 得到, 取得) attributes from MBeans, invoke operations(调用操作), send/receive notifications(通知)




Quick Start

current version:5.0.0

maven:

<dependencies>    <dependency>        <groupId>org.springframework.integration</groupId>        <artifactId>spring-integration-core</artifactId>        <version>5.0.0.RELEASE</version>    </dependency></dependencies>
gradle:

dependencies {    compile 'org.springframework.integration:spring-integration-core:5.0.0.RELEASE'}
In the following "quick start" application you can see that the same gateway interface(网关接口) is used to invoke two completely different service implementations. To build and run this program you will need the spring-integration-ws and spring-integration-xml modules as described above.
public class Main {public static void main(String... args) throws Exception {ApplicationContext ctx =new ClassPathXmlApplicationContext("context.xml");// Simple ServiceTempConverter converter =ctx.getBean("simpleGateway", TempConverter.class);System.out.println(converter.fahrenheitToCelcius(68.0f));// Web Serviceconverter  = ctx.getBean("wsGateway", TempConverter.class);System.out.println(converter.fahrenheitToCelcius(68.0f));}}
public interface TempConverter {float fahrenheitToCelcius(float fahren);}
<!-- Simple Service --><int:gateway id="simpleGateway"service-interface="foo.TempConverter"default-request-channel="simpleExpression" /><int:service-activator id="expressionConverter"input-channel="simpleExpression"expression="(payload - 32) / 9 * 5"/><!-- Web Service --><int:gateway id="wsGateway" service-interface="foo.TempConverter"default-request-channel="viaWebService" /><int:chain id="wsChain" input-channel="viaWebService"><int:transformer   expression="'<FahrenheitToCelsius xmlns="https://www.w3schools.com/xml/"><Fahrenheit>XXX</Fahrenheit></FahrenheitToCelsius>'.replace('XXX', payload.toString())" /><int-ws:header-enricher><int-ws:soap-action value="https://www.w3schools.com/xml/FahrenheitToCelsius"/></int-ws:header-enricher><int-ws:outbound-gatewayuri="https://www.w3schools.com/xml/tempconvert.asmx"/><int-xml:xpath-transformerxpath-expression="/*[local-name()='FahrenheitToCelsiusResponse']/*[local-name()='FahrenheitToCelsiusResult']"/></int:chain>
And here is the same application (web service part) using theJava DSL (参考:官网)(and Spring Boot). You will need the spring-boot-starter-integration dependency or spring-integration-java-dsl directly if you don't use Spring Boot. If you use Spring Integration starting version 5.0, you don't need any additional dependencies - the Java DSL is included to the core project:
@Configuration@SpringBootApplication@IntegrationComponentScanpublic class Application {  public static void main(String[] args) {    ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);    TempConverter converter = ctx.getBean(TempConverter.class);    System.out.println(converter.fahrenheitToCelcius(68.0f));    ctx.close();  }  @MessagingGateway  public interface TempConverter {    @Gateway(requestChannel = "convert.input")    float fahrenheitToCelcius(float fahren);  }  @Bean  public IntegrationFlow convert() {      return f -> f        .transform(payload ->              "<FahrenheitToCelsius xmlns=\"https://www.w3schools.com/xml/\">"            +     "<Fahrenheit>" + payload + "</Fahrenheit>"            + "</FahrenheitToCelsius>")        .enrichHeaders(h -> h            .header(WebServiceHeaders.SOAP_ACTION,                "https://www.w3schools.com/xml/FahrenheitToCelsius"))        .handle(new SimpleWebServiceOutboundGateway(            "https://www.w3schools.com/xml/tempconvert.asmx"))        .transform(Transformers.xpath("/*[local-name()=\"FahrenheitToCelsiusResponse\"]"            + "/*[local-name()=\"FahrenheitToCelsiusResult\"]"));  }}







说明:

1. POJO:POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。使用POJO名称是为了避免和EJB混淆起来, 而且简称比较直接. 其中有一些属性及其getter setter方法的类,没有业务逻辑,有时可以作为VO(value -object)或dto(Data Transform Object)来使用.当然,如果你有一个简单的运算属性也是可以的,但不允许有业务方法,也不能携带有connection之类的方法。(来自:百度百科)


2. SFTP:sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的其中一部分,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件信息传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。(来自:百度百科)


3. HTTP:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。1960年美国人Ted Nelson构思了一种通过计算机处理文本信息的方法,并称之为超文本(hypertext),这成为了HTTP超文本传输协议标准架构的发展根基。(来自:百度百科)


4. FTP:FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。在FTP的使用当中,用户经常遇到两个概念:"下载"(Download)和"上传"(Upload)。"下载"文件就是从远程主机拷贝文件至自己的计算机上;"上传"文件就是将文件从自己的计算机中拷贝至远程主机上。用Internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件。(来自:百度百科)


5. Twitter:Twitter(非官方汉语通称推特)是一家美国社交网络及微博客服务的网站,是全球互联网上访问量最大的十个网站之一。是微博客的典型应用。它可以让用户更新不超过140个字符的消息,这些消息也被称作“推文(Tweet)”。这个服务是由杰克·多西在2006年3月创办并在当年7月启动的。(来自:百度百科)


6. SOAP:简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。(来自:百度百科)


7. ReST:REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。(来自:百度百科)


8. TCP:TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793定义。在简化的计算机网络OSI模型中,它完成第四层传输层所指定的功能,用户数据报协议(UDP)是同一层内[1] 另一个重要的传输协议。在因特网协议族(Internet protocol suite)中,TCP层是位于IP层之上,应用层之下的中间层。不同主机的应用层之间经常需要可靠的、像管道一样的连接,但是IP层不提供这样的流机制,而是提供不可靠的包交换。(来自:百度百科)


9. UDP:UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768是UDP的正式规范。UDP在IP报文的协议号是17。UDP协议全称是用户数据报协议[1] ,在网络中它与TCP协议一样用于处理数据包,是一种无连接的协议。在OSI模型中,在第四层——传输层,处于IP协议的上一层。(来自:百度百科)


10. JMS:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。(来自:百度百科)


11. RabbitMQ:MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。其中较为成熟的MQ产品有IBM WEBSPHERE MQ等等。(来自:百度百科)


12. JMX:MX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。(来自:百度百科)


13. MBean:描述一个可管理的资源。是一个java对象,遵循以下一些规则:1.必须是公用的,非抽象的类 2.必须有至少一个公用的构造器 3.必须实现它自己的相应的MBean接口或者实现javax.management.DynamicMBean接口4.可选的,一个MBean可以实现javax.management.NotificationBroadcaster接口MBean的类型(来自:百度百科)


阅读全文
0 0
原创粉丝点击