天下之事,必先处之难,而后易之。

Spring系列学习之Spring Integration

英文原文:https://spring.io/projects/spring-integration

目录

概述

介绍

特性

例子

Spring Boot配置

快速开始

学习

文档

指南

示例


概述

扩展Spring编程模型以支持众所周知的企业集成模式。 Spring Integration在基于Spring的应用程序中实现轻量级消息传递,并支持通过声明适配器与外部系统集成。与Spring对远程处理,消息传递和调度的支持相比,这些适配器提供了更高级别的抽象。 Spring Integration的主要目标是提供一个简单的模型来构建企业集成解决方案,同时保持关注点的分离,这对于生成可维护,可测试的代码至关重要。

介绍

使用Spring Framework鼓励开发人员使用接口进行编码,并使用依赖注入(DI)为普通旧Java对象(PO​​JO)提供执行其任务所需的依赖项。 Spring Integration将这一概念更进一步,其中POJO使用消息传递范例连接在一起,并且各个组件可能不了解应用程序中的其他组件。这种应用程序是通过组装细粒度可重用组件来构建的,以形成更高级别的功能。通过精心设计,这些流程可以模块化,并在更高的层次上重复使用。

除了将细粒度组件连接在一起外,Spring Integration还提供多种通道适配器和网关,以便与外部系统进行通信。通道适配器用于单向集成(发送或接收);网关用于请求/回复方案(入站或出站)。有关适配器和网关的完整列表,请参阅参考文档。

Spring Cloud Stream项目建立在Spring Integration之上,其中Spring Integration用作消息驱动的微服务的引擎。

特性

  •     实施大多数企业集成模式
  •     端点Endpoint
  •     频道Channel(点对点和发布/订阅)
  •     聚合(Aggregator)
  •     过滤(Filter)
  •     变压器(Transformer)
  •     控制总线(Control Bus)
  •     ...
  •     与外部系统集成
  •     REST / HTTP
  •     FTP/ SFTP
  •     推特(Twitter)
  •     Web服务(SOAP和ReST)
  •     TCP/ UDP
  •     JMS
  •     RabbitMQ
  •     电子邮件(Email)
  •     ...
  •     该框架具有广泛的JMX支持
  •     将框架组件公开为MBean
  •     适配器从MBean获取属性,调用操作,发送/接收通知
  •  

例子

在下面的“快速入门”应用程序中,您可以看到使用相同的网关接口来调用两个完全不同的服务实现。 要构建和运行该程序,您需要如上所述的spring-integration-ws和spring-integration-xml模块。



public class Main {

	public static void main(String... args) throws Exception {
		ApplicationContext ctx =
			new ClassPathXmlApplicationContext("context.xml");
		// Simple Service
		TempConverter converter =
			ctx.getBean("simpleGateway", TempConverter.class);
		System.out.println(converter.fahrenheitToCelcius(68.0f));
		// Web Service
		converter  = 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="'&lt;FahrenheitToCelsius xmlns=&quot;https://www.w3schools.com/xml/&quot;&gt;&lt;Fahrenheit&gt;XXX&lt;/Fahrenheit&gt;&lt;/FahrenheitToCelsius&gt;'.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-gateway
		uri="https://www.w3schools.com/xml/tempconvert.asmx"/>
	<int-xml:xpath-transformer
		xpath-expression="/*[local-name()='FahrenheitToCelsiusResponse']/*[local-name()='FahrenheitToCelsiusResult']"/>
</int:chain>

 这是使用Java DSL(和Spring Boot)的相同应用程序(Web服务部分)。 如果不使用Spring Boot,则需要直接使用spring-boot-starter-integration依赖项或spring-integration-java-dsl。 如果您使用Spring Integration从5.0开始,则不需要任何其他依赖项 - Java DSL包含在核心项目中:



@Configuration
@SpringBootApplication
@IntegrationComponentScan
public 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\"]"));
  }

}

Spring Boot配置

Spring集成的Spring Boot自动配置

快速开始


使用Spring Initializr引导您的应用程序。

学习

文档

每个Spring项目都有自己的; 它详细解释了如何使用项目功能以及使用它们可以实现的功能。

指南

该指南旨在在15-30分钟内完成,提供快速,实用的说明,用于为Spring的任何开发任务构建入门应用程序。

  • Integrating Data 了解如何构建使用Spring Integration获取数据,处理数据并将其写入文件的应用程序。

示例

尝试一些示例:

 

posted @   boonya  阅读(93)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2023-09-26 Hibernate和Spring测试HSQLDB
2022-09-26 携程大数据实践:高并发应用架构及推荐系统案例
2017-09-26 Java中基本类型占用字节数
2016-09-26 软件开发中团队首领的好坏之分
2016-09-26 解耦——Hybrid H5跨平台性思考
2016-09-26 JavaScript IDE 大盘点,让选择不再难
2012-09-26 普通web项目的Resteasy配置使用
我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。

点击右上角即可分享
微信分享提示