集成图

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

{
  "contentDescriptor" : {
    "providerVersion" : "6.4.4",
    "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 meters。旧版指标已在 5.4 版本中移除,将不再出现在图中。

在前面的示例中,该图由三个顶级元素组成。

contentDescriptor 图元素包含有关提供数据的应用程序的通用信息。name 可以在 IntegrationGraphServer bean 上或在 spring.application.name 应用程序上下文环境属性中进行自定义。其他属性由框架提供,可让您区分与其他来源相似的模型。

links 图元素表示来自 nodes 图元素中的节点之间的连接,因此也表示源 Spring Integration 应用中的集成组件之间的连接。例如,从 MessageChannel 到带有某些 MessageHandlerEventDrivenConsumer,或从 AbstractReplyProducingMessageHandlerMessageChannel。为了方便并让您确定链接的目的,该模型包含 type 属性。可能的类型有:

  • input: 表示从 MessageChannel 到端点、inputChannelrequestChannel 属性的方向

  • output: 表示从 MessageHandlerMessageProducerSourcePollingChannelAdapter 通过 outputChannelreplyChannel 属性到 MessageChannel 的方向

  • error: 表示从 PollingConsumerMessageProducerSourcePollingChannelAdapter 上的 MessageHandler 通过 errorChannel 属性到 MessageChannel 的方向;

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

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

此元素中的信息可被可视化工具用于渲染来自 nodes 图元素中节点之间的连接,其中 fromto 数字表示链接节点的 nodeId 属性的值。例如,link 元素可用于确定目标节点上的适当 port

下面的“文本图像”显示了类型之间的关系

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

nodes 图元素也许是最有趣的,因为它的元素不仅包含运行时组件及其 componentType 实例和 name 值,还可以选择包含组件暴露的指标。节点元素包含各种属性,这些属性通常是不言自明的。例如,基于表达式的组件包含 expression 属性,该属性包含组件的主要表达式字符串。要启用指标,请在 @Configuration 类中添加 @EnableIntegrationManagement 或在您的 XML 配置中添加 <int:management/> 元素。有关完整信息,请参阅 指标与管理

nodeId 表示一个唯一的增量标识符,用于区分不同的组件。它也用于 links 元素中,表示此组件与其他组件(如果存在)的关系(连接)。inputoutput 属性分别对应于 AbstractEndpointMessageHandlerSourcePollingChannelAdapterMessageProducerSupportinputChanneloutputChannel 属性。有关更多信息,请参阅下一节。

从 5.1 版本开始,IntegrationGraphServer 接受一个 Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback,用于为特定的 NamedComponentIntegrationNode 上填充附加属性。例如,您可以将 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 也有一个 SourcePollingChannelAdapter 和一个 MessageChannel,用于定期将消息从源数据发送到该通道。其他组件可能是中间件请求-回复组件(例如 JmsOutboundGateway),它们有一个消费型 AbstractEndpoint 用于订阅(或轮询) requestChannel (input) 以获取消息,并有一个 replyChannel (output) 用于生成回复消息发送到下游。同时,任何 MessageProducerSupport 实现(例如 ApplicationEventListeningMessageProducer)都封装了某些源协议监听逻辑并将消息发送到 outputChannel

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

@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 可以使用它来确定如何绘制组件。