Spring Cloud - Configuration Server with Git Integration
Spring Cloud is a Spring module which provides the features to make the development of distributed cloud-based applications simple and quick. Spring cloud configuration server is also one of the features which help us in the configuration management of distributed applications.
In this article, we will discuss how spring cloud configuration can be used to externalize and manage the configurations of different distributed applications for different environments from a single place.
We will be using a git repository as a configuration store which is considered an ideal choice.
1. Setting up Configuration Server - We will create a simple spring boot application which with the help of some dependencies will work as our configuration server serving configuration to our client application using a git versioned configuration store.
1.1 Create a spring boot application with below-required dependencies and import it to your IDE.
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
package com.cloud.cloudconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class CloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigApplication.class, args);
}
}
application.properties -
server.port=8888
spring.cloud.config.server.git.uri=${HOME}/HELLO-SPRING-CLOUD/config-repo
spring.cloud.config.server.git.clone-on-start=true
spring.security.user.name=root
spring.security.user.password=rootp
After creating the directory run the below commands to git initialize the directory and add a new file to it config-client-development.properties
~/HELLO-SPRING-CLOUD/config-repo(master)$ git init
~/HELLO-SPRING-CLOUD/config-repo(master)$ vim config-client-development.properties
After writing the above commands vim command-line editor will open and you can add the properties given below using it and save it. (Or you can simply go to the directory after git init, create a new file config-client-development.properties and add the content using some text editor also and save it.)
app.env=development
Once the file is saved commit it
~/HELLO-SPRING-CLOUD/config-repo(master)$ git add .
~/HELLO-SPRING-CLOUD/config-repo(master)$ git commit -m "initialized config properties"
Note: The name of the property follows a naming pattern - the name of the client application followed by a dash and then the active profile. So above created file will be used by a client application with name config-client and active profile development.
/{application}-{profile}.properties
/{application}/{profile}[/{label}]
/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties
/{application}-{profile}.yml
we will get the following output
user.role: Developer
<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>
After generating the application import it to the IDE and create a resource file bootstrap.properties which will be loaded at the startup of the application.
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=rootp
The above configuration file has the connection details to our configuration server application along with the client application name and active profile.
Now let's write a simple controller to server a GET request which will use the configuration value from the configuration server.
package com.cloudclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class CloudClientApplication {
@Value("${app.env}")
private String envName;
public static void main(String[] args) {
SpringApplication.run(CloudClientApplication.class, args);
}
@GetMapping(
value = "/which-env",
produces = MediaType.TEXT_PLAIN_VALUE)
public String whoami() {
return String.format("Hello! You are on %s enviromnment.", envName);
}
}
Though It works very well with spring applications like we have used with @Value annotation but it works well on any other environment also.Now we are done with our client application setup also, let's run our client application also and check if the configuration is working properly.
After running the client application (simply run the main class CloudClientApplication using your IDE) lets hit its REST URL -
http://localhost:8080/which-env
And if the response is as given below our client is fetching the configuration form configuration server properly.
Hello! You are on development enviromnment.
Comments
Post a Comment