Developing a SpringBoot app using Azure Cosmos DB for MongoDB API

Azure Cosmos DB is a fully managed globally distributed, multi-model database service, transparently replicating your data across any number of Azure regions. You can elastically scale throughput and storage, and take advantage of fast, single-digit-millisecond data access using the API of your choice backed by 99.999 SLA. This sample shows you how to use the Azure Cosmos DB for MongoDB API to store and access data from a SpringBoot application.

Prerequisites

Before you can run this sample, you must have the following prerequisites:

  • An active Azure account. If you don't have one, you can sign up for a free account. Alternatively, you can use the Azure Cosmos DB Emulator for this tutorial.
  • An Azure Cosmos DB for MongoDB API account configured with at least two read regions.

Running this sample

  1. Clone this repository

  2. Substitute the spring.data.mongodb.uri in src\main\resources\application.properties with your Azure Cosmos DB account's connection string.

    spring.data.mongodb.uri=mongodb://<account-name>:<account-key>@<account-name>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@<account-name>@
    

    If the key string used in the MongoDB URI contains special characters, such as plus signs, use the URL-encoded value of that key. Otherwise, you may face issues trying to connect and see errors pertaining to SASL Authentication failure. This is especially true when working with Azure Cosmos DB Emulator over a local network.

  3. Create a new database in your Azure Cosmos DB account.

  4. Substitute the spring.data.mongodb.database in src\main\resources\application.properties with the name of the database in your Azure Cosmos DB account.

    spring.data.mongodb.database=<database-name>
    
  5. Run your application:

    mvn spring-boot:run
    

About the code

The code included in this sample is intended to illustrate using Azure Cosmos DB for MongoDB API from SpringBoot application. It is a port of the sample from SpringBoot Getting Started article. It also uses code from this tutorial to configure read preference for different repositories.

More information