Spring Boot WEBサービスを ASA (Azure Spring Apps) 環境で起動する (jar ファイル形式)
目的
Spring Boot WEBサービスを Azure Spring Apps 環境で起動して理解を深めます。
実現すること
Microsoft Azure Spring Apps に Spring Boot の jar ファイル形式のWEBアプリをデプロイ・起動します。
技術背景
Microsoft Azure Spring Apps (ASA) とは?
こちらを展開してご覧いただけます。
Microsoft Azure Spring Apps (ASA)
Spring Framework で構築されたアプリケーションを Azure 上で実行するためのサービスです。
特徴としては、以下のような点が挙げられます。
- オープンソースであり、Spring Boot、Spring Cloud、Spring Cloud Data Flow などの Spring フレームワークをサポートしている。
- アプリケーションのデプロイやスケーリング、モニタリング、ログ収集などの管理を自動化している。
- アプリケーションを Docker コンテナにパッケージ化し、Kubernetes 上で実行することができる。
- Azure Active Directory や Azure Key Vault などの Azure サービスとの統合をサポートしている。
メリットとしては、以下のような点が挙げられます。
- サービスの管理が自動化されているため、アプリケーションの開発者はインフラストラクチャの管理に時間を費やすことがなくなります。
- Azure 上でアプリケーションを実行することで、高い可用性やスケーラビリティを実現できます。
- Azure サービスとの統合が簡単にできるため、セキュリティやアイデンティティの管理が簡単になります。
- コンテナ化されたアプリケーションは、開発から本番環境まで同じ環境で実行できるため、開発と運用のスピードを向上できます。
開発環境
- Windows 11 Home 22H2 を使用しています。
- WSL の Ubuntu を操作していきますので macOS の方も参考にして頂けます。
WSL (Microsoft Store アプリ版)
> wsl --version
WSL バージョン: 1.0.3.0
カーネル バージョン: 5.15.79.1
WSLg バージョン: 1.0.47
Ubuntu
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Java JDK ※ Java JDK の導入と Hello World!
$ java -version
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)
Maven ※ Maven の導入と Hello World!
$ mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
※ この記事では基本的に Ubuntu のターミナルで操作を行います。
"Hello World" を表示する手順
Spring Boot WEBサービスの作成
Spring Boot WEBサービスの Hello World! を参照してください。
プロジェクトフォルダに移動
※ ~/tmp/hello-spring-boot をプロジェクトフォルダとします。
$ cd ~/tmp/hello-spring-boot
Java アプリをビルド
※ target/app.jar が作成されます。
$ mvn clean install
Azure のアカウントを取得
Azure CLI でサインイン
Azure CLI をインストールする手順 を参照してください。
$ az login
Azure 環境
リソースグループ
リソースグループを作成
$ az group create \
--name rg-hello \
--location japaneast
リソースグループ一覧表示
$ az group list
※ リソースグループを削除する場合
$ az group delete -n <group>
Spring Apps サービスインインスタンス
※ Azure Spring Apps に Spring Boot jar ファイル形式のWEBアプリをデプロイする方法はいくつかありますが、今回は Azure CLI で jar ファイルをデプロイする方法を紹介します。
Spring Apps 拡張機能をインストール ※初回のみ
$ az extension add --name spring
※ 拡張機能の一覧を表示する場合
$ az extension list
Spring Apps サービスインスタンスを作成
※ Kubernetes 基盤の環境だと思われるので少し(※初回は特に)時間が掛かるみたいです。
※ --sku Free では作成できません。
$ az spring create \
--resource-group rg-hello \
--name aps-hello \
--location japaneast \
--sku Basic
Spring Apps サービスインスタンスの一覧表示
$ az spring list
※ Spring Apps サービスインスタンスを削除する場合 (※ y/n 無し:即時)
$ az spring delete -n <service> -g <group>
Spring Apps アプリ
Spring Apps アプリを作成
$ az spring app create \
--resource-group rg-hello \
--service aps-hello \
--name sap-hello-spring-boot \
--runtime-version Java_11 \
--assign-endpoint true
Spring Apps アプリの一覧表示
$ az spring app list -s <service> -g <group>
※ Spring Apps アプリを削除する場合 (※ y/n 無し:即時)
$ az spring app delete -n <name> -s <service> -g <group>
デプロイ
$ az spring app deploy \
--resource-group rg-hello \
--service aps-hello \
--name sap-hello-spring-boot \
--artifact-path target/app.jar
デプロイ成功後、以下のURLでアクセス可能
https://<service-name>-<app-name>.azuremicroservices.io
WEBブラウザで確認
※ URLは環境で読み替えて下さい。
https://aps-hello-sap-hello-spring-boot.azuremicroservices.io/api/data
WEBブラウザに {"message":"Hello World!"} と表示され、JSON データを取得することが出来ました。
※ もしくは別ターミナルのコマンドで確認
$ curl -v https://aps-hello-sap-hello-spring-boot.azuremicroservices.io/api/data
* Trying 20.43.70.0:443...
* Connected to aps-hello-sap-hello-spring-boot.azuremicroservices.io (20.43.70.0) 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 header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* 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-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; CN=*.azuremicroservices.io
* start date: Sep 29 06:09:42 2022 GMT
* expire date: Sep 24 06:09:42 2023 GMT
* subjectAltName: host "aps-hello-sap-hello-spring-boot.azuremicroservices.io" matched cert's "*.azuremicroservices.io"
* issuer: C=US; O=Microsoft Corporation; CN=Microsoft Azure TLS Issuing CA 02
* 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 0x556c0195fe80)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> GET /api/data HTTP/2
> Host: aps-hello-sap-hello-spring-boot.azuremicroservices.io
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 200
< date: Sat, 25 Feb 2023 02:02:15 GMT
< content-type: application/json
< strict-transport-security: max-age=15724800; includeSubDomains
<
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Connection #0 to host aps-hello-sap-hello-spring-boot.azuremicroservices.io left intact
{"message":"Hello World!"}
ターミナルに {"message":"Hello World!"} と表示され、JSON データを取得することが出来ました。
※ Azure が SSL/TLSと HTTP/2プロトコルに自動的に対応してくれます。
まとめ
- Ubuntu のシンプルな構成の Java 開発環境で Spring Boot WEBサービスを Azure Spring Apps 環境で起動させることが出来ました。