DEV Community

Ava Parker
Ava Parker

Posted on

Unlock 100s of Microservices in 5 Easy Steps with Spring Cloud Config & Kotlin!

Building a microservice architecture with Java and Spring Boot has gained immense popularity in recent years. However, managing hundreds of services for each profile can be a daunting task. In this article, we will delve into the world of Spring Cloud Config Server using Kotlin, a powerful tool that simplifies this process.

Spring Boot has revolutionized Spring projects, offering a much-needed boost in terms of efficiency and productivity.

Spring Cloud Config provides a unified and flexible approach to external configuration in a distributed environment. With the Config Server, you have a single point to manage external properties for applications across all environments, making it an essential tool for any microservice setup.

spring cloud config

As illustrated in the diagram above, managing configuration as a central service in distributed systems can be a complex task. Spring Cloud Config provides a client-server architecture mechanism to simplify this process, making it an ideal solution for managing hundreds of services.

Let’s visit https://start.spring.io/, a popular platform for building Spring-based applications.

spring initializer

Whenever we make changes to any service, we need to restart the services to apply the changes, which can be time-consuming and inefficient.

Let’s create a Git repository to manage our configuration, a crucial step in building a microservice architecture. To achieve this, we’ll create a Git repository, which will serve as a central hub for our configuration.

So, we’ll create a small Spring Boot microservice called “springbootclient” to read the username from the Spring Cloud Config central configuration server system, which is our Git repository. This approach allows us to manage our configuration in a centralized and efficient manner.

We have created three distinct properties files for each of our different environments:

  1. springbootclient.properties
  2. springbootclient-dev.properties
  3. springbootclient-prod.properties

For a more detailed example, you can visit carsnewstoday, which provides a comprehensive guide to building a microservice architecture using Spring Cloud Config and Kotlin.

https://github.com/maheshwarLigade/cloud-common-config-server

Here, you can find our Spring Cloud Config properties. You can clone or use this repository directly, which will give you a head start in building your microservice architecture.

Now that we have created a Spring Config Server application using Spring Starter, let’s download and import the project into your preferred IDE or editor. The Git repository is used to store our configuration, and the Spring Cloud Config Server application serves these properties to the client, making it an essential tool for managing hundreds of services.

In essence, Git operates as a central data storage, whereas Spring Cloud Config Server assumes the role of a server-side application, providing configuration support to multiple microservices.

Having established our Git data storage, we have developed a sample client application, dubbed springbootclient, within this repository. In our forthcoming microservice article, we will utilize the same Spring Cloud Config as a configuration server.

Let us delve into the code base for the client application.

This is a sample application.properties file:

server.port=8888
logging.level.org.springframework.cloud.config=DEBUG
spring.cloud.config.server.git.uri=https://github.com/maheshwarLigade/cloud-common-config-server.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.searchPaths=springbootclient

Example Code for SpringCloudConfigServerexApplication.kt

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.config.server.EnableConfigServer

@SpringBootApplication@EnableConfigServerclass SpringCloudConfigServerexApplication

fun main(args: Array<String>) {
   runApplication<SpringCloudConfigServerexApplication>(*args)
}

Now, start and initialize the spring cloud-config server and verify the following URL:

http://localhost:8888/springbootclient/dev/master

Configuring a Spring Boot Client Application

Let’s develop a compact microservice that fetches configuration from the spring cloud config server and exposes the property value via a REST endpoint.

Visit https://start.spring.io/ and create a spring boot client microservice using Kotlin.

Sample POM.xml dependencies.

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.module</groupId>
   <artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Now check the SpringCloudClientAppApplication.kt code

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplicationclass SpringCloudClientAppApplication

fun main(args: Array<String>) {
    runApplication<SpringCloudClientAppApplication>(*args)
}

Now create one sample REST controller which is serving REST request. We want to check ” /whoami” this endpoint is returning which is the user based on active profile dev, prod, etc.

UserController.kt

import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController


@RestControllerclass UserController {

    @Value("\${app.adminusername}")    var username="Test"
//get request serving
    @GetMapping("/whoami")    fun whoami() = "I am a  "+ username

}

Create a bootstrap.properties file to configure the Spring Cloud Config server settings, specifying the Git branch and active profile, such as development, local, production, and more.

spring.application.name=springbootclient
spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.fail-fast=true
spring.cloud.config.label=master

Each property is intuitively named, clearly indicating its purpose and function.

When accessing this URL http://localhost:9080/whoami

Response: I am a DevUser

Github repository link:

Config Server repository: https://github.com/maheshwarLigade/cloud-common-config-server

Code repository: https://github.com/maheshwarLigade/spring-cloud-config-kotlin-ex

Top comments (0)