Create a microservices application architecture with Spring Boot and Spring Cloud - Part 2 - Config server

Introduction

The config server will be a centralized location for holding configuration settings for the various services.  The configuration files in this case will be YAML files that the other services will look at for specific properties that they will need.

Overview

For the config server in this project, we are going to be using Spring Cloud Config (https://spring.io/projects/spring-cloud-config) because it’s easy to setup and use for what we need it for.

The above diagram explains the communication flow between the services and the config server.  Each service is going to have properties that are stored on the config server so that when that service boots up, it will contact the config server and grab it’s externalized config properties from there.

The advantages to this is to allow multiple instances of the same service to grab properties that need to be the same for all instances of that service.  If we didn’t have a config server, we would need to individually set properties per service instance which takes up time and effort.

Tutorial

1.)  Create a new Spring Boot app called, config_server, with a package of com.bryanlor.  Ensure that Gradle is used, which should be selected by default anyways.

 

2.)  Open up the build.gradle file.

3.)  Remove the content that’s currently in the build.gradle file and then add the following to the file instead: 

buildscript {
	ext {
		springBootVersion = '2.1.4.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.bryanlor'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	mavenCentral()
	maven { url "https://repo.spring.io/milestone" }
}

ext['springCloudVersion'] = 'Greenwich.RC2'

dependencies {
	compile('org.springframework.cloud:spring-cloud-config-server')
	compile('org.springframework.cloud:spring-cloud-starter-sleuth')
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

4.)  Save the file.

5.)  Right click on the project and go to Gradle → Refresh Gradle Project.  You can also use the gradle commands in the command-line to build the project so that it pulls in the dependencies needed. 


 

6.)  Open up ConfigServerApplication.java.

7.)  Add the following package import:

import org.springframework.cloud.config.server.EnableConfigServer;

8.)  Then below the annotation, @SpringBootApplication, place the following annotation to enable the application as a config server: @EnableConfigServer.


 

9.)  Create an application.properties file in /src/main/resources/ if it doesn’t exist yet.

 

10.)  Open application.properties and make sure it has the following property:

spring.profiles.active=native

11.)  Create a bootstrap.yml file in /src/main/resources/ if it doesn’t exist yet.

12.)  Open bootstrap.yml file and make sure it has the following properties:

server:
  port: 8088

13.)  Start up the config server.

Code Repository

Grab the code at https://github.com/lorbs28/bl-config-server.


Next tutorial part...

Create a microservices application architecture with Spring Boot and Spring Cloud - Part 3 - Discovery service