Externalize Application Configuration With Spring Cloud Config

Introduction

One of the principles from the 12 Factor App, states that we have to separate our application configuration from the code. The configuration varies based on the environment and it's better to organize them based on the environment your application is running. 

So in this article, we will be looking at how we can externalize configurations with Spring Cloud Config.

Exploring Spring Cloud Configuration Server  in Microservices

In this article, we will learn how to use the Spring Cloud Configuration server to centralize managing the configuration of our microservices. An increasing number of microservices typically come with an increasing number of configuration files that need to be managed and updated. 

With the Spring Cloud Configuration server, we can place the configuration files for all our microservices in a central configuration repository that will make it much easier to handle them. Our microservices will be updated to retrieve their configuration from the configuration server at startup. 

Spring Config Integration With a PCF Application: A Step-by-Step Guide

Why Spring Configuration? 

Spring Cloud Config provides support for externalized configuration in a distributed system. It allows you to dynamically push updates to configuration properties to the application without needing to restart or redeploy.

Step-by-Step Guide to Integrate With a PCF Application

Step 1: Add a Maven Dependency

XML
 




xxxxxxxxxx
1
23


 
1
<dependency>
2

          
3
               <groupId>io.pivotal.spring.cloud</groupId>
4

          
5
<artifactId>spring-cloud-services-starter-config-client</artifactId>
6

          
7
             </dependency>
8

          
9
              <dependency>
10

          
11
                    <groupId>org.springframework.cloud</groupId>
12

          
13
                    <artifactId>spring-cloud-starter-config</artifactId>
14

          
15
             </dependency>
16

          
17
             <dependency>
18

          
19
                    <groupId>org.springframework.boot</groupId>
20

          
21
                    <artifactId>spring-boot-starter-actuator</artifactId>
22

          
23
             </dependency>


pom.xml 

XML
 






Step 2: Add refreshscope and EnableAutoConfiguration to the Model Class

Java
 




x


1
package gsahoo.demo.model;
2

          
3
import org.springframework.beans.factory.annotation.Value;
4
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5
import org.springframework.cloud.context.config.annotation.RefreshScope;
6
import org.springframework.context.annotation.Configuration;
7
import org.springframework.stereotype.Component;
8

          
9
@Configuration
10

          
11
@EnableAutoConfiguration
12

          
13
@RefreshScope
14

          
15
@Component
16

          
17
public class Person {
18

          
19
    @Value("${person.company}")
20

          
21
    String company = null;
22

          
23
    public void setCompany(String company) {
24

          
25
        this.company = company;
26

          
27
    }
28

          
29
    public String getCompany() {
30

          
31
        return company;
32

          
33
    }
34

          
35
}


If you are autowiring this model object in any other class then make sure to add refreshscope to that class as well.

Configuration As A Service: Spring Cloud Config – Using Kotlin

Developing a microservice architecture with Java and Spring Boot is quite popular these days. In microservice architecture we hundreds of services and managing services for each service and for each profile which is a quite tedious task. In this article, we will demonstrate the Spring cloud config server using Kotlin. 

Spring Boot provided a much-needed spark to the Spring projects.

Kotlin Microservices With Micronaut, Spring Cloud, and JPA

The Micronaut Framework provides support for Kotlin built upon the Kapt compiler plugin. It also implements the most popular cloud-native patterns, like distributed configuration, service discovery, and client-side load balancing. These features allow you to include an application that's been built on top of Micronaut into an existing microservices-based system. The most popular example of such an approach may be integration with the Spring Cloud ecosystem. If you have already used Spring Cloud, it is very likely you built your microservices-based architecture using the Eureka discovery server and Spring Cloud Config as a configuration server. Beginning with version 1.1, Micronaut supports both these popular tools as part of the Spring Cloud project. That's good news because, in version 1.0, the only supported distributed solution was Consul, and there was no way to use Eureka discovery together with Consul's property source (running them together ends with an exception).

In this article, you will learn how to: