搶先一步
VMware 提供培訓和認證,以加速您的進度。
瞭解更多擴展 Spring 程式設計模型以支援廣為人知的企業整合模式。Spring Integration 在基於 Spring 的應用程式中實現輕量級訊息傳遞,並透過宣告式配接器支援與外部系統的整合。這些配接器在 Spring 對於遠端處理、訊息傳遞和排程的支援之上,提供了更高層次的抽象。Spring Integration 的主要目標是提供一個簡單的模型來建構企業整合解決方案,同時保持關注點分離,這對於產生可維護、可測試的程式碼至關重要。
使用 Spring Framework 鼓勵開發人員使用介面進行編碼,並使用依賴注入 (DI) 為 Plain Old Java Object (POJO) 提供執行其任務所需的依賴項。Spring Integration 將此概念更進一步,其中 POJO 使用訊息傳遞範例連接在一起,並且個別元件可能不知道應用程式中的其他元件。這樣的應用程式是透過組裝細粒度的可重複使用元件來形成更高層次的功能而建構的。透過仔細的設計,這些流程可以模組化,並在更高的層次上重複使用。
除了將細粒度元件連接在一起之外,Spring Integration 還提供了多種管道配接器和閘道器,用於與外部系統通訊。管道配接器用於單向整合(傳送或接收);閘道器用於請求/回覆情境(入站或出站)。如需配接器和閘道器的完整清單,請參閱參考文件。
Spring Cloud Stream 專案以 Spring Integration 為基礎建構,其中 Spring Integration 用作訊息驅動微服務的引擎。
實作大多數企業整合模式
端點
管道(點對點和發佈/訂閱)
彙總器
篩選器
轉換器
控制匯流排
…
與外部系統整合
ReST/HTTP
FTP/SFTP
STOMP
WebServices (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://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>
這是相同的應用程式(Web 服務部分),使用 Java DSL(和 Spring Boot)。如果您不使用 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://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\"]"));
}
}