Microsoft 建议使用最安全的可用身份验证流。 本过程中介绍的身份验证流程(例如数据库、缓存、消息传送或 AI 服务)需要非常高的信任度,并携带其他流中不存在的风险。 仅当更安全的选项(例如无密码连接或无密钥连接的托管标识)不可行时,才使用此流。 对于本地计算机操作,首选无密码连接或无密钥连接的用户标识。
选择要在计算服务和目标服务之间使用的身份验证类型:选择“连接字符串”。
使用...以继续:选择“数据库凭据”
用户名:myadmin
密码:输入密码。
选择“下一页:网络”。 使用默认选项“配置防火墙规则以启用对目标服务的访问权限”。
选择“下一步: 查看并创建”以查看选择,然后选择“创建”来创建连接。
3.1. 提供每个资源的名称
使用以下命令创建变量以保存资源名称。 请务必将占位符替换为你自己的值。
注意
Microsoft 建议使用最安全的可用身份验证流。 本过程中介绍的身份验证流程(例如数据库、缓存、消息传送或 AI 服务)需要非常高的信任度,并携带其他流中不存在的风险。 仅当更安全的选项(例如无密码连接或无密钥连接的托管标识)不可行时,才使用此流。 对于本地计算机操作,首选无密码连接或无密钥连接的用户标识。
az group create --resource-group ${RESOURCE_GROUP}
使用以下命令将新创建的资源组设置为默认资源组。
az configure --defaults group=${RESOURCE_GROUP}
3.3. 创建 Azure Spring Apps 实例
Azure Spring Apps 用于托管 Spring Web 应用。 创建 Azure Spring Apps 实例和其中的应用程序。
使用以下命令创建 Azure Spring Apps 服务实例。
az spring create --name ${AZURE_SPRING_APPS_NAME} --sku enterprise
使用以下命令在 Azure Spring Apps 实例中创建应用程序。
az spring app create \
--service ${AZURE_SPRING_APPS_NAME} \
--name ${APP_NAME} \
--assign-endpoint true
3.4. 准备 PostgreSQL 实例
Spring Web 应用将 H2 用于 localhost 中的数据库,将 Azure Database for PostgreSQL 用于 Azure 中的数据库。
使用以下命令创建 PostgreSQL 实例:
注意
Microsoft 建议使用最安全的可用身份验证流。 本过程中介绍的身份验证流程(例如数据库、缓存、消息传送或 AI 服务)需要非常高的信任度,并携带其他流中不存在的风险。 仅当更安全的选项(例如无密码连接或无密钥连接的托管标识)不可行时,才使用此流。 对于本地计算机操作,首选无密码连接或无密钥连接的用户标识。
Microsoft 建议使用最安全的可用身份验证流。 本过程中介绍的身份验证流程(例如数据库、缓存、消息传送或 AI 服务)需要非常高的信任度,并携带其他流中不存在的风险。 仅当更安全的选项(例如无密码连接或无密钥连接的托管标识)不可行时,才使用此流。 对于本地计算机操作,首选无密码连接或无密钥连接的用户标识。
az spring app update \
--service ${AZURE_SPRING_APPS_NAME} \
--name ${APP_NAME} \
--env SPRING_DATASOURCE_URL="jdbc:postgresql://${PSQL_FQDN}:5432/${POSTGRESQL_DB}?sslmode=require" \
SPRING_DATASOURCE_USERNAME="${POSTGRESQL_ADMIN_USERNAME}" \
SPRING_DATASOURCE_PASSWORD="${POSTGRESQL_ADMIN_PASSWORD}"
Initializing a new project (azd init)
(✓) Done: Initialized git repository
(✓) Done: Downloading template code to: <your-local-path>
Enter a new environment name: <your-env-name>
SUCCESS: New project initialized!
You can view the template code in your directory: <your-local-path>
Learn more about running 3rd party code on our DevHub: https://aka.ms/azd-third-party-code-notice
3. 准备云环境
运行此示例所需的主要资源是 Azure Spring Apps 实例和 Azure Database for PostgreSQL 实例。 本部分提供创建这些资源的步骤。
使用以下步骤创建 Azure Spring Apps 实例和 Azure Database for PostgreSQL 实例:
使用以下命令通过 OAuth2 登录到 Azure CLI。 如果已登录,请忽略此步。
azd auth login
控制台输出类似于以下示例的消息:
Logged in to Azure.
运行以下命令,以将模板的基础结构预配到 Azure:
azd provision
以下列表描述了命令交互:
请选择要使用的 Azure 订阅:使用箭头移动,键入以进行筛选,然后按 Enter。
请选择要使用的 Azure 位置:使用箭头移动,键入以进行筛选,然后按 Enter。
控制台输出类似于以下示例的消息:
SUCCESS: Your application was provisioned in Azure in xx minutes xx seconds.
You can view the resources created under the resource group rg-<your-environment-name> in Azure Portal:
https://portal.azure.com/#@/resource/subscriptions/<your-subscription-id>/resourceGroups/rg-<your-environment-name>/overview
选择订阅:选择所创建的 Azure Spring Apps 实例的订阅列表编号,它默认为列表中的第一个订阅。 如果使用默认编号,请直接按 Enter。
使用 Azure 中的现有 Azure Spring Apps:按 y 使用现有的 Azure Spring Apps 实例。
选择要部署的 Azure Spring Apps:选择创建的 Azure Spring Apps 实例的编号。 如果使用默认编号,请直接按 Enter。
公开此应用的公共访问权限:按 y。
确认保存上述所有配置:按 y。 如果按 n,则配置不会保存在 POM 文件中。
使用以下命令部署应用:
./mvnw azure-spring-apps:deploy
以下列表描述了命令交互:
OAuth2 登录:你需要根据 OAuth2 协议授权到 Azure 的登录。
执行命令后,你会从以下日志消息中看到部署已成功:
[INFO] Start updating app(simple-todo-web)...
[INFO] App(simple-todo-web) is successfully updated.
[INFO] Starting Spring App after deploying artifacts...
[INFO] Deployment Status: Running
[INFO] InstanceName:simple-todo-web--default-xxxxxxx-xxxxxxxxx-xxxxx Status:Running Reason:null DiscoverStatus:NONE
[INFO] Getting public url of app(simple-todo-web)...
[INFO] Application url: https://simple-todo-web.<unique-identifier>.<region-name>.azurecontainerapps.io
输出的应用程序 URL 是用于访问 todo 应用程序的终结点。
现在可以将应用部署到 Azure Spring Apps。 按照以下步骤打包应用,预配 Web 应用程序所需的 Azure 资源,然后部署到 Azure Spring Apps:
使用以下命令打包应用程序的可部署副本:
azd package
控制台输出类似于以下示例的消息:
SUCCESS: Your application was packaged for Azure in xx seconds.
使用以下命令将应用程序代码部署到这些新预配的资源:
azd deploy
控制台输出类似于以下示例的消息:
Deploying services (azd deploy)
(✓) Done: Deploying service simple-todo-web
- Endpoint: https://simple-todo-web.xxx.<your-azure-location>.azurecontainerapps.io
SUCCESS: Your application was deployed to Azure in xx minutes xx seconds.
You can view the resources created under the resource group rg-<your-environment-name> in Azure Portal:
https://portal.azure.com/#@/resource/subscriptions/<your-subscription-id>/resourceGroups/rg-<your-environment-name>/overview
Initializing a new project (azd init)
(✓) Done: Initialized git repository
(✓) Done: Downloading template code to: <your-local-path>
Please enter a new environment name: <your-env-name>
SUCCESS: New project initialized!
You can view the template code in your directory: <your-local-path>
Learn more about running 3rd party code on our DevHub: https://aka.ms/azd-third-party-code-notice
3. 准备云环境
运行此示例所需的主要资源是 Azure Spring Apps 实例和 Azure Database for PostgreSQL 实例。 本部分提供创建这些资源的步骤。
使用以下步骤创建 Azure Spring Apps 实例和 Azure Database for PostgreSQL 实例:
使用以下命令以通过 OAuth2 登录 Azure。 如果已登录,请忽略此步。
azd auth login
控制台输出类似于以下示例的消息:
Logged in to Azure.
使用以下命令通过标准计划设置模板:
azd env set PLAN standard
运行以下命令,以将模板的基础结构预配到 Azure:
azd provision
以下列表描述了命令交互:
请选择要使用的 Azure 订阅:使用箭头移动,键入以进行筛选,然后按 Enter。
请选择要使用的 Azure 位置:使用箭头移动,键入以进行筛选,然后按 Enter。
控制台输出类似于以下示例的消息:
SUCCESS: Your application was provisioned in Azure in xx minutes xx seconds.
You can view the resources created under the resource group rg-<your-environment-name> in Azure Portal:
https://portal.azure.com/#@/resource/subscriptions/<your-subscription-id>/resourceGroups/rg-<your-environment-name>/overview
选择订阅:选择所创建的 Azure Spring Apps 实例的订阅列表编号,它默认为列表中的第一个订阅。 如果使用默认编号,请直接按 ENTER。
选择 Azure Spring Apps:选择所创建的 Azure Spring Apps 实例的编号。 如果使用默认编号,请直接按 ENTER。
公开此应用的公共访问权限?:按 y。
确认保存上述所有配置 (Y/n):按 y。 如果按 n,则配置不会保存在 POM 文件中。
使用以下命令部署应用:
./mvnw azure-spring-apps:deploy
以下列表描述了命令交互:
OAuth2 登录:你需要根据 OAuth2 协议授权到 Azure 的登录。
执行命令后,你会从以下日志消息中看到部署已成功:
[INFO] Deployment(default) is successfully created
[INFO] Starting Spring App after deploying artifacts...
[INFO] Deployment Status: Running
[INFO] InstanceName:simple-todo-web-default-x-xxxxxxxxxx-xxxxx Status:Running Reason:null DiscoverStatus:UNREGISTERED
[INFO] InstanceName:simple-todo-web-default-x-xxxxxxxxx-xxxxx Status:Terminating Reason:null DiscoverStatus:UNREGISTERED
[INFO] Getting public url of app(simple-todo-web)...
[INFO] Application url: https://<your-Azure-Spring-Apps-instance-name>-simple-todo-web.azuremicroservices.io
输出的应用程序 URL 是用于访问 todo 应用程序的终结点。
现在可以将应用部署到 Azure Spring Apps。 按照以下步骤打包应用,预配 Web 应用程序所需的 Azure 资源,然后部署到 Azure Spring Apps:
使用以下命令打包应用程序的可部署副本:
azd package
控制台输出类似于以下示例的消息:
SUCCESS: Your application was packaged for Azure in xx seconds.
使用以下命令将应用程序代码部署到这些新预配的资源:
azd deploy
控制台输出类似于以下示例的消息:
Deploying services (azd deploy)
(✓) Done: Deploying service simple-todo-web
- Endpoint: https://<your-Azure-Spring-Apps-instance-name>-simple-todo-web.azuremicroservices.io/
SUCCESS: Your application was deployed to Azure in xx minutes xx seconds.
You can view the resources created under the resource group rg-<your-environment-name> in Azure Portal:
https://portal.azure.com/#@/resource/subscriptions/<your-subscription-id>/resourceGroups/rg-<your-environment-name>/overview