配置

自定义消息代理

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.