Configure Spring Config Server
Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.
Maven Dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <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>
Start Configuration Server
You first need a Config Service to act as a sort of intermediary between the Spring applications and version-controlled repository of configuration files. We use Spring Cloud’s @EnableConfigServer
to enable a config server that can communicate with other applications.
@SpringBootApplication @EnableConfigServer public class SpringConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringConfigServerApplication.class, args); }
The Config Server needs to know which repository to manage. For the sake of simplicity, we have used native file system to store the config file.
Other sources are any JDBC compatible database, GIT, Subversion, Hashicorp Vault, Credhub.
File System Repository
To use file system to store the config file, we need to set profile = native.
Configure below properties in application.properties
spring.profiles.active=native server.port=8001 config.server.native.search-locations = <path of config files>
Query Configuration
When config server starts, it will exposes some endpoints for config clients to get the configuration. The HTTP service has resources in the following form:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
where application
is injected as the spring.config.name
in the SpringApplication
(what is normally application
in a regular Spring Boot app), profile
is an active profile (or comma-separated list of properties), and label
is an optional git label (defaults to master
.)
Let us put a config file rest-dev.yml in the location provided above for config.server.native.search-locations
where,
rest – spring application name
dev – Spring active profile
Content of rest-dev.yml
rest: greeting: Hello User!!
Test the application
Once the server is running, try hitting url.
http://localhost:8001/rest/dev
In response, you will get the content of rest-dev.yml file stored in the config folder.
We will be configuring Spring Config Client and Spring Cloud Bus next.