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

将 Azure Database for PostgreSQL 绑定到 Azure Spring Apps 中的应用程序

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

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

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

可以通过 Azure Spring Apps 将所选 Azure 服务自动绑定到应用程序,而不必手动配置 Spring Boot 应用程序。 本文介绍如何将应用程序绑定到 Azure Database for PostgreSQL 实例。

本文介绍两种身份验证方法:Microsoft Entra 身份验证和 PostgreSQL 身份验证。 “无密码”选项卡可显示 Microsoft Entra 身份验证,“密码”选项卡则显示 PostgreSQL 身份验证。

Microsoft Entra 身份验证是一种使用 Microsoft Entra ID 中定义的标识连接到 Azure Database for PostgreSQL 的机制。 通过 Microsoft Entra 身份验证,可以在一个中心位置集中管理数据库用户标识和其他 Microsoft 服务,从而简化权限管理。

PostgreSQL 身份验证使用存储在 PostgreSQL 中的帐户。 如果你选择使用密码作为帐户的凭据,这些凭据将存储在用户表中。 由于这些密码存储在 PostgreSQL 中,因此你需要自行管理密码的轮换。

先决条件

准备项目

使用以下步骤准备项目。

  1. 在项目的 pom.xml 文件中,添加以下依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-starter-jdbc-postgresql</artifactId>
    </dependency>
    
  2. 在 application.properties 文件中,删除所有 spring.datasource.* 属性。

  3. 通过运行 az spring app deploy 更新当前应用,或者通过运行 az spring app deployment create 针对此更改创建新的部署。

将应用绑定到 Azure Database for PostgreSQL 实例

注意

请务必仅选择以下方法之一来创建连接。 如果已创建具有一个连接的表,则其他用户无法访问或修改表。 尝试其他方法时,应用程序将引发错误,例如“权限被拒绝”。 若要解决此问题,请连接到新数据库或删除并重新创建现有数据库。

注意

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

  1. 为 Azure CLI 安装服务连接器无密码扩展:

    az extension add --name serviceconnector-passwordless --upgrade
    
  2. 使用 az spring connection create 命令,将 Azure Spring Apps 配置为使用系统分配的托管标识连接到 PostgreSQL 数据库。

    az spring connection create postgres-flexible \
        --resource-group $AZ_SPRING_APPS_RESOURCE_GROUP \
        --service $AZ_SPRING_APPS_SERVICE_INSTANCE_NAME \
        --app $APP_NAME \
        --deployment $DEPLOYMENT_NAME \
        --target-resource-group $POSTGRES_RESOURCE_GROUP \
        --server $POSTGRES_SERVER_NAME \
        --database $DATABASE_NAME \
        --system-identity
    

先决条件

准备项目

使用以下步骤准备项目。

  1. 在项目的 pom.xml 文件中,添加以下依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-starter-jdbc-postgresql</artifactId>
    </dependency>
    
  2. 在 application.properties 文件中,删除所有 spring.datasource.* 属性。

  3. 通过运行 az spring app deploy 更新当前应用,或者通过运行 az spring app deployment create 针对此更改创建新的部署。

将应用绑定到 Azure Database for PostgreSQL 实例

注意

服务连接程序是在部署级别创建的。 因此,如果创建了另一个部署,则需要再次创建连接。

  1. 为 Azure CLI 安装服务连接器无密码扩展:

    az extension add --name serviceconnector-passwordless --upgrade
    
  2. 使用 az spring connection create 命令,将 Azure Spring Apps 配置为使用系统分配的托管标识连接到 PostgreSQL 数据库。

    az spring connection create postgres \
        --resource-group $AZ_SPRING_APPS_RESOURCE_GROUP \
        --service $AZ_SPRING_APPS_SERVICE_INSTANCE_NAME \
        --app $APP_NAME \
        --deployment $DEPLOYMENT_NAME \
        --target-resource-group $POSTGRES_RESOURCE_GROUP \
        --server $POSTGRES_SERVER_NAME \
        --database $DATABASE_NAME \
        --system-identity
    

后续步骤

本文介绍了如何将 Azure Spring Apps 中的应用程序绑定到 Azure Database for PostgreSQL 实例。 若要详细了解如何将服务绑定到应用程序,请参阅将 Azure Cosmos DB 数据库绑定到 Azure Spring Apps 中的应用程序