6. Config-Server Spring Cloud Microservice

What is Spring Cloud Config Server?
Spring Cloud Config Server is a tool that externalizes configuration for microservices, centralizing it in a single location. This server supports various backends for storing configuration, including Git, SVN, or even a local file system. It retrieves configuration from these sources and provides it to microservices dynamically. The Config-server store the Configurations(Application.properties) files of all microservices.
Config-Server Architecture


Key Benefits
  • Centralized Configuration: Stores all configuration files in one repository for different environments like development and production.
  • Dynamic Updates: Microservices can fetch and apply updated configurations without restarting.
  • Environment Management: Serves environment-specific configurations, such as separate files for development and production.
Sample Configuration Flow
  • Centralized Storage: Configuration files are stored in GitHub with environment-specific profiles.
  • Microservices Fetch Configurations: Microservices request their configurations from the Config Server based on their environment.
  • Configuration Refresh: Microservices can automatically reload updated configurations without restarting.

Development Steps
  • Configure Config-Server
    • Create a Config Server: Set up a Spring Boot project with  and add the necessary dependency.
    • Import Config Server: Import config-server as module in IDE.
    • Enable Config Server: Enable Config-Server by using this annotation @EnableConfigServer in main class.
    • Registering with Eureka: Use Eureka to enable load balancing and fault tolerance for the Config Server and microservices.
    • Create a GitHub Repo: Create a GitHub Repository to store configurations of all Microservices.
  • Defactor Microservice to use Config-Server
    • Refactor Microservices(Employee-Service & Department-Service) to use Config-Server: Microservices will get their own configuration from GitHub using Config-Server.
      • Add Two Dependencies in Employee-Service POM.
      • Create a Properties file in the GitHub repo for Employee-Service.
      • Add Config-Server URL in the Application.properties file of Employee-Service.
      • Move all information from the Application.properties file to GitHub
      • Run and Verify from the Postman client

Configure Config-Server
1. Create a Config Server: Create a config-server with below Dependency from Spring Initializr.
  • Config Server
  • Eureka Discovery Client
  • Spring Boot Actuator
Dependencies added

2. Import Config Server: This is the same way to import in IDE like Service-Registry, API-Gateway. Just follow below steps from screenshot.
Import

3. Enable Config-Server with @EnableConfigServer: Add @EnableConfigServer annotation in class level of main class of Config-Server.
@EnableConfigServer
Enable Config Server



4. R
egistering with Eureka: Follow the same steps to register as a Eureka Client, as done for the Service-Registry and API-Gateway.
Follow the 3rd steps from here(API-Gateway Registered as Eureka Client)

After registered, Project won't run before setup GitHub Repository.

5. Make link Config-Server with GitHub Repo: Here have some steps to store Configurations of each Microservice in GitHub. Follow the below steps to store all microservice's configuration to GitHub.
  • Create Github Repository

    • Create Git Repo
      GitHub Repository Create
  • Make a link between GitHub Repo and Config-Server by mentioning the GitHub URL, Clone as true, and Branch name.
  • Run Config-Server project and verify it's instance in Eureka (Browser UI)       
Link with GitHub and Run project
Link Config-Server with GitHub Repo and Run project on 8888 port

Verify Config-Server in Eureka UI
Verify Config-Server's instance on Eureka UI

Important Note: If the GitHub repository is blank and we mention the main branch in the Application.properties file then Config-Server might not run. So mentioned here master branch (spring.cloud.config.server.git.default-label=master). And once create any file in the main branch then mention the main branch (spring.cloud.config.server.git.default-label=main) and run the project. We have changed after creating an employee-service.properties file in the main branch.



Defactor Microservice to use Config-Server
(Defactor Employee-Service to use Config)
Steps
  1. Add Two Dependencies in Employee-Service POM.
  2. Create a Properties file in the GitHub repo for Employee-Service.
  3. Add Config-Server URL in Application.properties file of Employee-Service.
  4. Move all information from Application.properties file to GitHub
  5. Run and Verify from the Postman client
  • Add Two Dependencies
    • Config Client
    • Spring Boot Actuator

    • Add dependencies in Employee-Service

  • Create Properties file in GitHub repo for Employee-Service: Properties file name must be same as Employee-Service name (employee-service.properties). This is not case-sensitive. So we can use lowercase.

    • Create employee-service.properties file in GitHub Reop
      Create Properties file For Employee-Service

  • Add Config-Server URL in the Application.properties file of Employee-Service.
Add Config-Server URL

  • Move all information from the Application.properties file to the GitHub employee-service.properties file.
    • Move all configurations from Employee-Service's Application.properties file to GitHub's employee-service.properties file except Application-Name and Config_Server URL.

    • Move all configuration
      • Just keep the Application-Name and Config-Server URL

    • Commit the GitHub file(employee-service.properties) file
  • Run and Verify from the Postman client
    • Run the Config-Server, and Employee-Service and check it's instances is up in Eureka UI
    • Verify by calling API from the Postman Client

    • Finally check from Postman
      Employee Service calling bia API-Gateway service


Defactor for Department-Service: Do the same for remain/new microservices.

Post a Comment

0 Comments