Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Bootstrapping Spring Cloud Config Server MongoDB

DZone's Guide to

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!

· Database Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

Spring 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.

  1. Configure your pom.xml:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server-mongodb</artifactId>
            <version>0.0.2.BUILD-SNAPSHOT</version>
        </dependency>
    </dependencies>
    
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>ojo-snapshots</id>
            <name>OJO Snapshots</name>
            <url>https://oss.jfrog.org/artifactory/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
  2. Create a standard Spring Boot application with the EnableMongoConfigServer annotation:

    @SpringBootApplication
    @EnableMongoConfigServer
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
  3. Configure the application's spring.data.mongodb.* properties in application.yml:

    spring:
      data:
        mongodb:
          uri: mongodb://localhost/config-db

Usage

We can now add configuration documents to MongoDB and access it via the REST API.

  1. Add configuration documents to MongoDB:

    use config-db;
    
    db.gateway.insert({
      "label": "master",
      "profile": "prod",
      "source": {
        "user": {
          "max-connections": 1,
          "timeout-ms": 3600
        }
      }
    });
  2. Access it by invoking  http://localhost:8080/master/gateway-prod.properties:

    user.max-connections: 1.0
    user.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!

Do you pay to use your database? What if your database paid you? Learn more with RavenDB.

Topics:
mongodb ,database ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}