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

注意

基本标准计划于 2025 年 3 月 17 日进入退休期。 有关详细信息,请参阅 Azure Spring Apps 停用公告

标准消耗和专用计划于 2024 年 9 月 30 日进入停用期,并将在 2025 年 3 月底之前完全关闭。

可以通过 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

以下 Terraform 脚本演示如何使用 Azure Cosmos DB 帐户设置一个部署到 Azure Spring Apps 的应用。

provider "azurerm" {
  features {}
}

variable "application_name" {
  type        = string
  description = "The name of your application"
  default     = "demo-cosmosdb"
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "China North 2"
}

resource "azurerm_cosmosdb_account" "cosmosdb" {
  name                = "cosmosacct-${var.application_name}-001"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  offer_type          = "Standard"
  kind                = "GlobalDocumentDB"

  consistency_policy {
    consistency_level = "Session"
  }

  geo_location {
    failover_priority = 0
    location          = azurerm_resource_group.example.location
  }
}

resource "azurerm_cosmosdb_sql_database" "cosmosdb" {
  name                = "cosmos-${var.application_name}-001"
  resource_group_name = azurerm_cosmosdb_account.cosmosdb.resource_group_name
  account_name        = azurerm_cosmosdb_account.cosmosdb.name
}

resource "azurerm_spring_cloud_service" "example" {
  name                = "${var.application_name}"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
}

resource "azurerm_spring_cloud_app" "example" {
  name                = "${var.application_name}-app"
  resource_group_name = azurerm_resource_group.example.name
  service_name        = azurerm_spring_cloud_service.example.name
  is_public           = true
  https_only          = true
}

resource "azurerm_spring_cloud_java_deployment" "example" {
  name                = "default"
  spring_cloud_app_id = azurerm_spring_cloud_app.example.id
  quota {
    cpu    = "2"
    memory = "4Gi"
  }
  instance_count      = 1
  jvm_options         = "-XX:+PrintGC"
  runtime_version     = "Java_11"

  environment_variables = {
    "spring.cloud.azure.cosmos.endpoint" : azurerm_cosmosdb_account.cosmosdb.endpoint
    "spring.cloud.azure.cosmos.key" : azurerm_cosmosdb_account.cosmosdb.primary_key
    "spring.cloud.azure.cosmos.database" : azurerm_cosmosdb_sql_database.cosmosdb.name
  }
}

resource "azurerm_spring_cloud_active_deployment" "example" {
  spring_cloud_app_id = azurerm_spring_cloud_app.example.id
  deployment_name     = azurerm_spring_cloud_java_deployment.example.name
}

后续步骤

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