Microservices centralized configuration by Spring Cloud and state changes' broadcast via Spring Cloud Bus and RabbitMQ.
This article explains:
If you are new to microservices architecture, this article may be helpful for better understanding.
The full code is available within this link.
The project is built with Spring and Spring Boot frameworks. And is implemented in java8.
Ok, so lets start !
The following schema introduces the different components and their interaction.
API-Gateway is the entry point for clients' requests, it recuperates instances' addresses from Eureka service registry.
Each microservice is connected to its own database. At startup, they receive configuration properties via the config server.
During runtime, if there are changes in the centralized configuration, Spring Cloud Bus informs the running instances via a message broker called RabbitMQ.
Want to dive deeper in technical details ?!
PART 1: Create microservices project:
Create Eureka client:
Spring cloud Netflix provides Netflix open source software (OSS), integrations for Spring Boot apps.
Among common patterns that are existing in Netflix OSS and provided by Spring Cloud Netflix, there is Eureka.
To import it, the dependency spring-cloud-starter-netflix-eureka-client starter is added.
OK.. So what is Eureka exactly?
When Netflix revealed its own microservices' open source software, it introduced Eureka as a pattern that englobes two components: Server and Client.
The server is a standalone application that manages the registry of running instances.
The client component on its part, is responsible on registering itself to Eureka server and maintain this connection.
Hence, every microservice, except the discovery service, needs to activate its client functionality via @EnableDiscoveryClient annotation.
2. Add the annotation @EnableDiscoveryClient to main class:
3. Add these configuration properties to application.properties file:
Run Eureka client using Maven?
To run your project add eureka.instance.instance-id property to application.properties file:
P.S: Comments in the picture explain why you need to add this property.
Then, run the following command at the application's root directory:
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.application.instance_id={instance_id}
Create Eureka discovery server:
1. Start by adding these dependencies to pom.xml:
spring-cloud-starter-netflix-eureka-server is used to import Eureka server.
2. Add annotation @EnableEurekaServer to the main class:
3. Add these configuration properties to application.properties file:
Create Spring cloud Api-Gateway:
1. Start by adding these dependencies to pom.xml:
As mentioned before any application that needs to be discovered by other microservices must import spring-cloud-starter-netflix-eureka-client.
2. Add these configuration properties to the application.properties file:
PART2: Create centralized configuration.
Create Config server:
1. Start by adding these dependencies to pom.xml of config server app:
2. Add these configuration properties to the application.properties file of config server app:
Create a directory in your local machine with properties files containing needed configurations. In the previous picture the directory will be existing under path ${user.home}/Training/configFiles.
In centralized configuration directory you can create 3 files named:
AppName is the value you attribute to application.name property in application.properties file.
If your app is running in a certain profile such as development or production, then the file AppName_<profile>.properties has the highest priority. Means that your app will consider configurations loaded from this file over application.properties and AppName.properties.
Otherwise AppName.properties file has the higher priority.
And in case none of AppName_<profile>.properties and AppName.properties are existing then all configurations will be loaded from application.properties file.
In centralized configuration directory create following files:
3. For microservice to be able to consume the common config at startup create, at resources directory at the same level of application.properties file, a file called bootstrap.properties.
Add the following properties to it:
4. Add this property to application.properties of config server client app:
5. Add the following dependencies config server client app:
PART3: Publish changes using spring cloud bus.
Publish event using Spring Cloud Bus:
1. Add this dependency to spring cloud config app:
What is Spring Boot Actuator? It is a sub-project of Spring Boot Framework, it helps to monitor and manage a Spring boot application.
领英推荐
What is Spring Cloud Bus?
Spring cloud bus is a project of spring cloud and it is used in our case to connect all microservices to a single message broker.
A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out
When changes are brought to the centralized configuration, microservices' instances need to be notified by those changes without having to be restarted.
Spring Cloud Bus makes this possible by connecting all microservices' instances to a message broker, via which it sends the modifications occurred during runtime.
Spring Cloud Bus provides starters for either an AMQP broker or Kafka as the transport.
In our case starter of AMQP broker is used. And RabbitMQ is chosen as the AMQP broker.
What is a message broker ?
A message broker is a software that enables applications, systems, and services to communicate with each other and exchange information. The message broker does this by translating messages between formal messaging protocols. This allows interdependent services to “talk” with one another directly, even if they were written in different languages or implemented on different platforms.
Message brokers can validate, store, route and deliver messages to the appropriate destination.
What is AMQP?
Stands for Advanced message queuing protocol. It is a binary protocol, where everything sent over it is binary, which avoids sending useless data.
It is a messaging protocol that enables conforming client applications to communicate with conforming messaging middleware brokers.
What is RabbitMQ?
It is a message broker used to exchange messages between distributed systems.
RabbitMQ uses an exchange whose role is to put messages in the appropriate receiver/consumer's queue.
Among its advantages:
2. Add these properties to application.properties file:
For the project to run, need to have RabbitMQ up and running on your machine.
Steps to download and install RabbitMQ on Windows 10:
7. Go to your browser and type this link: https://localhost:15672/?
You must get this screen:
8. To login enter the default credentials:
Test the project:
Clone the project from github directory (you will find the link above), and follow those steps:
P.S: Config server client's in our case are CustomerDetailsApi and ProductDetailsApi.
P.S: shared.code property is used to test the state changes' broadcast via Spring Cloud Bus and RabbitMQ. (keep reading to know how)
2.Add the following properties to customers.properties file created in the centralized configuration directory and make sure to remove them from application.properties of CustomerDetailsApi:
P.S: specific.code property is used to test the state changes' broadcast via Spring Cloud Bus and RabbitMQ. (keep reading to know how)
3. Add the following properties to products.properties file created in the centralized configuration directory and make sure to remove them from application.properties of ProductDetailsApi:
P.S: specific.code property is used to test the state changes' broadcast via Spring Cloud Bus and RabbitMQ. (keep reading to know how)
4. Run your projects in this order:
If all projects are running, check Eureka discovery service dashboard: https://localhost:8010/.
Each of ApiGatewyApplication, ProductDetailsApplication and CustomerDetailsApplication must be registered to Eureka registery:
And to make sure that ConfigServerApplication, ProductDetailsApplication and CustomerDetailsApplication are subscribed to RabbitMQ, check this link https://localhost:15672/#/queues, you should get 3 queues created via Spring Cloud Bus:
5.Call the following endpoints of CustomerDetailsApi app (same for ProductDetailsApi app):
In postman call these endpoints:
As you may notice the http request is addressing API-Gateway app since, as mentioned before, it is the entry point through which all coming requests must enter first.
If everything works fine you should receive the same response: KJSGDK68787.
Same thing for https://localhost:8082/customers/customers/shared/code:
6. Try now changing shared.code property in application.properties file and specific.code property in customers.properties file that are already created in centralized configuration directory.
Then call this endpoint:
As you may notice this http request is addressing Config server app directly. It calls the endpoint /busrefresh provided by Spring Boot Actuator.
Calling this endpoint will:
7. Call the previous endpoints: https://localhost:8082/customers/customers/shared/code and https://localhost:8082/customers/customers/specific/code.
TADA! Microservices are aware of the changes !
Conclusion:
In this article we were introduced to centralized configuration in distributed systems' architecture like microservices.
Also we were introduced to Spring Cloud framework and to its sub-projects like Spring Cloud Bus that takes charge broadcasting config changes without needing boilerplate code. Instead, all you have to do is importing dependencies, adding some annotations and of course installing the right message broker on your machine.
This article also contains a small exercise for you to give you the chance to manipulate code. So its up to you to move some config properties from application.properties files existing within microservices' projects, to config files located at the centralized config directory on your local machine. Don't forget to indicate the path as it is mentioned already in the article.