集成图

从版本 4.3 开始, Spring 集成提供了对应用程序的运行时对象模型的访问,该模型可以选择包括组件度量。 它以图表的形式公开,可用于可视化集成应用程序的当前状态。 这o.s.i.support.management.graphpackage 包含收集、构建和渲染 Spring 集成组件的运行时状态所需的所有类Graph对象。 这IntegrationGraphServer应声明为 Bean 来构建、检索和刷新Graph对象。 结果Graphobject 可以序列化为任何格式,尽管 JSON 在客户端解析和表示起来很灵活和方便。 仅具有默认组件的 Spring 集成应用程序将公开一个图形,如下所示:spring-doc.cadn.net.cn

{
  "contentDescriptor" : {
    "providerVersion" : "6.4.2",
    "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 弃用了旧指标,转而使用千分尺,如 Metrics Management 中所述。 旧版指标已在版本 5.4 中删除,将不再显示在图表中。

在前面的示例中,图形由三个顶级元素组成。spring-doc.cadn.net.cn

contentDescriptorgraph 元素包含有关提供数据的应用程序的一般信息。 这name可以在IntegrationGraphServerbean 或spring.application.nameapplication context environment 属性。 框架提供了其他属性,并允许您将类似模型与其他源区分开来。spring-doc.cadn.net.cn

linksgraph 元素表示节点之间的连接,来自nodesgraph 元素,因此,在源 Spring Integration 应用程序中的集成组件之间。 例如,从MessageChannel更改为EventDrivenConsumer与一些MessageHandler或从AbstractReplyProducingMessageHandler更改为MessageChannel. 为了方便起见并让您确定链接的用途,该模型包括type属性。 可能的类型包括:spring-doc.cadn.net.cn

  • input:标识方向MessageChannel到终端节点,inputChannelrequestChannel财产spring-doc.cadn.net.cn

  • output:从MessageHandler,MessageProducerSourcePollingChannelAdapterMessageChannel通过outputChannelreplyChannel财产spring-doc.cadn.net.cn

  • error:从MessageHandlerPollingConsumerMessageProducerSourcePollingChannelAdapterMessageChannel通过errorChannel财产;spring-doc.cadn.net.cn

  • discard:从DiscardingMessageHandler(例如MessageFilter) 到MessageChannel通过errorChannel财产。spring-doc.cadn.net.cn

  • route:从AbstractMappingMessageRouter(例如HeaderValueRouter) 到MessageChannel. 似output但在运行时确定。 可能是配置的 channel mapping 或动态解析的 channel。 为此,路由器通常最多只保留 100 个动态路由,但您可以通过设置dynamicChannelLimit财产。spring-doc.cadn.net.cn

可视化工具可以使用此元素中的信息来呈现节点之间的连接,这些节点来自nodesgraph 元素,其中fromto数字表示nodeId属性。 例如,link元素可用于确定适当的port在目标节点上。spring-doc.cadn.net.cn

下面的 “text image” 显示了类型之间的关系:spring-doc.cadn.net.cn

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

nodesgraph 元素可能是最有趣的,因为它的元素不仅包含运行时组件及其componentTypeinstances 和name值,但也可以选择包含组件公开的指标。 Node 元素包含各种属性,这些属性通常一目了然。 例如,基于表达式的组件包括expression包含组件的主表达式字符串的属性。 要启用这些指标,请添加@EnableIntegrationManagement更改为@Configuration类或添加<int:management/>元素添加到 XML 配置中。 有关完整信息,请参阅 Metrics and Managementspring-doc.cadn.net.cn

nodeId表示唯一的增量标识符,以便将一个组件与另一个组件区分开来。 它还用于links元素来表示此组件与其他组件的关系(连接)(如果有)。 这inputoutputattributes 用于inputChanneloutputChannel的属性AbstractEndpoint,MessageHandler,SourcePollingChannelAdapterMessageProducerSupport. 有关更多信息,请参阅下一节。spring-doc.cadn.net.cn

从版本 5.1 开始,IntegrationGraphServer接受Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback对于IntegrationNode对于特定的NamedComponent. 例如,您可以公开SmartLifecycle autoStartuprunning属性添加到目标图中:spring-doc.cadn.net.cn

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 运行时模型

Spring 集成组件具有不同的复杂程度。 例如,任何轮询的MessageSource还有一个SourcePollingChannelAdapter以及MessageChannel定期从源数据向其发送消息。 其他组件可能是中间件请求-回复组件(例如JmsOutboundGateway) 替换为AbstractEndpoint要订阅(或轮询)该requestChannel (input) 用于消息,并使用replyChannel (output) 生成要向下游发送的回复消息。 同时,任何MessageProducerSupportimplementation (例如ApplicationEventListeningMessageProducer) 包装一些源协议侦听逻辑,并将消息发送到outputChannel.spring-doc.cadn.net.cn

在图中, Spring 集成组件通过使用IntegrationNode类层次结构,您可以在o.s.i.support.management.graph包。 例如,您可以使用ErrorCapableDiscardingMessageHandlerNode对于AggregatingMessageHandler(因为它有一个discardChannel选项),并且在从PollableChannel通过使用PollingConsumer. 另一个例子是CompositeMessageHandlerNode— 对于MessageHandlerChain当订阅了SubscribableChannel通过使用EventDrivenConsumer.spring-doc.cadn.net.cn

@MessagingGateway(请参阅 Messaging Gateway)为其每个方法提供节点,其中name属性基于网关的 bean 名称和短方法签名。 请考虑以下网关示例:
@MessagingGateway(defaultRequestChannel = "four")
public interface Gate {

	void foo(String foo);

	void foo(Integer foo);

	void bar(String bar);

}

前面的网关生成类似于以下内容的节点:spring-doc.cadn.net.cn

{
  "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层次结构,用于解析 Client 端的图形模型以及了解一般的 Spring 集成运行时行为。 有关更多信息,另请参阅编程提示和技巧spring-doc.cadn.net.cn

版本 5.3 引入了IntegrationPatternabstraction 和所有代表企业集成模式 (EIP) 的开箱即用组件实现此抽象并提供IntegrationPatternTypeenum 值。 此信息对于目标应用程序中的某些分类逻辑非常有用,或者,如果公开到图形节点中,则 UI 可以使用此信息来确定如何绘制组件。spring-doc.cadn.net.cn