集成图
从 4.3 版本开始,Spring Integration 提供了对应用程序运行时对象模型的访问,该模型可选地包含组件指标。它以图形的形式暴露出来,可用于可视化集成应用程序的当前状态。o.s.i.support.management.graph 包包含所有必需的类,用于收集、构建和渲染 Spring Integration 组件的运行时状态为单个树状 Graph 对象。应将 IntegrationGraphServer 声明为一个 bean 来构建、检索和刷新 Graph 对象。生成的 Graph 对象可以序列化为任何格式,尽管 JSON 是灵活且方便解析和在客户端表示的格式。仅带有默认组件的 Spring Integration 应用程序将公开如下所示的图形:
{
"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 中讨论的 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: 从MessageHandler,MessageProducer或SourcePollingChannelAdapter到MessageChannel的方向,通过outputChannel或replyChannel属性 -
error: 从MessageHandler在PollingConsumer或MessageProducer或SourcePollingChannelAdapter到MessageChannel通过errorChannel属性; -
discard: 从DiscardingMessageHandler(如MessageFilter)到MessageChannel通过errorChannel属性。 -
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 属性。要启用指标,可以在 @Configuration 类中添加 @EnableIntegrationManagement 或在 XML 配置中添加 <int:management/> 元素。完整信息请参阅 Metrics and Management。
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 ),它有一个用于订阅(或轮询) requestChannel (输入)以获取消息的消费型 AbstractEndpoint ,以及一个 replyChannel (输出)以生成回复消息并发送到下游。同时,任何 MessageProducerSupport 实现(如 ApplicationEventListeningMessageProducer )都封装了一些源协议监听逻辑,并将消息发送到 outputChannel 。
在图中,Spring Integration 组件通过使用 IntegrationNode 类层次结构来表示,你可以在 o.s.i.support.management.graph 包中找到它。例如,你可以为 AggregatingMessageHandler 使用 ErrorCapableDiscardingMessageHandlerNode(因为它有一个 discardChannel 选项),并且当从 PollableChannel 消费时,可以通过使用 PollingConsumer 产生错误。另一个例子是 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 可以使用它来确定如何绘制组件。