配置

自定義訊息代理

Spring Cloud Bus 使用 Spring Cloud Stream 來廣播訊息。因此,要使訊息流轉,只需在類路徑中包含所選的繫結器實現即可。Bus 提供了方便的 AMQP (RabbitMQ) 和 Kafka (spring-cloud-starter-bus-[amqp|kafka]) 啟動器。一般來說,Spring Cloud Stream 依賴 Spring Boot 的自動配置約定來配置中介軟體。例如,可以使用 spring.rabbitmq.* 配置屬性更改 AMQP 代理地址。Spring Cloud Bus 在 spring.cloud.bus.* 中有一些原生配置屬性(例如,spring.cloud.bus.destination 是用作外部中介軟體的主題名稱)。通常情況下,預設值就足夠了。

要了解更多關於如何自定義訊息代理設定的資訊,請查閱 Spring Cloud Stream 文件。

跟蹤 Bus 事件

透過設定 spring.cloud.bus.trace.enabled=true 可以跟蹤 Bus 事件(RemoteApplicationEvent 的子類)。如果這樣做,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": "*:**"
  }
}

上述跟蹤顯示,一個 RefreshRemoteApplicationEvent 事件從 customers:9000 傳送,廣播到所有服務,並由 customers:9000stores:8081 接收(ack)。

要自己處理 ack 訊號,可以在應用程式中為 AckRemoteApplicationEventSentApplicationEvent 型別新增一個 @EventListener(並啟用跟蹤)。或者,你可以利用 TraceRepository 並從中挖掘資料。

任何 Bus 應用程式都可以跟蹤 ack。然而,有時在中央服務中這樣做會很有用,中央服務可以對資料執行更復雜的查詢或將其轉發到專門的跟蹤服務。