Spring Integration

5.2.0

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

介绍

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

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

Spring Cloud Stream项目基于Spring Integration,其中Spring Integration用作消息驱动的微服务的引擎。

特征

  • 大多数企业集成模式的实施

  • 终点

  • 渠道(点对点和发布/订阅)

  • 聚合器

  • 过滤

  • 变压器

  • 控制总线

  • 与外部系统集成

  • ReST / HTTP

  • FTP / SFTP

  • 推特

  • Web服务(SOAP和ReST)

  • TCP / UDP

  • JMS

  • 兔子MQ

  • 电子邮件

  • 该框架具有广泛的JMX支持

  • 将框架组件公开为MBean

  • 适配器,用于从MBean获取属性,调用操作,发送/接收通知

例子

在以下“快速启动”应用程序中,您可以看到相同的网关接口用于调用两个完全不同的服务实现。要构建和运行该程序,您将需要如上所述的spring-integration-wsspring-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 。如果您使用的是从版本5.0开始的Spring Integration,则不需要任何其他依赖项-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 Initializr引导您的应用程序。

文献资料

每个Spring项目都有自己的项目 。它详细说明了如何使用项目功能以及使用它们可以实现的功能。
5.2.0 当前 GA 参考文件 API文件
5.2.1 快照 参考文件 API文件
5.1.8 GA 参考文件 API文件
5.1.8 快照 参考文件 API文件
4.3.22 快照 参考文件 API文件
4.3.21 GA 参考文件 API文件

导游

该指南旨在在1530分钟内完成,提供了快速,动手的指导,用于为使用Spring的任何开发任务构建入门应用程序