你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将 Azure Cosmos DB 数据库连接到 Azure Spring Apps 中的应用程序

注意

从 2025 年 3 月中旬开始,基本标准企业计划将被弃用,停用期为 3 年。 建议过渡到 Azure 容器应用。 有关详细信息,请参阅 Azure Spring Apps 停用公告

从 2024 年 9 月 30 日开始,标准消耗和专用计划将被弃用,六个月后将完全关闭。 建议过渡到 Azure 容器应用。 有关详细信息,请参阅将 Azure Spring Apps 标准消耗和专用计划迁移到 Azure 容器应用

本文适用于:✔️ Java ✔️ C#

本文适用于:✔️ 基本版/标准版 ✔️ 企业版

可以通过 Azure Spring Apps 将所选 Azure 服务自动连接到应用程序,而不必手动配置 Spring Boot 应用程序。 本文演示如何将应用程序连接到 Azure Cosmos DB 数据库。

先决条件

准备项目

  1. 在应用程序的 pom.xml 文件中添加以下依赖项之一。 选择适合 API 类型的依赖项。

    • API 类型:NoSQL

      <dependency>
          <groupId>com.azure.spring</groupId>
          <artifactId>spring-cloud-azure-starter-data-cosmos</artifactId>
      </dependency>
      
    • API 类型:MongoDB

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

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-cassandra</artifactId>
      </dependency>
      
  2. 通过运行 az spring app deploy 更新当前应用,或者通过运行 az spring app deployment create 针对此更改创建新的部署。

将应用连接到 Azure Cosmos DB

注意

默认情况下,服务连接器在应用程序级别创建。 若要替代连接,可以在部署中再次创建其他连接。

使用 Azure CLI

在 Azure CLI 中使用 az spring connection create 命令将 Spring 应用配置为连接到 Cosmos NoSQL 数据库,如以下示例所示。 请务必将示例中的变量替换为实际值。

注意

更新 Azure Cosmos DB 数据库设置可能需要几分钟才能完成。

注意

如果使用 Cosmos Cassandra,请使用 --key_space 而不是 --database。 如果使用 Cosmos 表,请使用 --table 而不是 --database。 有关详细信息,请参阅快速入门:使用 Azure CLI 在 Azure Spring Apps 中创建服务连接

az spring connection create cosmos-sql \
    --resource-group $AZURE_SPRING_APPS_RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME \
    --app $APP_NAME \
    --target-resource-group $COSMOSDB_RESOURCE_GROUP \
    --account $COSMOSDB_ACCOUNT_NAME \
    --database $DATABASE_NAME \
    --secret

注意

如果是首次使用服务连接器,请首先运行 az provider register --namespace Microsoft.ServiceLinker 命令以注册服务连接器资源提供程序。

提示

运行 az spring connection list-support-types --output table 命令以获取 Azure Spring Apps 支持的目标服务和身份验证方法的列表。 如果系统未识别 az spring 命令,请检查是否已通过运行 az extension add --name spring 安装了所需的扩展。

使用 Azure 门户

或者,可以通过完成以下步骤,使用 Azure 门户配置此连接。 Azure 门户提供与 Azure CLI 相同的功能,并提供交互式体验。

  1. 在 Azure 门户中选择 Azure Spring Apps 实例,然后从导航菜单中选择“应用”。 选择要连接的应用,然后在导航菜单中选择“服务连接器”

  2. 选择创建

  3. 在“基本信息”选项卡上,对于“服务类型”,选择“Cosmos DB”,然后选择一个订阅。 对于“API 类型”,请选择“Core (SQL)”,然后选择 Cosmos DB 帐户和数据库。 对于“客户端类型”,请选择“Java”,然后选择“下一步: 身份验证”。 如果尚未创建数据库,请参阅快速入门:从 Azure 门户创建 Azure Cosmos DB 帐户、数据库、容器和项

  4. 在“身份验证”选项卡上,选择“连接字符串”。 服务连接器会自动从 Cosmos DB 帐户检索访问密钥。 选择“下一页:网络”。

  5. 在“网络”选项卡上,选择“配置防火墙规则以启用对目标服务的访问权限”,然后选择“下一步: 查看 + 创建”

  6. 在“查看 + 创建”选项卡上,等待验证通过,然后选择“创建”。 完成创建可能需要几分钟时间。

  7. 生成 Spring 应用和 Cosmos DB 数据库之间的连接后,可以在服务连接器页中看到该连接,并且可通过选择展开按钮查看配置的连接变量。

后续步骤

在本文中,你已了解如何将 Azure Spring Apps 中的应用程序连接到 Azure Cosmos DB 数据库。 若要详细了解如何将服务连接到应用程序,请参阅连接到 Azure Cache for Redis 缓存