Bind an Azure Cosmos DB database to your application in Azure Spring Cloud
This article applies to: ✔️ Java
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:
- A deployed Azure Spring Cloud instance. Follow our quickstart on deploying via the Azure CLI to get started.
- An Azure Cosmos DB account with a minimum permission level of Contributor.
Prepare your Java project
Add one of the following dependencies to your application's pom.xml pom.xml file. Choose the dependency that is appropriate for your API type.
API type: Core (SQL)
<dependency> <groupId>com.azure.spring</groupId> <artifactId>azure-spring-boot-starter-cosmos</artifactId> <version>3.6.0</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: Azure Table
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>azure-storage-spring-boot-starter</artifactId> <version>2.0.5</version> </dependency>
Update the current app by running
az spring-cloud app deploy, or create a new deployment for this change by runningaz spring-cloud app deployment create.
Bind your app to the Azure Cosmos DB
Azure Cosmos DB has five different API types that support binding. The following procedure shows how to use them:
Create an Azure Cosmos DB database. Refer to the quickstart on creating a database for help.
Record the name of your database. For this procedure, the database name is testdb.
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.
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.
Restart the application by selecting Restart on the application page.
To ensure the service is bound correctly, select the binding name and verify its details. The
propertyfield 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 application in Azure Spring Cloud to an Azure Cosmos DB database. To learn more about binding services to your application, see Bind to an Azure Cache for Redis cache.