spring

Spring Cloud Configuration Example

Welcome readers, in this tutorial, we will explore cloud configuration with spring boot application.

1. Introduction

  • Spring Boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jar and are directly started using embedded servlet container (such as Tomcat, Jetty or Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them in the application
  • It helps in removing the boilerplate code, extra annotations, and xml configurations
  • It provides a powerful batch processing and manages the rest endpoints
  • It provides an efficient jpa-starter library to effectively connect the application with the relational databases
  • It offers a Microservice architecture and cloud configuration that manages all the application related configuration properties in a centralized manner.

Now, open the eclipse ide and let’s see how to implement this tutorial in spring boot.

2. Spring Cloud Configuration Example

Here is a systematic guide for implementing this tutorial.

2.1 Tools Used

We are using Eclipse Kepler SR2, JDK 8 and Maven.

2.2 Project Structure

In case you are confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

Spring Cloud Configuration -  Application Structure
Fig. 1: Application Structure

2.3 Project Creation

This section will demonstrate how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.

Spring Cloud Configuration - Maven Project
Fig. 2: Create a Maven Project

In the New Maven Project window, it will ask you to select a project location. By default, ‘Use default workspace location’ will be selected. Just click on the next button to proceed.

Spring Cloud Configuration - Project Details
Fig. 3: Project Details

Select the Maven Web App archetype from the list of options and click next.

Spring Cloud Configuration - Archetype Selection
Fig. 4: Archetype Selection

It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT.

Fig. 5: Archetype Parameters

Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jcg.tutorial</groupId>
	<artifactId>Springcloudconfigurationtutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
</project>

Let’s start building the application!

3. Creating a Spring Boot application

Below are the steps involved in developing the application. But before starting we are assuming that developers have installed the MySQL on their machine.

3.1 Maven Dependencies

Here, we specify the dependencies for the Spring Boot and Cloud. Maven will automatically resolve the other dependencies. The updated file will have the following code.

pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jcg.tutorial</groupId>
	<artifactId>Springcloudconfigurationtutorial</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>

	<name>Springcloudconfigurationtutorial Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-parent</artifactId>
		<version>Angel.SR6</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
	</dependencies>

	<build>
		<finalName>Springcloudconfigurationtutorial</finalName>
	</build>
</project>

3.2 Configuration Files

Following configuration files will be used to implement this tutorial.

3.2.1 Application Properties

Create a new properties file at the location: Springcloudconfigurationtutorial/src/main/resources/ and add the following code to it.

application.properties

## Application configuration. ##
server.port=7070

## Tells spring config framework to locally look for the properties. ##
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/common-configuration

3.2.2 Client Application Configuration

Create a folder at the location: Springcloudconfigurationtutorial/src/main/resources/ and the following code to it.

config-client.properties

welcome.message = Welcome to Spring cloud configuration tutorial.
welcome.username = javacodegeek

3.3 Java Class

Add the following code the main class to bootstrap the application from the main method. Always remember, the entry point of the spring boot application is the class containing @SpringBootApplication annotation and the static main method.

Mycloudserver.java

package com.ducat.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
// The below annotation is required to enable the spring-cloud-configuration server in an application.
// This annotation will help to read the application's configuration details from an external server.
@EnableConfigServer
public class Mycloudserver {

	public static void main(String[] args) {
		SpringApplication.run(Mycloudserver.class, args);
	}
}

4. Run the Application

As we are ready with all the changes, let us compile the spring boot project and run the application as a java project. Right click on the Mycloudserver.java class, Run As -> Java Application.

Fig. 6: Run the Application

Developers can debug the example and see what happens after every step. Enjoy!

5. Project Demo

Now hit the following url on your favorite browser and users will see the client’s application configuration properties.

http://localhost:7070/config-client/default/master
Fig. 7: Configuration properties of Client application

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

6. Conclusion

In this section, developers learned how to create a Cloud configuration application with Spring Boot. Developers can download the sample application as an Eclipse project in the Downloads section.

7. Download the Eclipse Project

This was an example of implementing cloud configuration with spring boot.

Download
You can download the full source code of this example here: Spring Cloud Configuration Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button