Hey buddy! Do you have an idea about maintaining properties and configuration easily by spending the least amount of time? Then, definitely, you have to set up a Config Server which is another microservice with a version control system. Let's quickly dive into the implementation of it.
Config server
Step 6 - Config Server
Let's take a look at the project initialization with https://start.spring.io/
Spring Initialzr - Config Server
- Project build tool: Maven
- Language: Java
- Spring boot: 2.4.6
- Project Metadata
- Group: com.agnasarp
- Artifact: agnasarp-config-server
- Name: agnasarp-config-server
- Description: Agnasarp Config Server
- Package name: com.agnasarp.configserver
- Packaging: Jar
- Java version: 8
- Dependencies:
- Eureka Discovery Client: A REST based service for locating services for the purpose of load balancing and failover of middle-tier servers.
- Config Server: Central management for configuration via Git, SVN, or HashiCorp Vault.
Project structure
Project structure of config server in IntelliJ Idea
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.agnasarp</groupId> <artifactId>agnasarp-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>agnasarp-config-server</name> <description>Agnasarp Config Server</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server: port: 8580 spring: application: name: CONFIG-SERVER cloud: config: server: git: uri: https://github.com/Agnasarp/agnasarp-microservice-configurations clone-on-start: true profiles: active: native
AgnasarpConfigServerApplication.java
package com.agnasarp.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class AgnasarpConfigServerApplication { public static void main(String[] args) { SpringApplication.run(AgnasarpConfigServerApplication.class, args); } }
Now we can create a repository in a version controlling system to manage configurations that may be used in all microservices we have created.
Remote repository for configurations
In that application.yml file, we can place the configurations that we use to connect Eureka Server as below. These can be removed from all other microservices because now we have the configuration centrally.
eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eureka/ instance: hostname: localhost
Now we have to set up one configuration in each microservice to call Service Registry. We create bootstrap.yml and paste the below lines of code. This is going to be used to call Config Server just we set up.
spring: cloud: config: enabled: true uri: http://localhost:8580
Suppose there is a configuration change so we can update it in the GitHub repository and call the refresh method in relevant microservices as below.
curl --location --request POST 'http://localhost:8280/actuator/refresh'
Post a Comment