Bind an Azure Cosmos DB database to your Azure Spring Cloud application

Instead of manually configuring your Spring Boot applications, you can automatically bind select Azure services to your applications by using Azure Spring Cloud. This article demonstrates how to bind your application to an Azure Cosmos DB database.

Prerequisites:

Bind Azure Cosmos DB

Azure Cosmos DB has five different API types that support binding. The following procedure shows how to use them:

  1. Create an Azure Cosmos DB database. Refer to the quickstart on creating a database for help.

  2. Record the name of your database. For this procedure, the database name is testdb.

  3. Add one of the following dependencies to your Azure Spring Cloud application's pom.xml file. Choose the dependency that is appropriate for your API type.

    • API type: Core (SQL)

      <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
          <version>2.1.6</version>
      </dependency>
      
    • API type: MongoDB

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-mongodb</artifactId>
      </dependency>
      
    • API type: Cassandra

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-cassandra</artifactId>
      </dependency>
      
    • API type: Gremlin (graph)

      <dependency>
          <groupId>com.microsoft.spring.data.gremlin</groupId>
          <artifactId>spring-data-gremlin</artifactId>
          <version>2.1.7</version>
      </dependency>
      
    • API type: Azure Table

      <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>azure-storage-spring-boot-starter</artifactId>
          <version>2.0.5</version>
      </dependency>
      
  4. Use az spring-cloud app update to update the current deployment, or use az spring-cloud app deployment create to create a new deployment. These commands will either update or create the application with the new dependency.

  5. Go to your Azure Spring Cloud service page in the Azure portal. Go to Application Dashboard and select the application to bind to Azure Cosmos DB. This application is the same one you updated or deployed in the previous step.

  6. Select Service binding, and select Create service binding. To fill out the form, select:

    • The Binding type value Azure Cosmos DB.
    • The API type.
    • Your database name.
    • The Azure Cosmos DB account.

    Note

    If you are using Cassandra, use a key space for the database name.

  7. Restart the application by selecting Restart on the application page.

  8. To ensure the service is bound correctly, select the binding name and verify its details. The property field should be similar to this example:

    azure.cosmosdb.uri=https://<some account>.documents.azure.com:443
    azure.cosmosdb.key=abc******
    azure.cosmosdb.database=testdb
    

Next steps

In this article, you learned how to bind your Azure Spring Cloud application to an Azure Cosmos DB database. To learn more about binding services to your application, see Bind to an Azure Cache for Redis cache.