組態

自訂訊息代理

Spring Cloud Bus 使用 Spring Cloud Stream 來廣播訊息。因此,若要讓訊息流動,您只需要在類別路徑中包含您選擇的綁定器實作即可。針對具有 AMQP (RabbitMQ) 和 Kafka 的 Bus,有方便的啟動器 (spring-cloud-starter-bus-[amqp|kafka])。一般來說,Spring Cloud Stream 依賴 Spring Boot 自動組態慣例來組態中介軟體。例如,AMQP 代理位址可以使用 spring.rabbitmq.* 組態屬性來變更。Spring Cloud Bus 在 spring.cloud.bus.* 中有一些原生組態屬性 (例如,spring.cloud.bus.destination 是要用作外部中介軟體的主題名稱)。通常,預設值就已足夠。

若要深入瞭解如何自訂訊息代理設定,請參閱 Spring Cloud Stream 文件。

追蹤 Bus 事件

Bus 事件 (RemoteApplicationEvent 的子類別) 可以透過設定 spring.cloud.bus.trace.enabled=true 來追蹤。如果您這樣做,Spring Boot TraceRepository (如果存在) 會顯示每個已傳送的事件以及來自每個服務實例的所有 ACK。以下範例來自 /trace 端點

{
  "timestamp": "2015-11-26T10:24:44.411+0000",
  "info": {
    "signal": "spring.cloud.bus.ack",
    "type": "RefreshRemoteApplicationEvent",
    "id": "c4d374b7-58ea-4928-a312-31984def293b",
    "origin": "stores:8081",
    "destination": "*:**"
  }
  },
  {
  "timestamp": "2015-11-26T10:24:41.864+0000",
  "info": {
    "signal": "spring.cloud.bus.sent",
    "type": "RefreshRemoteApplicationEvent",
    "id": "c4d374b7-58ea-4928-a312-31984def293b",
    "origin": "customers:9000",
    "destination": "*:**"
  }
  },
  {
  "timestamp": "2015-11-26T10:24:41.862+0000",
  "info": {
    "signal": "spring.cloud.bus.ack",
    "type": "RefreshRemoteApplicationEvent",
    "id": "c4d374b7-58ea-4928-a312-31984def293b",
    "origin": "customers:9000",
    "destination": "*:**"
  }
}

先前的追蹤顯示,RefreshRemoteApplicationEventcustomers:9000 傳送,廣播至所有服務,並由 customers:9000stores:8081 接收 (ACK)。

若要自行處理 ACK 訊號,您可以為應用程式中的 AckRemoteApplicationEventSentApplicationEvent 類型新增 @EventListener (並啟用追蹤)。或者,您可以利用 TraceRepository 並從那裡挖掘資料。

任何 Bus 應用程式都可以追蹤 ACK。但是,有時在中央服務中執行此操作會很有用,該服務可以對資料執行更複雜的查詢,或將其轉發到專門的追蹤服務。