使用Azure App Service在Docker Hub自定义容器映像上启动Spring Boot WEB服务
使用Docker Hub自定义容器映像在Azure App Service上启动Spring Boot WEB服务。
为了…
通过在Azure App Service环境中启动Spring Boot的WEB服务,来加深对其的理解。
实现
将Spring Boot WEB应用程序的自定义容器映像部署到Microsoft Azure App Service。
还有以下文章作为使用 Microsoft Azure Container Registry (ACR) 进行配置的示例。
科技背景
微软 Azure 应用服务是什么?
Microsoft Azure App Service 是 Microsoft Azure 平台即服务(PaaS)服务之一,专注于托管WEB应用程序、移动应用程序和API应用程序的服务。
Azure App Service 的主要特点和优势如下。
简易部署
Azure App Service 可以使用 Azure 门户、Azure CLI、Azure DevOps 等工具轻松部署 WEB 应用程序。此外,它支持多种编程语言和框架,可以进行灵活的开发。
可伸缩性
Azure App Service 支持自动缩放和手动缩放,可以根据应用程序负载灵活缩放。
高可用性
Azure App Service 具备全球负载均衡和故障转移功能,可以实现高可用性。
集成
Azure App Service 可以与其他 Azure 服务无缝集成。例如,与 Azure SQL 数据库或 Azure 存储等存储服务集成,轻松管理应用程序数据。
安全性
Azure App Service 提供网络安全组(NSG)和 WEB 应用程序防火墙(WAF)等安全功能,可以增加应用程序的安全性。
总结
由于这些特点和优势,Azure App Service 被开发人员和企业用作支持快速且灵活的 WEB 应用程序开发和部署的平台。
Development environment – 开发环境
-
- Windows 11 Home 22H2 を使用しています。
WSL の Ubuntu を操作していきますので macOS の方も参考にして頂けます。
> 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 JDK的安装和Hello World!
$ java -version
openjdk版本”11.0.17″ 2022年10月18日
OpenJDK运行时环境(构建11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64位服务器VM(构建11.0.17+8-post-Ubuntu-1ubuntu222.04,混合模式,共享)
Maven ※ Maven的安装和Hello World!
$ mvn -version
Apache Maven 3.6.3
Maven主目录:/usr/share/maven
Java版本:11.0.17,供应商:Ubuntu,运行时:/usr/lib/jvm/java-11-openjdk-amd64
Docker Desktop
版本4.16.3(96739)
$ docker –version
Docker版本20.10.22,构建3a2c30b
$ docker-compose –version
Docker Compose版本v2.15.1
※ 本文主要介绍在Ubuntu终端中进行操作。
展示 “Hello World” 的步骤
创建Spring Boot WEB服务
您可以参考这一点。
进入项目文件夹
假设将~/tmp/hello-spring-boot作为项目文件夹。
$ cd ~/tmp/hello-spring-boot
用 Java 构建应用程序(*参考)
只需要一個選項,以下是本地化為中文的改寫:
※ 將建立目標/app.jar。
※ 在創建下面的容器映像時,同時創建目標/app.jar,這不是必需的。
$ mvn clean install
创建容器映像
需要编辑 Dockerfile。
请注意,在验证阶段,使用原始的 spring-boot:build-image 不会运行,必须明确添加 EXPOSE 8080 的设置。
$ vim Dockerfile
文件的内容
EXPOSE 8080
构建容器镜像
在本地的Docker环境(Docker Desktop)中,将创建app-hello-spring-boot容器镜像。
容器镜像的创建时间将符合Unix时代的规范,这是spring-boot:build-image的规格。
$ mvn spring-boot:build-image \
-Dspring-boot.build-image.imageName=app-hello-spring-boot \
-Dspring-boot.build-image.dockerfile=Dockerfile
确认容器镜像
$ docker images | grep app-hello-spring-boot
app-hello-spring-boot latest e37cc77f2b36 43 years ago 262MB
在本地启动容器镜像并进行确认。
$ docker run --name app-local -p 8080:8080 app-hello-spring-boot
请使用curl命令从另一个终端进行确认。
我将检查8080端口是否正在监听。
$ curl -v http://localhost:8080/api/data
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /api/data HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Mon, 27 Feb 2023 02:15:38 GMT
<
* Connection #0 to host localhost left intact
{"message":"Hello World!"}
将容器镜像注册到Docker Hub。
请将 “$USER” 替换为您自己的容器注册表。
给这些标签加上标签
$ docker tag app-hello-spring-boot $USER/app-hello-spring-boot:latest
進一步推進
$ docker push $USER/app-hello-spring-boot:latest
我已经将自定义的容器镜像作为 $USER/app-hello-spring-boot:latest 在Docker Hub 上发布了。
获取Azure账号
使用Azure的免费账户在云中建立。
使用Azure CLI进行登录
您可以参考这个。
登录
$ az login
※ 如果要更新到最新版本
$ az upgrade
Azure 环境
资源组
创建资源组
$ az group create \
--name rg-hello \
--location japaneast
显示资源组列表
$ az group list
※ 如果要删除资源组。
$ az group delete -n <group>
App服务计划
创建App Service计划
$ az appservice plan create \
--resource-group rg-hello \
--name asp-hello \
--sku Free \
--is-linux
展示App Service计划的列表。
$ az appservice plan list
※ 如果要删除 App Service 计划
$ az appservice plan delete -n <plan> -g <group>
应用服务 WEB 应用
在Azure App Service上部署Spring Boot WEB应用程序的容器映像有几种方法,但本次将介绍如何使用Azure CLI部署Docker Hub上的公共自定义容器映像。
创建和部署Web应用程序
※ 请将$USER替换为您自己的容器注册表。
$ az webapp create \
--resource-group rg-hello \
--plan asp-hello \
--name app-hello-spring-boot \
--deployment-container-image-name $USER/app-hello-spring-boot:latest
配置Web应用程序的设置。
※ App Service Web应用程序默认监听80端口。
$ az webapp config appsettings set \
--resource-group rg-hello \
--name app-hello-spring-boot \
--settings WEBSITES_PORT=8080
Web应用程序日志记录的配置
$ az webapp log config \
--resource-group rg-hello \
--name app-hello-spring-boot \
--docker-container-logging filesystem \
--application-logging filesystem \
--web-server-logging filesystem
通过网络浏览器访问
$ az webapp browse \
--resource-group rg-hello \
--name app-hello-spring-boot
尽管需要一些时间来启动,但页面将会显示出来。然而,显示的页面 “/” 并未设置任何内容。
请在网络浏览器中再次确认。※请根据环境替换URL。
https://app-hello-spring-boot.azurewebsites.net/api/data
在WEB浏览器上显示了{“message”:”Hello World!”}并成功获取了JSON数据。
※ 从另一个终端使用curl命令进行确认
$ curl -v https://app-hello-spring-boot.azurewebsites.net/api/data
* Trying 20.43.67.36:443...
* Connected to app-hello-spring-boot.azurewebsites.net (20.43.67.36) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS header, Finished (20):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS header, Finished (20):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; CN=*.azurewebsites.net
* start date: Dec 27 21:12:39 2022 GMT
* expire date: Dec 22 21:12:39 2023 GMT
* subjectAltName: host "app-hello-spring-boot.azurewebsites.net" matched cert's "*.azurewebsites.net"
* issuer: C=US; O=Microsoft Corporation; CN=Microsoft Azure TLS Issuing CA 05
* SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* Using Stream ID: 1 (easy handle 0x55b794ea9e80)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> GET /api/data HTTP/2
> Host: app-hello-spring-boot.azurewebsites.net
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 200
< content-type: application/json
< date: Mon, 27 Feb 2023 02:51:05 GMT
< set-cookie: ARRAffinity=ae907ce840c6b95b786870ca4c1362dc92f4733f1b7145fe24a6c8a07a0d5ff4;Path=/;HttpOnly;Secure;Domain=app-hello-spring-boot.azurewebsites.net
< set-cookie: ARRAffinitySameSite=ae907ce840c6b95b786870ca4c1362dc92f4733f1b7145fe24a6c8a07a0d5ff4;Path=/;HttpOnly;SameSite=None;Secure;Domain=app-hello-spring-boot.azurewebsites.net
<
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Connection #0 to host app-hello-spring-boot.azurewebsites.net left intact
{"message":"Hello World!"}
在终端上显示了{“message”:”Hello World!”},并成功获取到了JSON数据。
※ Azure会在其端使用SSL/TLS协议和HTTP/2协议。
总结
-
- Ubuntu のシンプルな構成の Java 開発環境で Spring Boot WEBサービスのカスタムコンテナイメージを Azure App Service 環境で起動させることが出来ました。
- コンテナに接続する為にAzure App Service にデプロイした Spring Boot コンテナに SSH 接続するに続きます。
请参考
[Microsoft Learn] Azure App Service命令参考资料