Bootstrapping Spring Cloud Config Server MongoDB
Bootstrapping a configuration server with Spring Cloud Config Server MongoDB is just a matter of three steps. Learn what they are and look at a usage example!
Join the DZone community and get the full member experience.
Join For FreeSpring Cloud Config Server MongoDB enables the seamless integration of the regular Spring Cloud Config Server with MongoDB to manage external properties for applications across all environments.
A Simple Config Server
Bootstrapping a configuration server with Spring Cloud Config Server MongoDB is just a matter of the following three steps.
Configure your
pom.xml
:x1<dependencies>
2<dependency>
3<groupId>org.springframework.cloud</groupId>
4<artifactId>spring-cloud-config-server-mongodb</artifactId>
5<version>0.0.2.BUILD-SNAPSHOT</version>
6</dependency>
7</dependencies>
8
9<repositories>
10<repository>
11<id>spring-snapshots</id>
12<name>Spring Snapshots</name>
13<url>https://repo.spring.io/libs-snapshot-local</url>
14<snapshots>
15<enabled>true</enabled>
16</snapshots>
17</repository>
18<repository>
19<id>ojo-snapshots</id>
20<name>OJO Snapshots</name>
21<url>https://oss.jfrog.org/artifactory/libs-snapshot</url>
22<snapshots>
23<enabled>true</enabled>
24</snapshots>
25</repository>
26</repositories>
Create a standard Spring Boot application with the
EnableMongoConfigServer
annotation:9123public class Application {
4
5public static void main(String[] args) {
6SpringApplication.run(Application.class, args);
7}
8
9}
Configure the application's
spring.data.mongodb.*
properties inapplication.yml
:41spring
2data
3mongodb
4uri mongodb //localhost/config-db
Usage
We can now add configuration documents to MongoDB and access it via the REST API.
Add configuration documents to MongoDB:
121use config-db;
2
3db.gateway.insert({
4"label": "master",
5"profile": "prod",
6"source": {
7"user": {
8"max-connections": 1,
9"timeout-ms": 3600
10}
11}
12});
Access it by invoking
http://localhost:8080/master/gateway-prod.properties
:21user.max-connections: 1.0
2user.timeout-ms: 3600.0
Spring Cloud Config Client-backed components can be configured to automatically load configuration from MongoDB by leveraging Spring Cloud Config Server MongoDB. See here for an example!
Like it? Give the project a star on GitHub. You can also comment below. All feedback is welcome!
Opinions expressed by DZone contributors are their own.
Comments