LoginSignup
0
0

Azure App Service に Spring Boot Webサービスをデプロイする (JAR ファイル形式)

Last updated at Posted at 2023-03-09

Azure App Service に Spring Boot Webサービスをデプロイする (JAR ファイル形式)

こんにちは、@studio_meowtoon です。今回は、Azure App Service 環境で Spring Boot Web アプリケーションを起動する方法を紹介します。
spring-boot_on_azure-app-service.png

目的

Windows 11 の Linux でクラウド開発します。

こちらから記事の一覧がご覧いただけます。

実現すること

Microsoft Azure App Service に、JAR ファイル形式の Spring Boot Web アプリケーションをデプロイします。

技術トピック

Microsoft Azure App Service とは?

こちらを展開してご覧いただけます。

Microsoft Azure App Service

Azure 上で実行されるコンテナアプリケーションのプラットフォームです。

Microsoft Azure App Service は、Microsoft Azure の PaaS (Platform as a Service) サービスの一つで、Web アプリケーションやモバイルアプリケーション、API アプリケーションのホスティングに特化したサービスです。

特徴とメリット 説明
簡単なデプロイ Azure App Service は、Azure Portal や Azure CLI、Azure DevOps などのツールを使って、Web アプリケーションを簡単にデプロイできます。また、多数のプログラミング言語やフレームワークに対応しており、柔軟な開発が可能です。
スケーラビリティ Azure App Service は、自動スケーリングや手動スケーリングをサポートしており、アプリケーションの負荷に応じて柔軟にスケールできます。
高可用性 Azure App Service は、グローバルな負荷分散やフェイルオーバー機能を備えており、高い可用性を実現できます。
統合 Azure App Service は、Azure の他のサービスとシームレスに統合できます。例えば、Azure SQL Database や Azure Storage などのストレージサービスと連携して、アプリケーションのデータ管理を容易にします。
セキュリティ Azure App Service は、ネットワークセキュリティグループ (NSG) や Web アプリケーションファイアウォール (WAF) などのセキュリティ機能を提供しており、アプリケーションのセキュリティを高めることができます。

これらの特徴とメリットにより、Azure App Service は、開発者や企業にとって、高速かつ柔軟な Web アプリケーションの開発とデプロイを支援するプラットフォームとして利用されています。

開発環境

  • 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 -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

この記事では基本的に Ubuntu のターミナルで操作を行います。Vim を使用してコピペする方法を初めて学ぶ人のために、以下の記事で手順を紹介しています。ぜひ挑戦してみてください。

Hello World を表示する手順

Spring Boot Web サービスの作成

こちらの関連記事の続きから手順を説明する記事となります。

Spring Boot アプリのプロジェクトフォルダに移動

プロジェクトフォルダに移動します。
※ ~/tmp/hello-spring-boot をプロジェクトフォルダとします。

$ cd ~/tmp/hello-spring-boot

Spring Boot アプリをビルド

Java アプリをビルドします。
※ target/app.jar が作成されます。

$ mvn clean package

Azure 環境にサインイン

こちらの関連記事の続きから手順を説明する記事となります。

Azure CLI でログインします。

$ az login

Azure 環境

リソースグループ

リソースグループを作成します。

Microsoft.Resources/resourceGroups
$ az group create \
    --name rg-hello \
    --location japaneast
説明を開きます。

このコマンドは、Azure CLI を使用して Azure のリソースグループを作成しています。

コマンド 内容
az group create Azure のリソースグループを作成するためのコマンドです。
オプション 内容
--name rg-hello リソースグループの名前を指定します。
--location japaneast リソースグループを作成する場所を japaneast に指定しています。 japaneast は Azure の日本東部地域を指します。

App Service プラン

App Service プランを作成します。

Microsoft.Web/serverfarms
$ az appservice plan create \
    --resource-group rg-hello \
    --name asp-hello \
    --sku Free \
    --is-linux
説明を開きます。

このコマンドは、App Service プランを作成しています。

コマンド 内容
az appservice plan create App Service プランを作成するためのコマンドです。
オプション 内容
--resource-group rg-hello App Service プランを作成するリソースグループの名前を指定します。
--name asp-hello App Service プランの名前を指定します。
--sku Free App Service プランのサービスレベルを指定します。この場合、Free プランを選択しています。
--is-linux -- App Service プランが Linux ベースのホストであるかどうかを指定します。

App Service プランを一覧表示してみます。

$ az appservice plan list

App Service プランを削除する場合場合は以下のコマンドです。

$ az appservice plan delete -n <plan> -g <group>

App Service Web アプリ

Web アプリの作成を行います。

Microsoft.Web/sites
$ az webapp create \
    --resource-group rg-hello \
    --plan asp-hello \
    --name app-hello-spring-boot \
    --runtime "JAVA:11-java11"
説明を開きます。

このコマンドは、App Service Web アプリを作成しています。

コマンド 内容
az webapp create App Service Web アプリを作成するためのコマンドです。
オプション 内容
--resource-group rg-hello App Service Web アプリを作成するリソースグループの名前を指定します。
--plan asp-hello App Service Web アプリを作成する、App Service プランの名前を指定します。
--name app-hello-spring-boot App Service Web アプリの名前を指定します。
--runtime "JAVA:11-java11" App Service Web アプリを Java 11 ランタイムで実行するように構成します。

Web アプリのスタートアップコマンドを設定します。

$ az webapp config set \
    --resource-group rg-hello \
    --name app-hello-spring-boot \
    --startup-file "java -jar /home/site/wwwroot/app.jar --server.port=80"
説明を開きます。

このコマンドは、App Service Web アプリの設定を変更しています。

コマンド 内容
az webapp config set App Service Web アプリの設定を変更するためのコマンドです。
オプション 内容
--resource-group rg-hello App Service Web アプリを含むリソースグループの名前を指定します。
--name app-hello-spring-boot App Service Web アプリの名前を指定します。
--startup-file "java -jar /home/site/wwwroot/app.jar --server.port=80" App Service Web アプリが80番ポートを使用して実行されるように設定します。

Web アプリのデプロイを行います。

$ az webapp deploy \
    --resource-group rg-hello \
    --name app-hello-spring-boot \
    --src-path target/app.jar \
    --type jar
説明を開きます。

このコマンドは、App Service Web アプリに新しいコードをデプロイします。

コマンド 内容
az webapp deploy App Service Web アプリに新しいコードをデプロイするためのコマンドです。
オプション 内容
--resource-group rg-hello App Service Web アプリを含むリソースグループの名前を指定します。
--name app-hello-spring-boot App Service Web アプリの名前を指定します。
--src-path target/app.jar App Service Web アプリにデプロイするコードの場所を指定します。
--type jar App Service Web アプリにデプロイするコードが JAR ファイルであることを指定します。

App Service Web アプリを削除する場合は以下のコマンドです。

$ az webapp delete -n <name> -g <group>

Web アプリ動作の確認

以下のコマンドを実行すると、WEBブラウザーが立ち上がります。

$ az webapp browse \
    --resource-group rg-hello \
    --name app-hello-spring-boot

立ち上がりに少し時間が掛かりますがページが表示されます。 しかし表示されたページ "/" にはコンテンツを設定していません。

WEBブラウザには以下のように表示されます。
image.png
改めてWEBブラウザで確認します。
※ URLは環境で読み替えて下さい。

https://app-hello-spring-boot.awesomewebsites.net/api/data

WEBブラウザに {"message":"Hello World!"} と表示され、JSON データを取得することが出来ました。

※ 別ターミナルから curl コマンドで確認してみます。

$ curl -v https://app-hello-spring-boot.awesomewebsites.net/api/data
詳細を表示します。
$ curl -v https://app-hello-spring-boot.awesomewebsites.net/api/data
*   Trying xx.xx.xx.xx:443...
* Connected to app-hello-spring-boot.awesomewebsites.net (xx.xx.xx.xx) 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=*.awesomewebsites.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.awesomewebsites.net" matched cert's "*.awesomewebsites.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 0x556a1919fe90)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> GET /api/data HTTP/2
> Host: app-hello-spring-boot.awesomewebsites.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: Wed, 08 Mar 2023 02:56:49 GMT
< set-cookie: ARRAffinity=f7e8112d174b035dedc0e95aa90dd578d821ba1ae7596ea4277e4837d982995e;Path=/;HttpOnly;Secure;Domain=app-hello-spring-boot.awesomewebsites.net
< set-cookie: ARRAffinitySameSite=f7e8112d174b035dedc0e95aa90dd578d821ba1ae7596ea4277e4837d982995e;Path=/;HttpOnly;SameSite=None;Secure;Domain=app-hello-spring-boot.awesomewebsites.net
<
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* Connection #0 to host app-hello-spring-boot.awesomewebsites.net left intact
{"message":"Hello World!"}

ターミナルに {"message":"Hello World!"} と表示され、JSON データを取得することが出来ました。

Azure App Service 自体が SSL/TLS 対応および HTTP/2 プロトコルをサポートしています。

SSH 接続

Web アプリに SSH で接続します。

$ az webapp ssh \
    --resource-group rg-hello \
    --name app-hello-spring-boot
説明を開きます。

このコマンドは、Azure App Service Web アプリに対して SSH 接続を確立します。

コマンド 内容
az webapp ssh App Service Web アプリに SSH 接続を確立するためのコマンドです。
オプション 内容
--resource-group rg-hello App Service Web アプリを含むリソースグループの名前を指定します。
--name app-hello-spring-boot-ssh App Service Web アプリの名前を指定します。

Web アプリに接続後にディレクトリを確認します。
※ Web アプリから出るときは ctrl + D を押します。

# pwd
/home
# ls -la
total 8
drwxrwxrwx    2 nobody   nobody        4096 Mar  9 08:11 .
drwxr-xr-x   59 root     root          4096 Mar  9 08:15 ..
drwxrwxrwx    2 nobody   nobody           0 Mar  9 08:11 ASP.NET
drwxrwxrwx    2 nobody   nobody           0 Mar  9 08:11 Data
drwxrwxrwx    2 nobody   nobody           0 Mar  9 08:11 DeploymentLogStream
drwxrwxrwx    2 nobody   nobody           0 Mar  9 08:13 LogFiles
drwxrwxrwx    2 nobody   nobody           0 Mar  9 08:11 b55a56dcdaabfb523bb54570
drwxrwxrwx    2 nobody   nobody           0 Mar  9 08:11 site

Web アプリの情報を表示してみます。

# cat /etc/*-release
3.14.6
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.14.6
PRETTY_NAME="Alpine Linux v3.14"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

この App Service Web アプリは Alpine Linux をベースに作成されていることが確認できました。

まとめ

Azure App Service 環境で、JAR ファイル形式の Spring Boot Web サービスを起動することができました。

MavenAzure CLI を使って、Spring Boot アプリの開発から、Azure 環境へのデプロイまで、すべてをターミナルから行うことができます。このように、クラウドでのシステム開発に必要なスキルや理解を深めることができます。初めての人でも簡単に手順を追うことができるので、ぜひ挑戦してみてください。

どうでしたか? 検証目的として、WSL Ubuntu で、Spring Boot Web アプリケーションを Azure App Service 環境で手軽に起動することができます。ぜひお試しください。今後も Azure の開発環境などを紹介していきますので、ぜひお楽しみにしてください。

推奨コンテンツ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0