Skip to content

Spring Integration Graph Controller 使用教程

概述

Integration Graph Controller 是 Spring Integration HTTP 模块的关键组件,它允许开发者通过 REST API 可视化监控整个集成应用的运行时拓扑结构。当你的系统包含多个消息通道、过滤器和转换器时,这个工具就像一张实时更新的电路图,帮助你快速诊断消息流问题。

应用场景

  • 调试复杂消息流时快速定位瓶颈
  • 新成员理解系统集成架构
  • 生产环境实时监控消息处理状态
  • 验证配置变更后的拓扑结构

启用 Integration Graph Controller

通过注解启用(推荐)

kotlin
@EnableIntegrationGraphController // 关键注解
@SpringBootApplication
class IntegrationApp

fun main(args: Array<String>) {
    runApplication<IntegrationApp>(*args)
}

替代 XML 配置(历史项目迁移)

kotlin
@Configuration
class LegacyXmlConfig {
    @Bean
    fun graphController() = IntegrationGraphController()
}

版本兼容性

此功能需要 Spring Integration 4.3+。使用前请确认依赖版本:

gradle
implementation("org.springframework.integration:spring-integration-http:5.5.+")

访问集成拓扑图

REST API 端点

启用后自动暴露以下端点:

端点路径方法描述
/integration/graphGET获取完整的集成拓扑JSON
/integration/graph/{id}GET获取特定组件的详细信息

查看拓扑示例

bash
curl http://localhost:8080/integration/graph | jq .

典型响应结构

json
{
  "contentDescriptor": {
    "providerVersion": "5.5.15"
  },
  "nodes": [
    {
      "id": "inputChannel",
      "name": "httpInputChannel",  // 消息通道
      "componentType": "channel"
    },
    {
      "id": "transformEndpoint",
      "name": "jsonTransformer",   // 转换器
      "componentType": "transformer"
    },
    {
      "id": "errorHandler",
      "name": "dlqHandler",         // 错误处理器
      "componentType": "service-activator"
    }
  ],
  "links": [
    {
      "from": "inputChannel",
      "to": "transformEndpoint"     // 连接关系
    }
  ]
}

实战:订单处理系统监控

系统架构时序图

关键组件配置示例

kotlin
@Bean
fun orderChannel(): MessageChannel {
    return DirectChannel() // 直连通道
}

@Bean
fun dlqChannel(): PublishSubscribeChannel {
    return PublishSubscribeChannel() // 发布订阅通道
}
kotlin
@Bean
@Transformer(inputChannel = "orderChannel", outputChannel = "validationChannel")
fun jsonTransformer(): GenericTransformer<String, Order> {
    return GenericTransformer { payload -> 
        objectMapper.readValue(payload, Order::class.java) 
    }
}

@Bean
@ServiceActivator(inputChannel = "errorChannel")
fun errorHandler(): MessageHandler {
    return MessageHandler { message -> 
        logger.error("处理失败: {}", message)
    } // 实际生产需添加死信队列逻辑
}

最佳实践

安全配置(必须)

kotlin
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {
    override fun configure(http: HttpSecurity) {
        http
            .authorizeRequests()
            .antMatchers("/integration/**").hasRole("ADMIN") 
            .and()
            .httpBasic()
    }
}

生产环境警告

未保护的 /integration/** 端点会暴露系统内部结构,必须配置访问控制!

性能优化建议

kotlin
@Configuration
class GraphConfig {
    @Bean
    fun graphController(): IntegrationGraphController {
        return IntegrationGraphController().apply {
            setRebuildTimeout(5000) // 设置拓扑重建超时
        }
    }
}

监控策略

  1. 低频访问:拓扑结构不会频繁变化,建议每5分钟获取一次
  2. 异常时触发:当消息积压时主动获取当前拓扑
  3. 配置变更后:验证新配置是否生效

常见问题排查

端点404错误

  1. 确认添加 @EnableIntegrationGraphController
  2. 检查包扫描路径是否包含配置类
  3. 验证依赖版本:spring-integration-http ≥ 4.3

拓扑信息不完整

kotlin
@Bean
fun integrationGraphServer(): IntegrationGraphServer {
    return IntegrationGraphServer(true) // 强制包含所有组件
}

组件状态不更新

kotlin
@Component
class OrderProcessor {
    @ServiceActivator(inputChannel = "orderChannel")
    fun process(order: Order) {
        // 确保方法未标记为private
    }
}

总结

Integration Graph Controller 相当于给 Spring Integration 应用安装了 X光透视仪。通过本教程您已掌握:

✅ 使用 @EnableIntegrationGraphController 激活功能
✅ 通过 /integration/graph 端点获取拓扑数据
✅ 配置安全策略保护敏感信息
✅ 结合可视化工具实现运行时监控

扩展知识:拓扑可视化工具

推荐集成方案:

最终效果参考:Spring Integration 官方样例