集成图控制器

如果你的应用是基于 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="http://localhost: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="http://localhost: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 映射。