|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
集成图
从版本 4.3 开始,Spring Integration 提供了对应用程序运行时对象模型的访问权限,该模型可选择性地包含组件指标。
它以图形形式公开,可用于可视化集成应用程序的当前状态。
o.s.i.support.management.graph 包包含所有必要的类,用于收集、构建并以单个树状 Graph 对象的形式呈现 Spring Integration 组件的运行时状态。
应声明 IntegrationGraphServer 为 Bean,以构建、检索和刷新 Graph 对象。
生成的 Graph 对象可以序列化为任何格式,尽管 JSON 在客户端解析和表示方面具有灵活性和便利性。
仅包含默认组件的 Spring Integration 应用程序将按如下方式公开图形:
{
"contentDescriptor" : {
"providerVersion" : "6.4.10",
"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可以在IntegrationGraphServerbean上或spring.application.name应用程序上下文环境属性中进行自定义。
其他属性由框架提供,用于将相似模型与其他来源区分开来。
links图元素表示来自nodes图元素的节点之间的连接,因此也表示源 Spring Integration 应用程序中集成组件之间的连接。
例如,从MessageChannel到EventDrivenConsumer,中间带有某些MessageHandler,或从AbstractReplyProducingMessageHandler到MessageChannel。
为了方便并帮助您确定链接的用途,模型包含了type属性。
可能的类型包括:
-
input: 标识从MessageChannel到终点inputChannel或requestChannel属性的方向 -
output: 从MessageHandler、MessageProducer或SourcePollingChannelAdapter通过outputChannel或replyChannel属性到MessageChannel的方向 -
error: 从MessageHandler在PollingConsumer或MessageProducer或SourcePollingChannelAdapter到MessageChannel,通过errorChannel属性; -
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),包含一个用于订阅(或轮询)requestChannel(input)以获取消息的消费端AbstractEndpoint,以及一个用于生成要向下发送的响应消息的生产端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 可利用它来确定如何绘制组件。