In this tutorial, you will learn how to implement configuration server in Spring step by step.
In an architecture consisting of many microservices, then it is necessary to externalize configuration parameters. This includes both application and infrastructure parameters.
Spring Cloud provide a way of achieving. With Spring Cloud’s config server, you can maintain configuration parameters externally. Spring Config Server stores the configuration parameter in a version-controller repository such as Git or SVN which can be local or remote.
We would do the following:
- Configuration Server Architecture
- Build the Product Server
- Build the Product Web
- Build the Config Server
- Build the Angular UI App
1. Configuration Server Architecture
The config server design we would use for this demo is shown below. In this case we are using the config server only for the Product Server microservice.
In the figure, you can see that the Product Server uses the Spring Cloud Config Server for storage of configuration parameters.
To see how the config server works, we would build a complete microservices architecture made up of four services
2. Build the ProductServer
This is simply a microservice that stores a list of products and returns them. This service would use Spring Cloud Config server to store config parameters,
You need to add the following dependencies to the ProductServer
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.2.1.RELEASE</version> </dependency>
The config dependency allows the ProductServer would be a client to the Config Server. The actuator dependency will take care of refreshing the configuration parameter in case their values changes in the Config Server.
Also create a yaml file in the resources folder and this is the content
server: port: 8081 spring: data: mongodb: uri: mongodb://localhost:27017/productdb application: name: product-server cloud: config: server: uri: http://localhost:8888
Notice that in the yaml file, the logical name of the application is product-server. We must also create a file file product-server.yml in the config server.
3. Build the ProductWeb
This a application that consumes the Product Server. This microservice simply uses RestTemplate to fetch a list of products from the Product Server microservice.
4. Build the Config Server
This is the actual config server. Microservices that use this config server would have to get configuration parameters from this server. Each microservice connecting to this server would have retrieve config parameter values from the file <application-name>.yml located in the config server’s resource folder.
You need to add the Config Server dependency as well as the Spring Web.
Add the @EnableConfigServer annotation to the main application class. Also create the yaml file with the content shown below:
server: port: 8888 spring: application: name: config-servr cloud: config: server: git: uri: file://D:/Microservices_Architectural_Patterns/config-server/configserver/.git/
Create the product-server.yml file and place some parameters. This config file would contain the values of configuration parameters that would be used for the product-server microservice. It means that the when the product server connects with the config server, it would be reading values from product-server.yml file in the config server.
In this example, we only place the page size parameter with a value of 2.
5. Build and Test the ProductUI Angular App
This is a UI application built with Angular to display a list of products in the browser. See the procedure to build this here.
After you complete this, go ahead and fire up all the services. View the product.hml page and you will see that only two records is displayed. This means that the Product Server actually read the page size parameter from the config server.