将 Azure Cosmos DB 数据库绑定到 Azure Spring Cloud 应用程序Bind an Azure Cosmos DB database to your Azure Spring Cloud application

可以通过 Azure Spring Cloud 将所选 Azure 服务自动绑定到应用程序,而不必手动配置 Spring Boot 应用程序。Instead of manually configuring your Spring Boot applications, you can automatically bind select Azure services to your applications by using Azure Spring Cloud. 本文演示如何将应用程序绑定到 Azure Cosmos DB 数据库。This article demonstrates how to bind your application to an Azure Cosmos DB database.

先决条件:Prerequisites:

绑定 Azure Cosmos DBBind Azure Cosmos DB

Azure Cosmos DB 具有五种支持绑定的不同 API 类型。Azure Cosmos DB has five different API types that support binding. 以下过程演示如何使用它们:The following procedure shows how to use them:

  1. 创建 Azure Cosmos DB 数据库。Create an Azure Cosmos DB database. 如需帮助,请参阅有关如何创建数据库的快速入门。Refer to the quickstart on creating a database for help.

  2. 记录数据库名称。Record the name of your database. 对于此过程,数据库名称是 testdbFor this procedure, the database name is testdb.

  3. 在 Azure Spring Cloud 应用程序的 pom.xml 文件中添加以下依赖项之一。Add one of the following dependencies to your Azure Spring Cloud application's pom.xml file. 选择适合 API 类型的依赖项。Choose the dependency that is appropriate for your API type.

    • API 类型:Core (SQL)API type: Core (SQL)

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

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

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

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

      <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>azure-storage-spring-boot-starter</artifactId>
          <version>2.0.5</version>
      </dependency>
      
  4. 使用 az spring-cloud app update 更新当前部署,或者使用 az spring-cloud app deployment create 创建新的部署。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. 转到 Azure 门户中的 Azure Spring Cloud 服务页面。Go to your Azure Spring Cloud service page in the Azure portal. 转到“应用程序仪表板”,选择要绑定到 Azure Cosmos DB 的应用程序。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:

    • 绑定类型Azure Cosmos DBThe Binding type value Azure Cosmos DB.
    • API 类型。The API type.
    • 数据库名称。Your database name.
    • Azure Cosmos DB 帐户。The Azure Cosmos DB account.

    备注

    如果使用 Cassandra,请使用数据库名称的密钥空间。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. property 字段应类似于以下示例:The property field should be similar to this example:

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

后续步骤Next steps

本问介绍了如何将 Azure Spring Cloud 应用程序绑定到 Azure Cosmos DB 数据库。In this article, you learned how to bind your Azure Spring Cloud application to an Azure Cosmos DB database. 若要详细了解如何将服务绑定到应用程序,请参阅绑定到 Azure Cache for Redis 缓存To learn more about binding services to your application, see Bind to an Azure Cache for Redis cache.