Create Spring Boot Application With Centralized Configuration
We will see how to create a spring boot application with centralized configuration.
Pre-requisites for below tutorial:
- JDK 1.8 or later
- Maven
- Git
This requires two applications:
- a configuration service application (config-server)
- a configuration client application (config-client)
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>info.code2learn</groupId>
<artifactId>config-server</artifactId>
<name>config-server</name>
<description>Configuration Server</description>
<properties>
<java.version>8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ConfigServerApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
The Config Server needs to know which repository to manage. There are several choices here, but start with a Git-based file system repository. You could as easily point the Config Server to a Github or GitLab repository.
On the file system, create a new directory and run git init
in it. Then add below files in the directory.
- config-client.properties
- config-client-staging.properties
- config-client-production.properties
git commit
in it.
spring.application.name
property identifies it as config-client
to the Config Server. This is how the Config Server knows
which set of configuration to send to a specific client. It also sends all the values from any file named application.properties
or application.yml
in the Git repository. Property keys in more specifically
named files (such as config-client.properties
) override those in application.properties
or application.yml
.
Add a simple property and value with different message related to
specific environment (eg.welcome.message = Welcome to config client
) to the newly created config-client-<label>.properties
files and then commit the changes using git commit
.
server.port=8080
config-client-staging.properties
welcome.message=Welcome To Config Client Application - staging
config-client-production.properties
welcome.message=Welcome To Config Client Application - production
Specify the path to the Git repository by specifying the spring.cloud.config.server.git.uri property in configuration-service/src/main/resources/application.properties. You must also specify a different server.port value to avoid port conflicts when you run both this server and another Spring Boot application on the same machine. The following listing (from configuration-service/src/main/resources/application.properties) shows such an application.properties file:
server.port=8888
spring.cloud.config.server.git.uri=${HOME}/Desktop/config-files
org.springframework.cloud:spring-cloud-starter-config
dependency, to connect to the Config Server.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>info.code2learn</groupId>
<artifactId>config-client</artifactId>
<name>config-client</name>
<description>Configuration Client</description>
<properties>
<java.version>8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.application.name
as config-client
and the location of the Config Server spring.config.import
in config-client/src/main/resources/application.properties
.
config-client/src/main/resources/application.properties
spring.application.name=config-client
spring.config.import=optional:configserver:http://localhost:8888/
management.endpoints.web.exposure.include=*
The client can access any value in the Config Server by using the traditional mechanisms (such as @ConfigurationProperties or @Value("${…}") or through the Environment abstraction).
Create a rest controller that returns the resolved message property’s value.
ConfigClientApplication.java
package info.code2learn.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@RefreshScope
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
ConfigClientController.java
package info.code2learn.configclient.controller;
import org.springframework.beans.factory.annotatioTest the Applicationn.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${welcome.message}")
private String message;
@RequestMapping("/message")
public ResponseEntity<String> message(String name) {
return new ResponseEntity<String>(message, HttpStatus.OK);
}
}
Test the application
Start the Config Server first and then, once it is running, start the
client using profile 'staging' or
'production' (eg.
mvn spring-boot:run -Dspring-boot.run.profiles=staging). Open the client app in the browser using url http://localhost:8080/message
. There, you should see Welcome message in the response specific to
the profile.
You can also see the configuration details for a particular client
application using http://localhost:8888/config-client/staging
Click here for github link
Withdrawals through credit/debit playing cards and bank transfers could take longer, often three to 5 days. The 원 엑스 벳 payout pace is determined by} the gambling site and fee method. However, there is a good rule of thumb that crypto payouts typically get processed within a day. Operating since 2016, Cafe’s point of interest on progressive jackpots with stellar prizes has proved to be a working tactic. Whether in search of fastened jackpot games or progressive jackpot titles – you can see a suitable choice right here.
ReplyDelete