整合圖控制器

如果你的應用是基於 Web 的(或基於 Spring Boot 構建並帶有嵌入式 Web 容器),並且 classpath 中存在 Spring Integration HTTP 或 WebFlux 模組(分別參見 HTTP 支援WebFlux 支援),則可以使用 IntegrationGraphControllerIntegrationGraphServer 功能作為 REST 服務公開。為此目的,HTTP 模組中提供了 @EnableIntegrationGraphController@Configuration 類註解以及 <int-http:graph-controller/> XML 元素。結合 @EnableWebMvc 註解(或用於 XML 定義的 <mvc:annotation-driven/>),此配置註冊一個 IntegrationGraphController @RestController,其 @RequestMapping.path 可以在 @EnableIntegrationGraphController 註解或 <int-http:graph-controller/> 元素上配置。預設路徑是 /integration

IntegrationGraphController @RestController 提供以下服務

  • @GetMapping(name = "getGraph"):用於檢索自上次 IntegrationGraphServer 重新整理以來 Spring Integration 元件的狀態。o.s.i.support.management.graph.Graph 作為 REST 服務的 @ResponseBody 返回。

  • @GetMapping(path = "/refresh", name = "refreshGraph"):用於重新整理當前 Graph 以反映實際執行時狀態,並將其作為 REST 響應返回。無需為指標重新整理圖。在檢索圖時會即時提供指標。如果應用上下文自上次檢索圖以來已被修改,則可以呼叫重新整理。在這種情況下,圖將被完全重建。

你可以使用 Spring Security 和 Spring MVC 專案提供的標準配置選項和元件,為 IntegrationGraphController 設定安全和跨域限制。以下示例實現了這些目標

<mvc:annotation-driven />

<mvc:cors>
	<mvc:mapping path="/myIntegration/**"
				 allowed-origins="https://:9090"
				 allowed-methods="GET" />
</mvc:cors>

<security:http>
    <security:intercept-url pattern="/myIntegration/**" access="ROLE_ADMIN" />
</security:http>


<int-http:graph-controller path="/myIntegration" />

以下示例展示瞭如何使用 Java 配置做同樣的事情

@Configuration
@EnableWebMvc // or @EnableWebFlux
@EnableWebSecurity // or @EnableWebFluxSecurity
@EnableIntegration
@EnableIntegrationGraphController(path = "/testIntegration", allowedOrigins="https://:9090")
public class IntegrationConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
	    http
            .authorizeRequests()
               .antMatchers("/testIntegration/**").hasRole("ADMIN")
            // ...
            .formLogin();
    }

    //...

}

請注意,為了方便起見,@EnableIntegrationGraphController 註解提供了一個 allowedOrigins 屬性。這為 path 提供了 GET 訪問。如需更復雜的配置,你可以使用標準的 Spring MVC 機制來配置 CORS 對映。