Spring系列学习之Spring Integration
英文原文:https://spring.io/projects/spring-integration
目录
概述
扩展Spring编程模型以支持众所周知的企业集成模式。 Spring Integration在基于Spring的应用程序中实现轻量级消息传递,并支持通过声明适配器与外部系统集成。与Spring对远程处理,消息传递和调度的支持相比,这些适配器提供了更高级别的抽象。 Spring Integration的主要目标是提供一个简单的模型来构建企业集成解决方案,同时保持关注点的分离,这对于生成可维护,可测试的代码至关重要。
介绍
使用Spring Framework鼓励开发人员使用接口进行编码,并使用依赖注入(DI)为普通旧Java对象(POJO)提供执行其任务所需的依赖项。 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="'<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-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 Initializr引导您的应用程序。
学习
文档
每个Spring项目都有自己的; 它详细解释了如何使用项目功能以及使用它们可以实现的功能。
5.1.1 CURRENT GA | Reference Doc. | API Doc. |
5.1.2 SNAPSHOT | Reference Doc. | API Doc. |
5.0.11 SNAPSHOT | Reference Doc. | API Doc. |
5.0.10 GA | Reference Doc. | API Doc. |
4.3.19 SNAPSHOT | Reference Doc. | API Doc. |
4.3.18 GA | Reference Doc. | API Doc. |
指南
该指南旨在在15-30分钟内完成,提供快速,实用的说明,用于为Spring的任何开发任务构建入门应用程序。
- Integrating Data 了解如何构建使用Spring Integration获取数据,处理数据并将其写入文件的应用程序。
示例
尝试一些示例:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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配置使用