In this tutorial, you will learn how to use Spring Cloud Bus to notify running Microservices about the changes in the Spring Cloud Config property file. Running Microservices will be updated with new properties stored in Spring Cloud Config file without the need for you to restart them. This way you can update values in a property file stored in a centralized configuration file and then have all of your hundreds of Microservices be updated at once and use the new value.
For a step by step series of video lessons, please check this page: Spring Boot Microservices and Spring Cloud.
Configure Spring Cloud Config
To be able to use Spring Cloud Bus, you will need to have Spring Cloud Config configured first. Please read the following tutorial to learn how to create your own Spring Cloud Config Server.
POM.XML Dependencies
For Spring Cloud Bus to work, we need to add the following dependency to the POM.xml file of all of our Microservices that need to receive updated Spring Cloud Config properties.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
also please make sure that POM.xml file of each of your Microservices has the Actuator dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.properties Configuration
Update application.properties file of each Microservice that needs to refresh their properties with the following:
management.endpoints.web.exposure.include=bus-refresh management.endpoints.web.exposure.include=bus-env spring.rabbitmq.host = localhost spring.rabbitmq.port = 5672 spring.rabbitmq.username = guest spring.rabbitmq.password = guest
In the application.properties configuration we enable the two Spring Cloud Bus Web Service endpoints and we also set the connection credentials for the Rabbit MQ which is being used to broadcast events. To learn how to install Rabbit MQ on your computer and how to change the default username and password from “guest” to a more secure one, please follow this tutorial – Rabbit MQ, Download, Install and Update Password.
@RefreshScope and @ConfigurationProperties Annotations
For a Bean to be able to receive updated configuration properties:
- It must be annotated with @RefreshScope annotation if it reads application properties using the @Value annotation,
- Or should use @ConfigurationProperties annotation to read configuration properties. For an example on how to use @ConfigurationProperties annotation please read this tutorial: Spring Boot @ConfigurationProperties tutorial.
- Or it should read application properties using the Environment object.
Once you update your Microservices with the above mentioned details, restart them.
Refreshing Properties with Spring Cloud Bus
If you have done the above steps, you should be ready to try how Spring Cloud Bus works. Here is how you can try it:
- Startup all of your Microservices,
- Update one of the properties in the centralized configuration file with a new value. The centralized configuration file is the one that is being managed by Spring Cloud Config.
- Send HTTP Post request to the following URL: http://localhost:8012/actuator/busrefresh where:
- 8012 – is the port number if your Spring Cloud Config server,
- /actuator – is a URL path to a Spring Cloud Actuator
- /bus-refresh – is a URL path to a Spring Cloud Bus Web service endpoint which we have enabled in application.properties above.
Note that to make Spring Cloud Bus trigger the refresh properties process, you need to send an HTTP POST request to /actuator/busrefresh URL path of your Spring Cloud Config Server.
If all is good, then once the HTTP POST request is sent to the above-mentioned URL, all of your Microservices should load a newer version of the centralized configuration file.
I hope this tutorial was helpful to you.
If you are interested to learn more about Spring Cloud and you enjoy learning by watching a series of step-by-step video lessons, then have a look at the below list of video courses. One of them might be exactly what you are looking for.