Spring IntegrationSpring Integration6.4.4

擴充套件 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-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://w3schools.tw/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://w3schools.tw/xml/FahrenheitToCelsius"/>
	</int-ws:header-enricher>
	<int-ws:outbound-gateway
		uri="https://w3schools.tw/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.tw/xml/\">"
            +     "<Fahrenheit>" + payload + "</Fahrenheit>"
            + "</FahrenheitToCelsius>")
        .enrichHeaders(h -> h
            .header(WebServiceHeaders.SOAP_ACTION,
                "https://w3schools.tw/xml/FahrenheitToCelsius"))
        .handle(new SimpleWebServiceOutboundGateway(
            "https://w3schools.tw/xml/tempconvert.asmx"))
        .transform(Transformers.xpath("/*[local-name()=\"FahrenheitToCelsiusResponse\"]"
            + "/*[local-name()=\"FahrenheitToCelsiusResult\"]"));
  }

}

Spring Boot 配置

Spring Boot 對 Spring Integration 的自動配置

另請參閱 Spring Functions Catalog,其中的大多數構件本質上是特定 Spring Integration 通道介面卡的自動配置。

Spring Initializr

快速啟動您的專案

領先一步

VMware 提供培訓和認證,助力您的進步。

瞭解更多

獲取支援

Tanzu Spring 在一項簡單訂閱中為 OpenJDK™、Spring 和 Apache Tomcat® 提供支援和二進位制檔案。

瞭解更多

近期活動

檢視 Spring 社群中的所有近期活動。

檢視全部