集成关系图
从版本 4.3 开始,Spring Integration 提供了对应用程序运行时对象模型的访问,该模型可选择性地包含组件指标。它以图的形式公开,可用于可视化集成应用程序的当前状态。o.s.i.support.management.graph 包包含所有必需的类,用于收集、构建和呈现 Spring Integration 组件的运行时状态,作为一个单一的树状 Graph 对象。应声明 IntegrationGraphServer 作为 bean 来构建、检索和刷新 Graph 对象。生成的 Graph 对象可以序列化为任何格式,尽管 JSON 在客户端解析和表示方面既灵活又方便。一个仅包含默认组件的 Spring Integration 应用程序将暴露如下所示的图:
{
"contentDescriptor" : {
"providerVersion" : "7.0.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 已弃用旧版指标,转而采用 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通过outputChannel或replyChannel属性到MessageChannel的方向 -
error:从PollingConsumer或MessageProducer或SourcePollingChannelAdapter上的MessageHandler通过errorChannel属性到MessageChannel的方向 -
discard:从DiscardingMessageHandler(例如MessageFilter)通过errorChannel属性到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 属性,该属性存储组件的主要表达式字符串。要启用指标功能,请在 @Configuration 类上添加 @EnableIntegrationManagement 注解,或在 XML 配置中添加 <int: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),它包含一个消费型 AbstractEndpoint 来订阅(或轮询)requestChannel(input)以获取消息,以及一个 replyChannel(output)来生成应答消息并向下游发送。同时,任何 MessageProducerSupport 实现(例如 ApplicationEventListeningMessageProducer)都封装了某些源协议监听逻辑,并将消息发送到 outputChannel。
在图中,Spring Integration 组件通过 IntegrationNode 类层次结构表示,您可以在 o.s.i.support.management.graph 包中找到这些类。例如,您可以为 AggregatingMessageHandler 使用 ErrorCapableDiscardingMessageHandlerNode(因为它具有 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)用于确定如何绘制组件。