Skip to main content

Spring Cloud config server setup with Git

In this article, we will see how to use the Spring Cloud Configuration server to place the configuration for microservices in a central configuration repository.

The Spring Cloud Configuration server supports a number of storage mechanisms to hold the configuration files: Git repositories, local filesystems, HashiCorp Vaults, JDBC databases, etc.

1) Create a GitHub repository

We will use a GitHub repository as a storage mechanism to hold our configuration files.

Let's create a public repository in GitHub.

Now add two configuration files, "order-service-development.yml" and "order-service-production.yml" to this repository and push the code.

order-service-development.yml

order-service-production.yml

2) Setup a config server

In this example, we will setup a Git-backed Spring Cloud Configuration server protected with username and password-based authentication.

2.1) Dependencies

A Spring Cloud Configuration server requires "spring-cloud-config-server", "spring-boot-starter-web" and "spring-boot-starter-security" dependencies added to the classpath

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2.2) Enable Config Server

The "@EnableConfigServer" annotation pulls in all the required setup to auto-configure the config server.

2.3) Properties

We need to configure a git repository URL that contains our version-controlled configuration content.


2.4) Run

Let's run the application, and the configuration should be available at:

% curl -u admin:admin123 http://localhost:8081/order-service/development/ | json_pp

{
   "label" : null,
   "name" : "order-service",
   "profiles" : [
      "development"
   ],
   "propertySources" : [
      {
         "name" : "https://github.com/nkchauhan003/test-config/order-service-development.yml",
         "source" : {
            "app.region.id" : 5000,
            "management.endpoints.health.show-details" : "always",
            "management.endpoints.web.exposure.include" : "*",
            "springdoc.swagger-ui.path" : "/swagger-ui.html"
         }
      }
   ],
   "state" : null,
   "version" : "7214339a264c67a6048cc6740714c0dce4bdb545"
}

3) Setup Client

Let's create a simple Spring Boot client application consisting of a REST controller with one GET method.

3.1) Dependencies

A simple spring-boot application with config-server's client capabilities should have "spring-cloud-starter-config", "spring-boot-starter-web" and "spring-boot-starter-security" dependencies added to the classpath.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

To use Swagger UI, we need to add an additional Maven dependency:

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>${springdoc-openapi-ui.version}</version>
        </dependency>

3.2) Properties

We need to configure "spring.config.import", "spring.application.name", "spring.profiles.active", "spring.cloud.config.uri", "spring.cloud.config.username" and "spring.cloud.config.password".

3.3) Test

Now that everything is set, let's run the application and test our functionality with the help of: Swagger UI

Request

Response

This '"regionId": 5000' in the response is coming from "app.region.id" property defined in "order-service-development.yml" configuration file.

Source code: GitHub


Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

Reactive programming in Java with Project Reactor

Reactive programming is a declarative programming paradigm that focuses on building applications that are responsive, resilient, and scalable in the face of modern challenges like concurrency, distributed systems, and asynchronous data streams . Reactive programming provides a set of tools, patterns, and abstractions to handle asynchronous and event-driven programming more effectively. Imperative programming focuses on describing the step-by-step instructions or commands that the computer needs to follow to achieve a specific task. In this paradigm, you explicitly state how to perform each operation and control flow in your code. The emphasis is on "how" the computation should be done. int sum = 0; for (int i = 1; i Declarative programming emphasizes specifying what you want to achieve rather than detailing how to achieve it. You describe the desired outcome or the properties of the result, and the programming language or framework handles the execution detai...