將 適用於 PostgreSQL 的 Azure 資料庫 系結至 Azure Spring Apps 中的應用程式
注意
Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。
本文適用於: ✔️ Java ✔️ C#
本文適用於: ✔️基本/標準✔️企業
使用 Azure Spring Apps,您可以自動將選取 Azure 服務系結至您的應用程式,而不必手動設定 Spring Boot 應用程式。 本文說明如何將應用程式系結至 適用於 PostgreSQL 的 Azure 資料庫 實例。
在本文中,我們包含兩種驗證方法:Microsoft Entra 驗證和 PostgreSQL 驗證。 [無密碼] 索引標籤會顯示 Microsoft Entra 驗證,而 [密碼] 索引標籤會顯示 PostgreSQL 驗證。
Microsoft Entra 驗證是使用 Microsoft Entra 識別碼中定義的身分識別連線到 適用於 PostgreSQL 的 Azure 資料庫 的機制。 透過 Microsoft Entra 驗證,您可以在中央位置管理資料庫使用者身分識別和其他 Microsoft 服務,以簡化許可權管理。
PostgreSQL 驗證會使用儲存在 PostgreSQL 中的帳戶。 如果您選擇使用密碼作為帳戶的認證,這些認證會儲存在用戶數據表中。 因為這些密碼會儲存在PostgreSQL中,因此您必須自行管理密碼的輪替。
必要條件
- 部署至 Azure Spring Apps 的應用程式。 如需詳細資訊,請參閱 快速入門:將第一個應用程式部署至 Azure Spring Apps。
- 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器實例。
- Azure CLI 2.45.0 版或更高版本。
準備您的專案
使用下列步驟來準備您的專案。
在專案的 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>
在 application.properties 檔案中,移除任何
spring.datasource.*
屬性。執行
az spring app deploy
來更新目前的應用程式,或執行az spring app deployment create
來建立此變更的新部署。
將您的應用程式系結至 適用於 PostgreSQL 的 Azure 資料庫 實例
注意
請務必只選取下列其中一種方法來建立連線。 如果您已經建立具有一個連線的數據表,其他使用者就無法存取或修改數據表。 當您嘗試其他方法時,應用程式會擲回錯誤,例如「許可權遭拒」。 若要修正此問題,請連線到新的資料庫或刪除並重新建立現有的資料庫。
注意
根據預設,服務 連線 器會在應用層級建立。 若要覆寫連線,您可以在部署中再次建立其他連線。
安裝 Azure CLI 的服務 連線 或無密碼擴充功能:
az extension add --name serviceconnector-passwordless --upgrade
設定 Azure Spring Apps 以使用
az spring connection create
命令連線到 Postgre SQL 資料庫 系統指派的受控識別。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
必要條件
- 部署至 Azure Spring Apps 的應用程式。 如需詳細資訊,請參閱 快速入門:將第一個應用程式部署至 Azure Spring Apps。
- 適用於 PostgreSQL 的 Azure 資料庫 單一伺服器實例。
- Azure CLI 2.45.0 版或更高版本。
準備您的專案
使用下列步驟來準備您的專案。
在專案的 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>
在 application.properties 檔案中,移除任何
spring.datasource.*
屬性。執行
az spring app deploy
來更新目前的應用程式,或執行az spring app deployment create
來建立此變更的新部署。
將您的應用程式系結至 適用於 PostgreSQL 的 Azure 資料庫 實例
注意
服務 連線 器是在部署層級建立的。 因此,如果已建立另一個部署,您必須再次建立連線。
安裝 Azure CLI 的服務 連線 或無密碼擴充功能:
az extension add --name serviceconnector-passwordless --upgrade
設定 Azure Spring Apps 以使用
az spring connection create
命令,以系統指派的受控識別連線到 Postgre SQL 資料庫。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 中的應用程式系結至 適用於 PostgreSQL 的 Azure 資料庫 實例。 若要深入瞭解如何將服務系結至應用程式,請參閱 將 Azure Cosmos DB 資料庫系結至 Azure Spring Apps 中的應用程式。