集成图
从 4.3 版本开始,Spring Integration 提供了对应用程序运行时对象模型的访问,该模型可选择包含组件指标。它以图的形式公开,可用于可视化集成应用程序的当前状态。o.s.i.support.management.graph 包包含收集、构建和渲染 Spring Integration 组件运行时状态为单个树状 Graph 对象所需的所有类。IntegrationGraphServer 应声明为一个 bean,用于构建、检索和刷新 Graph 对象。生成的 Graph 对象可以序列化为任何格式,尽管 JSON 灵活方便解析和在客户端呈现。一个只有默认组件的 Spring Integration 应用程序将公开的图如下:
{
"contentDescriptor" : {
"providerVersion" : "7.0.0",
"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 到带有某个 MessageHandler 的 EventDrivenConsumer,或从 AbstractReplyProducingMessageHandler 到 MessageChannel。为方便起见,并让您确定链接的用途,模型中包含 type 属性。可能的类型如下:
-
input:标识从MessageChannel到端点、inputChannel或requestChannel属性的方向 -
output:通过outputChannel或replyChannel属性,从MessageHandler、MessageProducer或SourcePollingChannelAdapter到MessageChannel的方向 -
error:通过errorChannel属性,从PollingConsumer上的MessageHandler或MessageProducer或SourcePollingChannelAdapter到MessageChannel的方向; -
discard:通过errorChannel属性,从DiscardingMessageHandler(如MessageFilter)到MessageChannel的方向。 -
route:从AbstractMappingMessageRouter(例如HeaderValueRouter)到MessageChannel。与output类似,但在运行时确定。可能是配置的通道映射或动态解析的通道。为此目的,路由器通常仅保留多达 100 个动态路由,但您可以通过设置dynamicChannelLimit属性来修改此值。
此元素中的信息可由可视化工具用于渲染 nodes 图元素中节点之间的连接,其中 from 和 to 数字表示链接节点的 nodeId 属性的值。例如,link 元素可用于确定目标节点上的正确 port。
以下“文本图像”显示了类型之间的关系:
+---(discard)
|
+----o----+
| |
| |
| |
(input)--o o---(output)
| |
| |
| |
+----o----+
|
+---(error)
nodes 图元素可能最有趣,因为它的元素不仅包含运行时组件及其 componentType 实例和 name 值,还可以选择包含组件公开的指标。节点元素包含各种属性,这些属性通常不言自明。例如,基于表达式的组件包含 expression 属性,其中包含组件的主要表达式字符串。要启用指标,请将 @EnableIntegrationManagement 添加到 @Configuration 类,或将 <int:management/> 元素添加到您的 XML 配置中。有关完整信息,请参阅指标和管理。
nodeId 表示一个唯一的增量标识符,可让您区分一个组件和另一个组件。它还用于 links 元素中,表示此组件与其他组件(如果有)的关系(连接)。input 和 output 属性用于 AbstractEndpoint、MessageHandler、SourcePollingChannelAdapter 或 MessageProducerSupport 的 inputChannel 和 outputChannel 属性。有关更多信息,请参阅下一节。
从 5.1 版本开始,IntegrationGraphServer 接受一个 Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback,用于为特定 NamedComponent 的 IntegrationNode 填充其他属性。例如,您可以将 SmartLifecycle 的 autoStartup 和 running 属性公开到目标图中:
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 选项),并且在使用 PollingConsumer 从 PollableChannel 消费时可以产生错误。另一个例子是 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 可以使用它来确定如何绘制组件。