整合圖

從 4.3 版開始,Spring Integration 提供對應用程式執行階段物件模型的存取,此模型可選擇性地包含元件指標。它以圖形方式呈現,可用於視覺化整合應用程式的目前狀態。o.s.i.support.management.graph 套件包含所有必要的類別,以收集、建置和呈現 Spring Integration 元件的執行階段狀態,作為單一樹狀結構的 Graph 物件。IntegrationGraphServer 應宣告為 bean,以建置、擷取和重新整理 Graph 物件。產生的 Graph 物件可以序列化為任何格式,儘管 JSON 格式彈性且方便在用戶端解析和表示。僅具有預設元件的 Spring Integration 應用程式將公開如下圖形

{
  "contentDescriptor" : {
    "providerVersion" : "6.3.5",
    "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:從 MessageHandlerMessageProducerSourcePollingChannelAdapterMessageChannel 的方向,透過 outputChannelreplyChannel 屬性

  • error:從 PollingConsumerMessageProducerSourcePollingChannelAdapter 上的 MessageHandlerMessageChannel,透過 errorChannel 屬性;

  • discard:從 DiscardingMessageHandler (例如 MessageFilter) 到 MessageChannel,透過 errorChannel 屬性。

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

此元素中的資訊可由視覺化工具使用,以呈現來自 nodes 圖形元素的節點之間的連線,其中 fromto 數字表示連結節點的 nodeId 屬性中的值。例如,link 元素可用於確定目標節點上的正確 port

以下「文字影像」顯示了類型之間的關係

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

nodes 圖形元素可能最有趣,因為其元素不僅包含執行階段元件及其 componentType 實例和 name 值,還可以選擇性地包含元件公開的指標。節點元素包含各種通常是不言自明的屬性。例如,基於運算式的元件包含 expression 屬性,其中包含元件的主要運算式字串。若要啟用指標,請將 @EnableIntegrationManagement 新增至 @Configuration 類別,或將 <int:management/> 元素新增至您的 XML 設定。請參閱 指標和管理 以取得完整資訊。

nodeId 代表唯一的遞增識別碼,可讓您區分一個元件與另一個元件。它也用於 links 元素中,以表示此元件與其他元件的關係 (連線),如果有的話。inputoutput 屬性適用於 AbstractEndpointMessageHandlerSourcePollingChannelAdapterMessageProducerSupportinputChanneloutputChannel 屬性。請參閱下一節以取得更多資訊。

從 5.1 版開始,IntegrationGraphServer 接受 Function<NamedComponent, Map<String, Object>> additionalPropertiesCallback,用於在特定 NamedComponentIntegrationNode 上填入其他屬性。例如,您可以將 SmartLifecycle autoStartuprunning 屬性公開到目標圖形中

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 也具有 SourcePollingChannelAdapterMessageChannel,以便定期從來源資料傳送訊息。其他元件可能是中介軟體請求-回覆元件 (例如 JmsOutboundGateway),具有取用 AbstractEndpoint 以訂閱 (或輪詢) requestChannel (input) 以取得訊息,以及 replyChannel (output) 以產生要傳送到下游的回覆訊息。同時,任何 MessageProducerSupport 實作 (例如 ApplicationEventListeningMessageProducer) 都會包裝一些來源協定監聽邏輯,並將訊息傳送到 outputChannel

在圖形中,Spring Integration 元件使用 IntegrationNode 類別階層表示,您可以在 o.s.i.support.management.graph 套件中找到它。例如,您可以將 ErrorCapableDiscardingMessageHandlerNode 用於 AggregatingMessageHandler (因為它具有 discardChannel 選項),並且可以在使用 PollingConsumerPollableChannel 取用時產生錯誤。另一個範例是 CompositeMessageHandlerNode — 用於 MessageHandlerChain,當使用 EventDrivenConsumer 訂閱 SubscribableChannel 時。

@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 可以使用它來確定如何繪製元件。