Spring IntegrationSpring Integration7.0.0

擴充套件 Spring 程式設計模型,以支援眾所周知的企業整合模式。Spring Integration 可以在基於 Spring 的應用程式中實現輕量級訊息傳遞,並透過宣告式介面卡支援與外部系統的整合。這些介面卡在 Spring 對遠端呼叫、訊息傳遞和排程的支援之上提供更高層次的抽象。Spring Integration 的主要目標是提供一個簡單的模型來構建企業整合解決方案,同時保持關注點分離,這對於生成可維護、可測試的程式碼至關重要。

簡介

使用 Spring 框架鼓勵開發人員使用介面進行編碼,並使用依賴注入 (DI) 為普通的 Java 物件 (POJO) 提供其執行任務所需的依賴項。Spring Integration 更進一步,透過訊息傳遞正規化將 POJO 組合在一起,各個元件可能不知道應用程式中的其他元件。這樣的應用程式是透過組裝細粒度可重用元件來形成更高層次的功能而構建的。透過精心設計,這些流可以模組化並在更高層次上重用。

除了將細粒度元件連線起來,Spring Integration 還提供了多種通道介面卡和閘道器來與外部系統通訊。通道介面卡用於單向整合(傳送或接收);閘道器用於請求/回覆場景(入站或出站)。有關介面卡和閘道器的完整列表,請參閱參考文件。

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 社群所有即將舉行的活動。

檢視所有