Javatpoint Logo
Javatpoint Logo

Implementing Spring Cloud Bus

In this section, when we make the changes in the Git repository, we have to hit multiple instances of the limits-service to refresh the configuration.

We will invoke one URL, and it will reflect all the hundred instances of the microservices. Here we will use Spring Cloud Bus. There are many options available in the Spring Cloud Bus: Apache Kafka, RabbitMQ, etc. In this section, we will use RabbitMQ.

Note: Before moving to the next step, make sure that the RabbitMQ server is running in the background.

Let’s implement the Spring Cloud Bus in limits-service and spring-cloud-config-server.

Step 1: Open the pom.xml file of limits-service and spring-cloud-config-server project. Add the amqp dependency and save the file to reflect the changes.

Step 2: Open bootstrap.properties file and disable the management security by adding the following statement:

dependency and save the file to reflect the changes.

Step 3: First, run the SpringCloudConfigServerApplication.java file and then run the two instances of limits-service: LimitsServiceApplication and LimitsServiceApplication8081.

Step 5: Invoke the URL http://localhost:8080/limits. It returns the following response:

dependency and save the file to reflect the changes.

Now, invoke the URL http://localhost:8081/limits. It also returns the same response.

Step 6: Open the limits-service-qa.properties file and change the minimum value from 22 to 29.

Now we have to commit the changes.

Step 7: Open the Postman and send a POST request with the URL http://localhost:8080/bus/refresh.

Note: If you are using Spring Boot 2.0.0 or above versions, use the following URL:
http://localhost:8080/actuatror/bus-refresh

Step 8: Open the browser and invoke both instances (http://localhost:8080/limits and http://localhost:8081/limits) of limits-service. Both the URLs return the updated value that we have configured in the limits-service-qa.properties file.

We have seen that the minimum value changes to 29. Here, you can notice that we have not committed the changes in the Git repository manually, but the changes reflect in both instances of limits-service.

Instead of committing the Git repository, we have invoked a URL http://localhost:8080/bus/refresh. All this happening due to Spring Cloud Bus.

In the specific instance, we have run the Spring Cloud Bus over RabbitMQ. When we start the application, it also starts with that. After starting the Spring Cloud Bus, all the microservices registers with the Bus.

When we make changes in configuration, and the changes are called on any of the instances, the microservice sends an event over the Spring Cloud Bus. The Spring Cloud Bus propagates that event to all the microservice instances that are registered with it.

In this section, we have solved the problem of calling multiple instances of microservices.







Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA