领先一步
VMware 提供培训和认证,助力您的进步。
了解更多扩展 Spring 编程模型以支持著名的 企业集成模式。Spring Integration 在基于 Spring 的应用程序中实现了轻量级消息传递,并通过声明性适配器支持与外部系统的集成。这些适配器在 Spring 对远程处理、消息传递和调度支持的基础上提供了更高层次的抽象。Spring Integration 的主要目标是提供一个简单的模型来构建企业集成解决方案,同时保持关注点分离,这对于生成可维护、可测试的代码至关重要。
使用 Spring Framework 鼓励开发者使用接口进行编码,并通过依赖注入(DI)为普通的 Java 对象(POJO)提供执行任务所需的依赖项。Spring Integration 更进一步,它使用消息传递范式将 POJO 连接在一起,并且各个组件可能不知道应用程序中的其他组件。这样的应用程序是通过组装细粒度的可重用组件来形成更高层次的功能。经过精心设计,这些流可以模块化并在更高的层次上重用。
除了将细粒度组件连接在一起之外,Spring Integration 还提供了广泛的选择的通道适配器(channel adapters)和网关(gateways)来与外部系统通信。通道适配器用于单向集成(发送或接收);网关用于请求/应答场景(入站或出站)。有关适配器和网关的完整列表,请参阅参考文档。
Spring Cloud Stream 项目构建在 Spring Integration 之上,其中 Spring Integration 被用作消息驱动微服务的引擎。
大多数企业集成模式的实现
端点
通道(点对点和发布/订阅)
聚合器
过滤器
转换器
控制总线
…
与外部系统的集成
ReST/HTTP
FTP/SFTP
STOMP
Web 服务(SOAP 和 RESTful)
TCP/UDP
JMS
RabbitMQ
电子邮件
…
该框架有广泛的 JMX 支持
将框架组件公开为 MBeans
用于从 MBeans 获取属性、调用操作、发送/接收通知的适配器
在下面的“快速入门”应用程序中,您可以看到使用相同的网关接口调用了两个完全不同的服务实现。要构建和运行此程序,您需要上述描述的 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://w3schools.org.cn/xml/"><Fahrenheit>XXX</Fahrenheit></FahrenheitToCelsius>'.replace('XXX', payload.toString())" />
<int-ws:header-enricher>
<int-ws:soap-action value="https://w3schools.org.cn/xml/FahrenheitToCelsius"/>
</int-ws:header-enricher>
<int-ws:outbound-gateway
uri="https://w3schools.org.cn/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-core。
@SpringBootApplication
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://w3schools.org.cn/xml/\">"
+ "<Fahrenheit>" + payload + "</Fahrenheit>"
+ "</FahrenheitToCelsius>")
.enrichHeaders(h -> h
.header(WebServiceHeaders.SOAP_ACTION,
"https://w3schools.org.cn/xml/FahrenheitToCelsius"))
.handle(new SimpleWebServiceOutboundGateway(
"https://w3schools.org.cn/xml/tempconvert.asmx"))
.transform(Transformers.xpath("/*[local-name()=\"FahrenheitToCelsiusResponse\"]"
+ "/*[local-name()=\"FahrenheitToCelsiusResult\"]"));
}
}
Spring Boot 对 Spring Integration 的自动配置
另请参阅 Spring Functions Catalog,其中的大多数构件本质上是特定 Spring Integration 通道适配器的自动配置。