Table of Contents
Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management instructions. A key idea is that the Bus is like a distributed Actuator for a Spring Boot application that is scaled out, but it can also be used as a communication channel between apps. Starters are provided for an AMQP broker as the transport or for Kafka, but the same basic feature set (and some more depending on the transport) is on the roadmap for other transports.
![]() | Note |
---|---|
Spring Cloud is released under the non-restrictive Apache 2.0 license. If you would like to contribute to this section of the documentation or if you find an error, please find the source code and issue trackers in the project at github. |
Spring Cloud Bus works by adding Spring Boot autconfiguration if it detects itself on the classpath. All you need to do to enable the bus is to add spring-cloud-starter-bus-amqp
or spring-cloud-starter-bus-kafka
to your dependency management and Spring Cloud takes care of the rest. Make sure the broker (RabbitMQ or Kafka) is available and configured: running on localhost you shouldn’t have to do anything, but if you are running remotely use Spring Cloud Connectors, or Spring Boot conventions to define the broker credentials, e.g. for Rabbit
application.yml.
spring: rabbitmq: host: mybroker.com port: 5672 username: user password: secret
The bus currently supports sending messages to all nodes listening or all nodes for a particular service (as defined by Eureka). More selector criteria may be added in the future (ie. only service X nodes in data center Y, etc…). There are also some http endpoints under the /bus/*
actuator namespace. There are currently two implemented. The first, /bus/env
, sends key/value pairs to update each node’s Spring Environment. The second, /bus/refresh
, will reload each application’s configuration, just as if they had all been pinged on their /refresh
endpoint.
![]() | Note |
---|---|
The Bus starters cover Rabbit and Kafka, because those are the two most common implementations, but Spring Cloud Stream is quite flexible and binder will work combined with |
The HTTP endpoints accept a "destination" parameter, e.g. "/bus/refresh?destination=customers:9000", where the destination is an ApplicationContext
ID. If the ID is owned by an instance on the Bus then it will process the message and all other instances will ignore it. Spring Boot sets the ID for you in the ContextIdApplicationContextInitializer
to a combination of the spring.application.name
, active profiles and server.port
by default.
The "destination" parameter is used in a Spring PathMatcher
(with the path separator as a colon :
) to determine if an instance will process the message. Using the example from above, "/bus/refresh?destination=customers:**" will target all instances of the "customers" service regardless of the profiles and ports set as the ApplicationContext
ID.
The bus tries to eliminate processing an event twice, once from the original ApplicationEvent
and once from the queue. To do this, it checks the sending application context id againts the current application context id. If multiple instances of a service have the same application context id, events will not be processed. Running on a local machine, each service will be on a different port and that will be part of the application context id. Cloud Foundry supplies an index to differentiate. To ensure that the application context id is the unique, set spring.application.index
to something unique for each instance of a service. For example, in lattice, set spring.application.index=${INSTANCE_INDEX}
in application.properties (or bootstrap.properties if using configserver).
Spring Cloud Bus uses
Spring Cloud Stream to
broadcast the messages so to get messages to flow you only need to
include the binder implementation of your choice in the
classpath. There are convenient starters specifically for the bus with
AMQP (RabbitMQ) and Kafka
(spring-cloud-starter-bus-[amqp,kafka]
). Generally speaking
Spring Cloud Stream relies on Spring Boot autoconfiguration
conventions for configuring middleware, so for instance the AMQP
broker address can be changed with spring.rabbitmq.*
configuration properties. Spring Cloud Bus has a handful of native
configuration properties in spring.cloud.bus.*
(e.g. spring.cloud.bus.destination
is the name of the topic to use
the the externall middleware). Normally the defaults will suffice.
To lean more about how to customize the message broker settings consult the Spring Cloud Stream documentation.
Bus events (subclasses of RemoteApplicationEvent
) can be traced by
setting spring.cloud.bus.trace.enabled=true
. If you do this then the
Spring Boot TraceRepository
(if it is present) will show each event
sent and all the acks from each service instance. Example (from the
/trace
endpoint):
{ "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": "*:**" } }
This trace shows that a RefreshRemoteApplicationEvent
was sent from
customers:9000
, broadcast to all services, and it was received
(acked) by customers:9000
and stores:8081
.
To handle the ack signals yourself you could add an @EventListener
for the AckRemoteApplicationEvent
and SentApplicationEvent
types
to your app (and enable tracing). Or you could tap into the
TraceRepository
and mine the data from there.
![]() | Note |
---|---|
Any Bus application can trace acks, but sometimes it will be useful to do this in a central service that can do more complex queries on the data. Or forward it to a specialized tracing service. |
The Bus can carry any event of type RemoteApplicationEvent
, but the
default transport is JSON and the deserializer needs to know which
types are going to be used ahead of time. To register a new type it
needs to be in a subpackage of org.springframework.cloud.bus.event
.
To customise the event name you can use @JsonTypeName
on your custom class
or rely on the default strategy which is to use the simple name of the class.
Note that both the producer and the consumer will need access to the class
definition.
If you cannot or don’t want to use a subpackage of org.springframework.cloud.bus.event
for your custom events, you must specify which packages to scan for events of
type RemoteApplicationEvent
using @RemoteApplicationEventScan
. Packages
specified with @RemoteApplicationEventScan
include subpackages.
For example, if you have a custom event called FooEvent
:
package com.acme; public class FooEvent extends RemoteApplicationEvent { ... }
you can register this event with the deserializer in the following way:
package com.acme; @Configuration @RemoteApplicationEventScan public class BusConfiguration { ... }
Without specifying a value, the package of the class where @RemoteApplicationEventScan
is used will be registered. In this example com.acme
will be registered using the
package of BusConfiguration
.
You can also explicitly specify the packages to scan using the value
, basePackages
or
basePackageClasses
properties on @RemoteApplicationEventScan
. For example:
package com.acme; @Configuration //@RemoteApplicationEventScan({"com.acme", "foo.bar"}) //@RemoteApplicationEventScan(basePackages = {"com.acme", "foo.bar", "fizz.buzz"}) @RemoteApplicationEventScan(basePackageClasses = BusConfiguration.class) public class BusConfiguration { ... }
All examples of @RemoteApplicationEventScan
above are equivalent,
in that the com.acme
package will be registered by explicitly specifying the
packages on @RemoteApplicationEventScan
. Note, you can specify multiple base
packages to scan.