Hello everyone,

Greetings today!

Today, we'll look at how to use the spring cloud bus with RabbitMQ to refresh configuration changes so that running microservices can pick up changes without restarting servers.

Prerequisite

Before learning about the spring cloud bus, you should be familiar with spring cloud config server and spring cloud config client. For that refer to Spring Cloud Config Server And Client Example With GIT, We'll be using the same application we created in the previous post here.

So let's get started.

Run the ConfigServer and BookMgt microservices from the previous post to understand the need for a spring cloud bus.

Navigate to localhost:8999/book/view GET Endpoint.

The O/P will be


Now, update the book.mgt.property1 property value in book-management-dev as shown below and commit the changes to GIT.
 
book.mgt.property1=400

Now, hit get endpoint localhost:8999/book/view once more to find the value of book.mgt.property1. You will find value is not updated to 400

To use the new property, we must restart the BookMgt microservice, which is not ideal.

Adding Actuator Config Cloud Application i.e BookMgt microservice.

With the addition of an actuator, we can refresh config changes by hitting Refresh URL, eliminating the need to restart the microservice after each property change.

Open the BookMgt pom.xml file and add actuator dependency.


Now we must add the @RefreshScope annotation to components that use git properties; in this case, we will add the @RefreshScope annotation to BookController.


Refresh endpoints are disabled by default in spring 2.5, so let's enable them by adding the following property to bootstap.properties.


Restart BookMgt and use the GET endpoint localhost:8999/book/view to get an initial property value of property1, which will be 400.

Now again update book.mgt.property1 property value in book-management-dev as follow & push changes to GIT.

book.mgt.property1=500

If you go back to localhost:8999/book/view, you will notice that the value has not been updated and is still 400.

Instead of restarting BookMgt, we would call the refresh endpoint listed below, which would allow BookMgt to retrieve the updated property without restarting the server.

Hit POST endpoint http://localhost:8999/actuator/refresh  without body

So you will get properties that are updated.

Now again hit localhost:8999/book/view and you will find the value of book.mgt.property1 would be updated to 500.

Let's say we have multiple microservices and multiple instances of each microservice, then we need to hit /actuator/refresh for each of them, which is a lot of manual work and a time-consuming process, which is where the spring cloud bus comes in.

With all microservices connected to a message broker such as RabbitMQ, we can hit /actuator/refresh for any microservice and all other microservices will automatically fetch updated properties, which is known as spring cloud bus.

Configure Spring Cloud Bus With RabbitMQ

Note-We need to have RabbitMQ running for configuring the spring cloud bus 

  • ConfigServer - Spring Cloud Bus Configuration
    • Add rabbitmq & config monitor dependency in the ConfigServer application.

    • Add rabbitmq configurations in application.properties

  • BookMgt- Spring Cloud Bus Configuration
    • Add spring-cloud-starter-bus-amqp & spring-boot-starter-actuator dependency

    • Now we need to enable spring cloud bus so add the below property in bootstrap.properties


Now we are done with our coding.

Time to test our code !!
  • Start RabbitMq Server
  • Start ConfigServer
  • Start 2 instances of BookMgt on a different port
  • Hit localhost:8999/book/view & localhost:8998/book/view and check the value of book.mgt.property1, in my case it is 500
  • Now update book.mgt.property1 property value in book-management-dev as follow & push changes to GIT. 
    • book.mgt.property1=800 
  • Now we will hit the below refresh endpoint for any 1 instance of BookMgt & we will find that for both cases of BookMgt value of book.mgt.property1 is updated to 800 since they are connected by the spring cloud bus.
    • POST Endpoint (with nobody) http://localhost:8999/actuator/bus-refresh 

O/P for both servers

Thanks 
Enjoy your learning!

Another post you can refer to is