Integration Graph

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

Starting with version 4.3, Spring Integration provides access to an application’s runtime object model, which can, optionally, include component metrics. It is exposed as a graph, which may be used to visualize the current state of the integration application. The o.s.i.support.management.graph package contains all the required classes to collect, build, and render the runtime state of Spring Integration components as a single tree-like Graph object. The IntegrationGraphServer should be declared as a bean to build, retrieve, and refresh the Graph object. The resulting Graph object can be serialized to any format, although JSON is flexible and convenient to parse and represent on the client side. A Spring Integration application with only the default components would expose a graph as follows:

{
  "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"
  } ]
}

Metrics Management 所讨论的,版本 5.2 以较新的 Micrometer 测量值弃用了旧指标。旧指标已在版本 5.4 中删除,并且将不再出现在图形中。

Version 5.2 deprecated the legacy metrics in favor of Micrometer meters as discussed Metrics Management. The legacy metrics were removed in Version 5.4 and will no longer appear in the graph.

在上例中,该图形包含三个顶层元素。

In the preceding example, the graph consists of three top-level elements.

contentDescriptor 图形元素包含提供数据的应用程序的常规信息。可以在 IntegrationGraphServer bean 或 spring.application.name 应用程序上下文环境属性中定制 name。其他属性由框架提供,让您将类似的模型与其他来源区分开来。

The contentDescriptor graph element contains general information about the application providing the data. The name can be customized on the IntegrationGraphServer bean or in the spring.application.name application context environment property. Other properties are provided by the framework and let you distinguish a similar model from other sources.

links 图形元素表示 nodes 图形元素中节点之间的连接,因此也表示源 Spring 集成应用程序中集成组件之间的连接。例如,从一个 MessageChannel 通过某种 MessageHandler 到一个 EventDrivenConsumer 或从一个 AbstractReplyProducingMessageHandler 到一个 MessageChannel。为了方便起见,并帮助您确定链接的目的,该模型包括 type 属性。可能的类型包括:

The links graph element represents connections between nodes from the nodes graph element and, therefore, between integration components in the source Spring Integration application. For example, from a MessageChannel to an EventDrivenConsumer with some MessageHandler or from an AbstractReplyProducingMessageHandler to a MessageChannel. For convenience and to let you determine a link’s purpose, the model includes the type attribute. The possible types are:

  • input: Identifies the direction from MessageChannel to the endpoint, inputChannel, or requestChannel property

  • output: The direction from the MessageHandler, MessageProducer, or SourcePollingChannelAdapter to the MessageChannel through an outputChannel or replyChannel property

  • error: From MessageHandler on PollingConsumer or MessageProducer or SourcePollingChannelAdapter to the MessageChannel through an errorChannel property;

  • discard: From DiscardingMessageHandler (such as MessageFilter) to the MessageChannel through an errorChannel property.

  • route: From AbstractMappingMessageRouter (such as HeaderValueRouter) to the MessageChannel. Similar to output but determined at run-time. Maybe a configured channel mapping or a dynamically resolved channel. Routers typically retain only up to 100 dynamic routes for this purpose, but you can modify this value by setting the dynamicChannelLimit property.

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

The information from this element can be used by a visualization tool to render connections between nodes from the nodes graph element, where the from and to numbers represent the value from the nodeId property of the linked nodes. For example, the link element can be used to determine the proper port on the target node.

以下“文本图像”展示了类型之间的关系:

The following “text image” shows the relationships between the types:

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

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

The nodes graph element is perhaps the most interesting, because its elements contain not only the runtime components with their componentType instances and name values but can also optionally contain metrics exposed by the component. Node elements contain various properties that are generally self-explanatory. For example, expression-based components include the expression property that contains the primary expression string for the component. To enable the metrics, add an @EnableIntegrationManagement to a @Configuration class or add an <int:management/> element to your XML configuration. See Metrics and Management for complete information.

nodeId 代表一个唯一的自增标识符,让你能区分一个组件和另一个组件。如果组件与其他组件有关(连接),它也会在 links 元素中表示这种关系。inputoutput 特性适用于 AbstractEndpointMessageHandlerSourcePollingChannelAdapterMessageProducerSupportinputChanneloutputChannel 属性。更多信息,见下一章节。

The nodeId represents a unique incremental identifier to let you distinguish one component from another. It is also used in the links element to represent a relationship (connection) of this component to others, if any. The input and output attributes are for the inputChannel and outputChannel properties of the AbstractEndpoint, MessageHandler, SourcePollingChannelAdapter, or MessageProducerSupport. See the next section for more information.

从版本 5.1 起,IntegrationGraphServer 接受一个 Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback,用于填充特定 NamedComponentIntegrationNode 上的附加属性。例如,你可以将 SmartLifecycle autoStartuprunning 属性暴露到目标图中:

Starting with version 5.1, the IntegrationGraphServer accepts a Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback for population of additional properties on the IntegrationNode for a particular NamedComponent. For example, you can expose the SmartLifecycle autoStartup and running properties into the target graph:

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;
        });

Graph Runtime Model

Spring 集成组件具有各种复杂性等级。例如,任何轮询 MessageSource 都有一个 SourcePollingChannelAdapter,还有一个 MessageChannel,用于从源数据定期发送消息。其他组件可能是中间件请求-响应组件(如 JmsOutboundGateway),带有用于订阅(或轮询)requestChannel (input) 的消息的使用者 AbstractEndpoint,和用于生成下游回复消息的 replyChannel (output)。同时,任何 MessageProducerSupport 实现(如 ApplicationEventListeningMessageProducer)都会封装一些源协议侦听逻辑,并将消息发送到 outputChannel

Spring Integration components have various levels of complexity. For example, any polled MessageSource also has a SourcePollingChannelAdapter and a MessageChannel to which to periodically send messages from the source data. Other components might be middleware request-reply components (such as JmsOutboundGateway) with a consuming AbstractEndpoint to subscribe to (or poll) the requestChannel (input) for messages, and a replyChannel (output) to produce a reply message to send downstream. Meanwhile, any MessageProducerSupport implementation (such as ApplicationEventListeningMessageProducer) wraps some source protocol listening logic and sends messages to the outputChannel.

在图中,Spring 集成组件使用 IntegrationNode 类层次结构来表示,你可以在 o.s.i.support.management.graph 包中找到它。例如,你可以使用 ErrorCapableDiscardingMessageHandlerNode 处理 AggregatingMessageHandler(因为它有一个 discardChannel 选项),并且可以使用 PollingConsumer 消耗 PollableChannel 时生成错误。另一个示例是 CompositeMessageHandlerNode——对于使用 EventDrivenConsumer 订阅 SubscribableChannelMessageHandlerChain

Within the graph, Spring Integration components are represented by using the IntegrationNode class hierarchy, which you can find in the o.s.i.support.management.graph package. For example, you can use the ErrorCapableDiscardingMessageHandlerNode for the AggregatingMessageHandler (because it has a discardChannel option) and can produce errors when consuming from a PollableChannel by using a PollingConsumer. Another example is CompositeMessageHandlerNode — for a MessageHandlerChain when subscribed to a SubscribableChannel by using an EventDrivenConsumer.

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

The @MessagingGateway (see Messaging Gateways) provides nodes for each of its method, where the name attribute is based on the gateway’s bean name and the short method signature. Consider the following example of a gateway:

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

	void foo(String foo);

	void foo(Integer foo);

	void bar(String bar);

}

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

The preceding gateway produces nodes similar to the following:

{
  "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 的一般运行时行为。有关详细信息,另请参阅 Programming Tips and Tricks

You can use this IntegrationNode hierarchy for parsing the graph model on the client side as well as to understand the general Spring Integration runtime behavior. See also Programming Tips and Tricks for more information.

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

Version 5.3 introduced an IntegrationPattern abstraction and all out-of-the-box components, which represent an Enterprise Integration Pattern (EIP), implement this abstraction and provide an IntegrationPatternType enum value. This information can be useful for some categorizing logic in the target application or, being exposed into the graph node, it can be used by a UI to determine how to draw the component.