Microservices centralized configuration by Spring Cloud and state changes'? broadcast via Spring Cloud Bus and RabbitMQ.
Tweet Tweet !! you have some updates!

Microservices centralized configuration by Spring Cloud and state changes' broadcast via Spring Cloud Bus and RabbitMQ.

This article explains:

  1. How to create a centralized configuration for microservices' instances.
  2. How to notify those instances of updates occurred during runtime.

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.

No alt text provided for this image

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:

  1. Start by adding these dependencies to pom.xml:

No alt text provided for this image

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:

No alt text provided for this image

3. Add these configuration properties to application.properties file:

No alt text provided for this image

Run Eureka client using Maven?

To run your project add eureka.instance.instance-id property to application.properties file:

No alt text provided for this image

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:

No alt text provided for this image

spring-cloud-starter-netflix-eureka-server is used to import Eureka server.

2. Add annotation @EnableEurekaServer to the main class:

No alt text provided for this image

3. Add these configuration properties to application.properties file:

No alt text provided for this image

Create Spring cloud Api-Gateway:

1. Start by adding these dependencies to pom.xml:

No alt text provided for this image

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:

No alt text provided for this image

PART2: Create centralized configuration.

Create Config server:

1. Start by adding these dependencies to pom.xml of config server app:

No alt text provided for this image

2. Add these configuration properties to the application.properties file of config server app:

No alt text provided for this image

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:

  • application.properties
  • AppName_<profile>.properties
  • AppName.properties

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:

No alt text provided for this image


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:

No alt text provided for this image

4. Add this property to application.properties of config server client app:

No alt text provided for this image

5. Add the following dependencies config server client app:

No alt text provided for this image

PART3: Publish changes using spring cloud bus.

Publish event using Spring Cloud Bus:

1. Add this dependency to spring cloud config app:

No alt text provided for this image

  • spring-cloud-starter-bus-amqp starter?enables the spring cloud bus with AMQP broker.
  • spring-boot-starter-actuator starter enables spring boot actuator for config server 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

source.

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.

source.

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.

source.

What is RabbitMQ?

It is a message broker used to exchange messages between distributed systems.

No alt text provided for this image

source.

RabbitMQ uses an exchange whose role is to put messages in the appropriate receiver/consumer's queue.

Among its advantages:

  • Loose coupling: The broker, the publisher and the consumer are running on different and independent machines.
  • Performance: Even if the receiver is busy or down, the broker can receive messages and keep them waiting for it to be available again. Messages will be kept intact until the consumer confirms reception.
  • Scalability: RabbitMQ in itself can be extended on separated machines.
  • Language agnostic: Publisher and the consumer can be built with different technologies.

2. Add these properties to application.properties file:

No alt text provided for this image

For the project to run, need to have RabbitMQ up and running on your machine.

Steps to download and install RabbitMQ on Windows 10:

  1. Download Erlang: link
  2. Once the executable file is downloaded install it.
  3. Download rabbitmq: link

No alt text provided for this image

  1. Once downloaded, install the executable file.
  2. Open RabbitMQ command prompt and run:?rabbitmq-plugins enable rabbitmq_management
  3. If this command is successful, the following message is displayed:

No alt text provided for this image

7. Go to your browser and type this link: https://localhost:15672/?

You must get this screen:

No alt text provided for this image

8. To login enter the default credentials:

  • username: guest
  • password: guest?

Test the project:

Clone the project from github directory (you will find the link above), and follow those steps:

  1. Add to application.properties in the centralized configuration directory the following properties and make sure to remove them from each config server client's application.properties file.

P.S: Config server client's in our case are CustomerDetailsApi and ProductDetailsApi.

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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:

  • ConfigServerApplication.
  • DiscoveryServiceApplication.
  • ApiGatewayApplication.
  • ProductDetailsApplication and/or CustomerDetailsApplication.

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:

No alt text provided for this image

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:

No alt text provided for this image

5.Call the following endpoints of CustomerDetailsApi app (same for ProductDetailsApi app):

No alt text provided for this image

In postman call these endpoints:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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:

  • Bring the latest configurations from centralized configuration directory.
  • Notify RabbitMQ about refresh event.
  • All subscribed microservices will be informed of updates via RabbitMQ.

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 !

No alt text provided for this image


No alt text provided for this image

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.

要查看或添加评论,请登录

Henda Farhani的更多文章

  • Docker for beginners

    Docker for beginners

    Let's start by defining Docker It is a runtime environment designed to create, build and manage applications in…

    4 条评论
  • Micro-services architecture

    Micro-services architecture

    What is micro-services ? According to Wikipedia, Micro-services is a software development technique that structures an…

    6 条评论
  • Redux

    Redux

    Redux is an open source JavaScript library for managing application state. It was created in June 2015 and it was…

    2 条评论
  • Change detection Angular 2

    Change detection Angular 2

    You may have read that in Angular2 you must apply the single responsibility principle per component. For this reason…

    1 条评论

社区洞察

其他会员也浏览了