以最快速度启动 Azure Spring Apps (ASA)

以最快速度启动Azure Spring Apps(ASA)。

spring-boot_on_azure-spring-apps.png

为了某种目标而行动或达到某种结果。

在Windows 11上使用Linux进行云开发。

您可以在这里查看文章的列表。

 

实现

使用Ubuntu本地环境,借助Shell脚本将Spring Boot JAR文件格式的应用程序部署到Azure Spring Apps上。

首先

大家喜欢使用 Azure 吗?这次我们想尝试一下不同的方法,尽快启动 Azure Spring Apps。

Microsoft Azure 提供了一个可以通过 Web 浏览器轻松上传 Web 应用程序到云环境的平台。这样一来,您无需输入命令,就能简单地将 Web 应用程序部署到 Azure 上。本文为了学习目的,尝试介绍了如何通过 Ubuntu 上的 Shell 脚本来启动 Spring Apps。请注意。

相关文章

让我们与其他Azure PaaS服务进行比较!

 

开发环境

    Windows 11 Home 22H2 を使用しています。
由于我将操作WSL上的Ubuntu,所以您也可以参考macOS的使用方法。
WSL(Microsoft Store 应用程序版)※ 您可以在相关文章中查看安装方法
> wsl –version
WSL 版本:1.0.3.0
内核版本:5.15.79.1
WSLg 版本:1.0.47Ubuntu ※ 您可以在相关文章中查看安装方法
$ lsb_release -a
无可用的 LSB 模块。
发行商 ID:Ubuntu
描述:Ubuntu 22.04.1 LTS
发行版本:22.04

Java JDK ※ 您可以在相关文章中查看安装方法
$ java -version
openjdk version “11.0.18” 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)

Maven ※ 您可以在相关文章中查看安装方法
$ mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.18, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64

Azure CLI ※ 您可以在相关文章中查看安装方法
$ az –version
azure-cli 2.45.0
core 2.45.0
telemetry 1.0.8

在这篇文章中,主要是通过Ubuntu的终端操作。为了帮助初学者学习如何使用Vim进行复制粘贴,下面的文章介绍了详细的步骤。请务必尝试一下。

 

创建Spring Boot应用程序

您可以在这篇相关文章中确认步骤。

 

在这个过程中,已经在项目文件夹中创建了一个名为 target/app.jar 的JAR文件格式的Web应用程序。

进入项目文件夹

我們切換至專案資料夾。
※ 假設 ~/tmp/hello-spring-boot 為專案資料夾。

$ cd ~/tmp/hello-spring-boot

启动Spring Apps

我会编写一个 Shell 脚本。

$ vim create_spring_apps.sh

依据情况,请修改以下三个参数。

编号 参数 内容

1 resource_group_name=rg-hello rg-hello 资源组的名称。

2 spring_service_name=aps-hello aps-hello Spring Apps 服务的名称。

3 spring_app_name=sap-hello-spring-boot sap-hello-spring-boot Spring Apps 应用的名称。

#!/bin/sh

# set variables.
location_name=japaneast
resource_group_name=rg-hello
spring_service_name=aps-hello
spring_app_name=sap-hello-spring-boot

# create a resource group
az group create \
    --name $resource_group_name \
    --location $location_name

# create a spring service.
az spring create \
    --resource-group $resource_group_name \
    --name $spring_service_name \
    --location $location_name \
    --sku Basic

# create a spring app.
az spring app create \
    --resource-group $resource_group_name \
    --service $spring_service_name \
    --name $spring_app_name \
    --runtime-version Java_11 \
    --assign-endpoint true

# deploy a spring app.
az spring app deploy \
    --resource-group $resource_group_name \
    --service $spring_service_name \
    --name $spring_app_name \
    --artifact-path target/app.jar

# get a spring service id.
spring_service_id=$(az spring list \
    --query "[0].id" \
    -o tsv)

# get a signed in user id.
signed_in_user_id=$(az ad signed-in-user show \
    --query id \
    -o tsv)

# create a spring app connect role.
az role assignment create \
    --role 'Azure Spring Apps Connect Role' \
    --scope $spring_service_id \
    --assignee $signed_in_user_id

# get a spring app data.
curl https://$spring_service_name-$spring_app_name.azuremicroservices.io/api/data -w '\n'

给予执行权限。

$ chmod +x create_spring_apps.sh

登入 Azure。

$ az login

您可以在这篇相关文章中查看步骤。

 

执行Shell脚本。

$ ./create_spring_apps.sh

当部署完成后,会显示以下内容。

{"message":"Hello World!"}
在执行Shell脚本的命令之后,直到显示”Hello World!”为止,经过了18分14秒的时间。

打开 Azure 门户并查看

image.png

删除Spring Apps

我要创建一个Shell脚本。

$ vim delete_resource_group.sh

请将以下参数与上面创建的参数进行匹配。

| No | 行 | 参数 | 内容 |
|—-|—-|——|——|
| 1 | resource_group_name=rg-hello | rg-hello | 资源组的名称。|

文件的内容 de

#!/bin/sh

# set variables.
resource_group_name=rg-hello

# delete resource group
az group delete \
    --name $resource_group_name \
    --yes

添加执行权限。

$ chmod +x delete_resource_group.sh

执行shell脚本。

$ ./delete_resource_group.sh

在删除过程中,将显示如下。

 / Running ..

总结

在Azure Spring Apps环境中,可以轻松启动以JAR文件格式的Spring Boot Web应用程序。

在实际工作中,您可以手动在Spring Apps中部署Web应用程序。例如,您可以在Azure门户上手动进行设置,还可以使用CI/CD管道自动部署。但是,通过阅读这个脚本,您可以了解部署Web应用程序到Spring Apps所需的步骤。您还可以尝试进一步改进这个脚本。

请问您对于在WSL Ubuntu上能够便捷地搭建Azure云应用开发环境有何看法?请务必试一试。我们将继续介绍Azure的开发环境等内容,请务必期待。

推荐内容

 

bannerAds