整合圖

從 4.3 版本開始,Spring Integration 提供了訪問應用程式執行時物件模型的功能,該模型可選地包含元件指標。它以圖的形式暴露出來,可用於視覺化整合應用程式的當前狀態。o.s.i.support.management.graph 包包含了收集、構建和渲染 Spring Integration 元件執行時狀態為單個樹狀 Graph 物件所需的所有類。應該將 IntegrationGraphServer 宣告為一個 bean,以便構建、檢索和重新整理 Graph 物件。生成的 Graph 物件可以序列化為任何格式,儘管 JSON 靈活且便於在客戶端解析和表示。一個僅包含預設元件的 Spring Integration 應用將暴露如下所示的圖:

{
  "contentDescriptor" : {
    "providerVersion" : "6.4.4",
    "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 meters。舊版指標已在 5.4 版本中移除,將不再出現在圖中。

在前面的示例中,該圖由三個頂級元素組成。

contentDescriptor 圖元素包含有關提供資料的應用程式的通用資訊。name 可以在 IntegrationGraphServer bean 上或在 spring.application.name 應用程式上下文環境屬性中進行自定義。其他屬性由框架提供,可讓您區分與其他來源相似的模型。

links 圖元素表示來自 nodes 圖元素中的節點之間的連線,因此也表示源 Spring Integration 應用中的整合元件之間的連線。例如,從 MessageChannel 到帶有某些 MessageHandlerEventDrivenConsumer,或從 AbstractReplyProducingMessageHandlerMessageChannel。為了方便並讓您確定連結的目的,該模型包含 type 屬性。可能的型別有:

  • input: 表示從 MessageChannel 到端點、inputChannelrequestChannel 屬性的方向

  • output: 表示從 MessageHandlerMessageProducerSourcePollingChannelAdapter 透過 outputChannelreplyChannel 屬性到 MessageChannel 的方向

  • error: 表示從 PollingConsumerMessageProducerSourcePollingChannelAdapter 上的 MessageHandler 透過 errorChannel 屬性到 MessageChannel 的方向;

  • discard: 表示從 DiscardingMessageHandler (例如 MessageFilter) 透過 errorChannel 屬性到 MessageChannel 的方向。

  • route: 表示從 AbstractMappingMessageRouter (例如 HeaderValueRouter) 到 MessageChannel 的方向。類似於 output,但在執行時確定。可能是配置的通道對映或動態解析的通道。路由器通常為此目的僅保留最多 100 個動態路由,但您可以透過設定 dynamicChannelLimit 屬性來修改此值。

此元素中的資訊可被視覺化工具用於渲染來自 nodes 圖元素中節點之間的連線,其中 fromto 數字表示連結節點的 nodeId 屬性的值。例如,link 元素可用於確定目標節點上的適當 port

下面的“文字影像”顯示了型別之間的關係

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

nodes 圖元素也許是最有趣的,因為它的元素不僅包含執行時元件及其 componentType 例項和 name 值,還可以選擇包含元件暴露的指標。節點元素包含各種屬性,這些屬性通常是不言自明的。例如,基於表示式的元件包含 expression 屬性,該屬性包含元件的主要表示式字串。要啟用指標,請在 @Configuration 類中新增 @EnableIntegrationManagement 或在您的 XML 配置中新增 <int:management/> 元素。有關完整資訊,請參閱 指標與管理

nodeId 表示一個唯一的增量識別符號,用於區分不同的元件。它也用於 links 元素中,表示此元件與其他元件(如果存在)的關係(連線)。inputoutput 屬性分別對應於 AbstractEndpointMessageHandlerSourcePollingChannelAdapterMessageProducerSupportinputChanneloutputChannel 屬性。有關更多資訊,請參閱下一節。

從 5.1 版本開始,IntegrationGraphServer 接受一個 Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback,用於為特定的 NamedComponentIntegrationNode 上填充附加屬性。例如,您可以將 SmartLifecycleautoStartuprunning 屬性暴露到目標圖中:

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 選項),並且在使用 PollingConsumerPollableChannel 消費時可以產生錯誤。另一個例子是 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 可以使用它來確定如何繪製元件。