配置

自定義訊息代理

Spring Cloud Bus 使用 Spring Cloud Stream 廣播訊息。因此,要使訊息流動,您只需在類路徑中包含所選的繫結器實現即可。Bus 提供了方便的 AMQP (RabbitMQ) 和 Kafka (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 事件

透過設定 spring.cloud.bus.trace.enabled=true 可以追蹤 Bus 事件(RemoteApplicationEvent 的子類)。如果這樣做,Spring Boot TraceRepository(如果存在)將顯示傳送的每個事件以及每個服務例項的所有確認。以下示例來自 /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 接收(確認)。

要自行處理確認訊號,您可以向您的應用程式新增一個用於 AckRemoteApplicationEventSentApplicationEvent 型別的 @EventListener(並啟用追蹤)。或者,您可以利用 TraceRepository 並從中挖掘資料。

任何 Bus 應用程式都可以追蹤確認。然而,有時,在中央服務中執行此操作很有用,該服務可以對資料執行更復雜的查詢或將其轉發到專門的追蹤服務。
© . This site is unofficial and not affiliated with VMware.