What are Microservices and setting up Config Server

What are Microservices and setting up Config Server

Microservice Architecture is a Service-oriented Architecture. In Java Spring Boot Term, Spring Boot Application acts as Microservice and it has its own endpoint URL and port number. It is totally independent service running in a Microservices environment or Microservice Cloud environment.

In Simple, Microservices is allowing to break the large system into multiple small components running independently. This is called a Microservices Cloud environment.

What is a cloud environment?
Let say if you have three services which are Account-Service, Product-Service, and Reporting Service. This three service is independent Service. These services don’t know how to collaborate with each other and how to communicate with each other. even if you have three Spring boot applications, it is not microservice enabled because these services are not cloud-enabled.
How to make them cloud enable?
Spring Boot provides a set of features using those features we can enable our Spring boot Application as a Microservices Cloud-enabled Application so these features called cloud components. Through this cloud component, the Spring Boot application knows that how to collaborate and communicate with other Spring Boot Applications with minimal configuration and lesser boilerplate code. Once we enable our Spring Boot Application with cloud component that Spring Boot Application call it as Microservice.

Microservices Communication

Spring Cloud Component

  1. Config Server
  2. Service Registry (Eureka Naming Server)
  3. API Gateway
  4. Spring Cloud Loadbalancer
  5. Circuit Breaker
  6. Spring Cloud Sleuth and Zipkin
  7. Feign Client

Config Server
In the Microservices environment, we will keep all the Microservice configuration files in a Centralized place. The configuration file is either Properties file or yml files. In the Monolithic Application, we will keep the configuration file in the resources folder. if any changes happened in the configuration file we need to redeploy or restart the application but in the case of the Microservice environment, we don’t have to redeploy or restart the Microservice. If any changes happened in Config files, the Config Server takes the changes without restarting or redeploy the application. It is one of the benefits of Config Server Also we can easily maintain the configuration file because all are maintained in one centralized place.
The architecture of Config Server
Config Server Architecture

Eureka Naming Server
Eureka Naming Server is Spring Boot Application. It is a Rest based Service. Eureka Naming Server keeps track of all Services running in the Microservice Cloud environment. If we create any Service, that service information registered in Eureka Naming Server. It holds the Service information like the Service name, port, and IP Address of the Service. Whenever a Service requests other services, the Eureka naming Server finds the service from the available registered Service and provides the instance of the particular Service. Let say if we need a Product Service, Eureka Naming Server finds the Product Service and it provides the available instance of Product Service.
So, we can use Eureka Naming Server for two purposes one for registering service into Naming Server and another one is, whenever any service want to talk to other services, Eureka Naming Server find the instance of Service and provide the available instance.
Setting up Config Server
As part of Config Server Setup, we need to set up GIT Repository to maintain all our Microservice Config files. In this example, I will show how to set up a local GIT Repository. Download the following GIT client software and install it in your local System.
https://git-scm.com/downloads
This is a self-explanatory installation so you can do it by yourself. Once you installed it, create a folder anywhere on your local computer. let say we are creating a folder called “config-repo” Open Windows PowerShell or Windows command prompt and go to the folder you have created. I have created the folder in the following location:
D:\test
Inside the folder type “git” and enter, I will return a list of help and usage info, if you get this, you installed git successfully

 Now in this command prompt type the following command. It will create a GIT Repository
git init
it will give the following output, that meaning, it created GIT  Local repository in your test folder so, the test folder act as GIT Local Repository
Initialized empty Git repository in D:/test/.git/
Now I want to add the following files to my GIT Local Repository 
“account-service.yml”
“account-service-dev.yml”
“account-service-qa.yml”
“service-registry.yml”
Copy the above files in your test folder and add them to your GIT Local Repository by using the below command (Dot refer to current folder files)
git add .
Once you perform the above command you actually add these files to your git local repository then you need to commit in your local Repository following the git command.
git commit -m “Adding config files”
Once you performed the above command, you will be getting the following output
[master (root-commit) 71f4d98] Adding config files
4 files changed, 28 insertions(+)
create mode 100644 account-service-dev.yml
create mode 100644 account-service-qa.yml
create mode 100644 account-service.yml
create mode 100644 service-registry.yml

Up to this level, we have created a git Local Repository added the above config files. The next Setup is to configure this Git Repository in our Config Server.  Config Server is a Simple Rest based Spring Boot Application. We will create.
Go to the following website to create Config Server
https://start.spring.io/ Following value I have selected in the strat.spring.io form template. Please choose the same and I am using java 8. In the dependency, you need to select only Config Server

Generate the project and export it into your favorite IDE. I am using IntelliJ.
On my computer, I have created a new folder called “D:\test-cloud-app” where I want to keep all my Microservice projects. I saving generated config server project in the following location.
D:\test-cloud-app

After importing the config-service project following information available in the pom.xml file.

Now we are ready to configure the Config Serice. Open your Application.java(I renamed it to Application.java) and add @EnableConfigServer Annotation like below

@EnableConfigServer
@SpringBootApplication
public class Application {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }

}

Then create an application.yml file in the resource folder and add the following configuration

server:
  port: 8888
spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: file:///d:\test

The meaning of the above configuration is, My Config Server port is 8888, My Application name is “config-service” and my GIT Repository location is file:///d:\test. Here we need to use file:///. This is very very important. Now we have configured the config Server. Let’s test it is working as expected or not. Start the server and try to access the config files available in the “test” Local Repository. Before trying whatever file is available in the test folder it must be committed using the command which I showed in part of the git repository setup.
According to the following Server console, the Config Server is successfully started.

How to test?
We have added the following 5 config files in the GIT repository, so our config Server must able to access those files. if you want to access the account-service.yml file we need to use the following URL. We need to use exactly the yml file name and its environment or profile name. Here i have not mentioned any profile name so we need to use “account-service/default”. When you say default, it will fetch the “account-serviec.yml” file. 
http://localhost:8888/account-service/default
You will be getting the following output. This shows whatever config data is available those data fetched showed here. This meaning our Config Server is working Perfectly.

If you use the profile or environment name instead of the default, the config server fetches its respective yml file data. Let say, if replace the default with dev, Config Server loads the account-service-dev.yml file.
http://localhost:8888/account-service/dev

When you notice this output, it shows dev.yml file info and default yml file. When you load any specific profile file, it will show the first profile file then it shows the default config file. This meaning, the config server gives first priority to which file you want to access, if the file is not available it loads the default config file.
As of now, we have completed the setting up of the Config Server. Next Post we will be going to explore how to setup a Naming server and how to access Naming Server config files from the config Server then how to register the config /server in Eureka Naming Server. Until then bye bye from Suresh. Happy Learning.

=================================================================
Healthy Women makes healthy India. Buy Organic Sanitary napkin made up of using pure cotton and bamboo fiber.
Pure cotton XL Size

Pure Cotton XXL Size

Pure Bamboo XL Size

Pure Bamboo XXL Size

Close Menu
×
×

Cart