配置
自定义消息代理
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:9000
和 stores:8081
接收(ack)。
要自己处理 ack 信号,可以在应用程序中为 AckRemoteApplicationEvent
和 SentApplicationEvent
类型添加一个 @EventListener
(并启用跟踪)。或者,你可以利用 TraceRepository
并从中挖掘数据。
任何 Bus 应用程序都可以跟踪 ack。然而,有时在中央服务中这样做会很有用,中央服务可以对数据执行更复杂的查询或将其转发到专门的跟踪服务。 |