集成图

从 4.3 版本开始,Spring Integration 提供了访问应用程序运行时对象模型的功能,该模型可以选择性地包含组件指标。 它以图的形式公开,可用于可视化集成应用程序的当前状态。 o.s.i.support.management.graph 包包含了所有必需的类,用于收集、构建和渲染 Spring Integration 组件的运行时状态,作为一个单一的树状 Graph 对象。 IntegrationGraphServer 应该被声明为一个 bean,用于构建、检索和刷新 Graph 对象。 生成的 Graph 对象可以序列化为任何格式,尽管 JSON 灵活方便,易于在客户端解析和表示。 一个只包含默认组件的 Spring Integration 应用程序将公开一个图,如下所示:

{
  "contentDescriptor" : {
    "providerVersion" : "{project-version}",
    "providerFormatVersion" : 1.2,
    "provider" : "spring-integration",
    "name" : "myAppName:1.0"
  },
  "nodes" : [ {
    "nodeId" : 1,
    "componentType" : "null-channel",
    "integrationPatternType" : "null_channel",
    "integrationPatternCategory" : "messaging_channel",
    "properties" : { },
    "sendTimers" : {
      "successes" : {
        "count" : 1,
        "mean" : 0.0,
        "max" : 0.0
      },
      "failures" : {
        "count" : 0,
        "mean" : 0.0,
        "max" : 0.0
      }
    },
    "receiveCounters" : {
      "successes" : 0,
      "failures" : 0
    },
    "name" : "nullChannel"
  }, {
    "nodeId" : 2,
    "componentType" : "publish-subscribe-channel",
    "integrationPatternType" : "publish_subscribe_channel",
    "integrationPatternCategory" : "messaging_channel",
    "properties" : { },
    "sendTimers" : {
      "successes" : {
        "count" : 1,
        "mean" : 7.807002,
        "max" : 7.807002
      },
      "failures" : {
        "count" : 0,
        "mean" : 0.0,
        "max" : 0.0
      }
    },
    "name" : "errorChannel"
  }, {
    "nodeId" : 3,
    "componentType" : "logging-channel-adapter",
    "integrationPatternType" : "outbound_channel_adapter",
    "integrationPatternCategory" : "messaging_endpoint",
    "properties" : { },
    "output" : null,
    "input" : "errorChannel",
    "sendTimers" : {
      "successes" : {
        "count" : 1,
        "mean" : 6.742722,
        "max" : 6.742722
      },
      "failures" : {
        "count" : 0,
        "mean" : 0.0,
        "max" : 0.0
      }
    },
    "name" : "errorLogger"
  } ],
  "links" : [ {
    "from" : 2,
    "to" : 3,
    "type" : "input"
  } ]
}

版本 5.2 弃用了旧版指标,转而支持 Micrometer 仪表,如 指标管理 中所述。 旧版指标在版本 5.4 中已移除,将不再出现在图中。

在前面的示例中,图由三个顶级元素组成。 contentDescriptor 图元素包含提供数据的应用程序的一般信息。 name 可以在 IntegrationGraphServer bean 或 spring.application.name 应用程序上下文环境属性中进行自定义。 其他属性由框架提供,可让您将类似的模型与其他来源区分开来。 links 图元素表示 nodes 图元素中的节点之间以及源 Spring Integration 应用程序中集成组件之间的连接。 例如,从 MessageChannel 到带有 MessageHandlerEventDrivenConsumer,或从 AbstractReplyProducingMessageHandlerMessageChannel。 为了方便起见并让您确定链接的用途,模型包含 type 属性。 可能的类型包括:

  • input:标识从 MessageChannel 到端点、inputChannelrequestChannel 属性的方向

  • output:从 MessageHandlerMessageProducerSourcePollingChannelAdapterMessageChannel 的方向,通过 outputChannelreplyChannel 属性

  • error:从 PollingConsumerMessageProducerSourcePollingChannelAdapter 上的 MessageHandlerMessageChannel 的方向,通过 errorChannel 属性;

  • discard:从 DiscardingMessageHandler(例如 MessageFilter)到 MessageChannel 的方向,通过 errorChannel 属性。

  • route:从 AbstractMappingMessageRouter(例如 HeaderValueRouter)到 MessageChannel。 类似于 output,但在运行时确定。 可能是配置的通道映射或动态解析的通道。 路由器通常为此目的仅保留最多 100 条动态路由,但您可以通过设置 dynamicChannelLimit 属性来修改此值。

此元素中的信息可由可视化工具用于渲染 nodes 图元素中的节点之间的连接,其中 fromto 数字表示链接节点的 nodeId 属性的值。 例如,link 元素可用于确定目标节点上的正确 port。 以下“文本图像”显示了类型之间的关系:

              +---(discard)
              |
         +----o----+
         |         |
         |         |
         |         |
(input)--o         o---(output)
         |         |
         |         |
         |         |
         +----o----+
              |
              +---(error)

nodes 图元素可能是最有趣的,因为它的元素不仅包含运行时组件及其 componentType 实例和 name 值,还可以选择性地包含组件公开的指标。 节点元素包含各种属性,通常不言自明。 例如,基于表达式的组件包含 expression 属性,其中包含组件的主要表达式字符串。 要启用指标,请将 @EnableIntegrationManagement 添加到 @Configuration 类或将 <int:management/> 元素添加到您的 XML 配置中。 有关完整信息,请参见 指标和管理nodeId 表示一个唯一的增量标识符,可让您区分一个组件和另一个组件。 它也用于 links 元素中,以表示此组件与(如果有)其他组件的关系(连接)。 inputoutput 属性用于 AbstractEndpointMessageHandlerSourcePollingChannelAdapterMessageProducerSupportinputChanneloutputChannel 属性。 有关更多信息,请参见下一节。 从版本 5.1 开始,IntegrationGraphServer 接受一个 Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback,用于在 IntegrationNode 上为特定的 NamedComponent 填充附加属性。 例如,您可以将 SmartLifecycleautoStartuprunning 属性公开到目标图中:

server.setAdditionalPropertiesCallback(namedComponent -> {
            Map<String, Object> properties = null;
            if (namedComponent instanceof SmartLifecycle) {
                SmartLifecycle smartLifecycle = (SmartLifecycle) namedComponent;
                properties = new HashMap<>();
                properties.put("auto-startup", smartLifecycle.isAutoStartup());
                properties.put("running", smartLifecycle.isRunning());
            }
            return properties;
        });

图运行时模型

Spring Integration 组件具有不同级别的复杂性。 例如,任何轮询的 MessageSource 也具有 SourcePollingChannelAdapterMessageChannel,用于定期将消息从源数据发送到其中。 其他组件可能是中间件请求-回复组件(例如 JmsOutboundGateway),它具有一个消费 AbstractEndpoint,用于订阅(或轮询) requestChannel (input) 以获取消息,以及一个 replyChannel (output) 以生成回复消息以向下游发送。 同时,任何 MessageProducerSupport 实现(例如 ApplicationEventListeningMessageProducer)都包装了一些源协议监听逻辑,并将消息发送到 outputChannel

在图中,Spring Integration 组件通过 IntegrationNode 类层次结构表示,您可以在 o.s.i.support.management.graph 包中找到它。 例如,您可以将 ErrorCapableDiscardingMessageHandlerNode 用于 AggregatingMessageHandler(因为它具有 discardChannel 选项),并且在使用 PollingConsumerPollableChannel 消费时可以产生错误。 另一个示例是 CompositeMessageHandlerNode — 对于 MessageHandlerChain,当使用 EventDrivenConsumer 订阅 SubscribableChannel 时。

@MessagingGateway(参见 消息网关)为其每个方法提供节点,其中 name 属性基于网关的 bean 名称和简短方法签名。 考虑以下网关示例:

@MessagingGateway(defaultRequestChannel = "four")
public interface Gate {

	void foo(String foo);

	void foo(Integer foo);

	void bar(String bar);

}

前面的网关生成类似于以下内容的节点:

{
  "nodeId" : 10,
  "name" : "gate.bar(class java.lang.String)",
  "stats" : null,
  "componentType" : "gateway",
  "integrationPatternType" : "gateway",
  "integrationPatternCategory" : "messaging_endpoint",
  "output" : "four",
  "errors" : null
},
{
  "nodeId" : 11,
  "name" : "gate.foo(class java.lang.String)",
  "stats" : null,
  "componentType" : "gateway",
  "integrationPatternType" : "gateway",
  "integrationPatternCategory" : "messaging_endpoint",
  "output" : "four",
  "errors" : null
},
{
  "nodeId" : 12,
  "name" : "gate.foo(class java.lang.Integer)",
  "stats" : null,
  "componentType" : "gateway",
  "integrationPatternType" : "gateway",
  "integrationPatternCategory" : "messaging_endpoint",
  "output" : "four",
  "errors" : null
}

您可以使用此 IntegrationNode 层次结构在客户端解析图模型,并了解一般的 Spring Integration 运行时行为。 另请参见 编程技巧和窍门 以获取更多信息。

版本 5.3 引入了 IntegrationPattern 抽象,所有开箱即用的组件(代表企业集成模式 (EIP))都实现了此抽象并提供了 IntegrationPatternType 枚举值。 此信息对于目标应用程序中的某些分类逻辑可能很有用,或者,当暴露到图节点中时,它可以被 UI 用于确定如何绘制组件。