演習 - Java Web アプリを Azure App Service にデプロイする

完了

このユニットでは、アプリケーションを Azure App Service にデプロイします。

Azure App Service とは

Azure App Service は、Tomcat を実行するためのサービスとしてのプラットフォーム (PaaS) として、Azure から提供されているものです。 Windows と Linux の環境、セキュリティ、負荷分散、自動スケーリング、DevOps 統合を備えています。 OS と Tomcat の管理は Azure に任せて、アプリケーションのビルドに専念できます。

Screenshot that shows the Azure portal screen.

JSF アプリケーションのサンプルを入手する

Java Web アプリケーションをデプロイするには、次に示すように GitHub から PrimeFaces JavaServer Faces (JSF) Web アプリケーションを入手できます。

git clone https://github.com/yoshioterada/Deploy-PrimeFaces-JSF-Web-App-on-Tomcat-9.0

複製すると、ディレクトリに次のファイルが表示されます。

Deploy-PrimeFaces-JSF-Web-App-on-Tomcat-9.0
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── microsoft
        │           └── azure
        │               └── samples
        │                   ├── controller
        │                   │   └── TodoListController.java
        │                   ├── dao
        │                   │   ├── ItemManagement.java
        │                   │   └── TodoItemManagementInMemory.java
        │                   └── model
        │                       └── TodoItem.java
        └── webapp
            ├── META-INF
            │   └── context.xml
            ├── WEB-INF
            │   ├── beans.xml
            │   ├── classes
            │   │   └── logging.properties
            │   ├── faces-config.xml
            │   └── web.xml
            └── index.xhtml

Azure App Service 用の Maven プラグイン

Java 開発者が Azure にアプリケーションを簡単にデプロイできるように、Microsoft は Maven Plugin for Azure App Service を用意しています。 このプラグインを使用すると、アプリケーションを簡単に構成して Azure にデプロイできます。 Maven Plugin for Azure App Service を使うには、次のコマンドを実行します。

Azure App Service 用の Maven プラグインを構成する

Maven Plugin for Azure App Service を構成するには、次のコマンドを実行します。

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.12.0:config

コマンドを実行すると、プロンプトにいくつかの質問が表示されるので、適切な項目を入力および選択して、それらを設定します。 次のオプションを入力します。

Item 入力値
サブスクリプション Azure サブスクリプションを選びます
OS の値を定義します 1: Linux
Define value for pricing tier (価格レベルの値を定義する) P1v2
Define value for Java Version (Java バージョンの値を定義する) 1:Java 8 または 2: Java 11
Define value for Runtime Stack (ランタイム スタックの値を定義する) 3:TOMCAT 9.0
確認 (Y/N) Y

このコマンドを実行すると、次の結果が表示されます。

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.12.0:config
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.microsoft.azure.samples:azure-javaweb-app >------------
[INFO] Building azure-javaweb-app Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.12.0:config (default-cli) @ azure-javaweb-app ---

Available subscriptions:
* 1: My Subscription (********-****-****-****-************)
Please choose a subscription [My Subscription]: [Enter]
[INFO] It may take a few minutes to load all Java Web Apps, please be patient.
[WARNING] There are no Java Web Apps in current subscription, please follow the following steps to create a new one.
Define value for OS [Linux]:
* 1: Linux
  2: Docker
  3: Windows
Enter your choice:
Define value for pricingTier [P1v2]:
   1: B1
   2: B2
   3: B3
   4: D1
   5: F1
*  6: P1v2
   7: P2v2
   8: P3v2
   9: S1
  10: S2
  11: S3
Define value for javaVersion [Java 8]:
* 1: Java 8
  2: Java 11
Enter your choice: 1
Define value for runtimeStack:
  1: Jbosseap 7.2
* 2: Tomcat 8.5
  3: Tomcat 9.0
Enter your choice: 3
Please confirm webapp properties
Subscription Id : f77aafe8-6be4-4d3d-bd9c-d0c37687ef70
AppName : azure-javaweb-app-1604982052600
ResourceGroup : azure-javaweb-app-1604982052600-rg
Region : westeurope
PricingTier : PremiumV2_P1v2
OS : Linux
Java : Java 8
Web server stack: Tomcat 9.0
Deploy to slot : false
Confirm (Y/N) [Y]: y
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  37.656 s
[INFO] Finished at: 2020-10-01T17:24:02+09:00
[INFO] ------------------------------------------------------------------------

pom.xml ファイルの <plugins> セクションに新しいセクションが表示されます。

リソース グループの名前、インスタンスの名前、デプロイの場所を変更する場合は、<resourceGroup><appName><region> を変更します。

    <plugins>
      <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.12.0</version>
        <configuration>
          <schemaVersion>V2</schemaVersion>
          <subscriptionId>********-****-****-****-************</subscriptionId>
          <resourceGroup>azure-javaweb-app</resourceGroup>
          <appName>azure-javaweb-app-1601463451101</appName>
          <pricingTier>P1v2</pricingTier>
          <region>japaneast</region>
          <runtime>
            <os>linux</os>
            <javaVersion>Java 8</javaVersion>
            <webContainer>TOMCAT 9.0</webContainer>
          </runtime>
          <deployment>
            <resources>
              <resource>
                <directory>${project.basedir}/target</directory>
                <includes>
                  <include>*.war</include>
                </includes>
              </resource>
            </resources>
          </deployment>
        </configuration>
      </plugin>
    </plugins>

コンパイルして Azure App Service にデプロイする

Azure App Service にデプロイするための設定が完了したので、ソース コードをもう一度コンパイルします。

mvn clean package

コンパイルしたら、Maven Plugin for Azure Web Apps コマンドを使ってアプリケーションをデプロイします。 次のコマンドを実行します。

mvn azure-webapp:deploy

デプロイが完了すると、次のメッセージが出力されます。

[INFO] Successfully deployed the artifact to https://azure-javaweb-app-1601463451101.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:15 min
[INFO] Finished at: 2020-11-19T15:55:55+09:00
[INFO] ------------------------------------------------------------------------

デプロイされたアプリケーションのパブリック URL が Successfully deployed the artifact to 行に表示されます。 次の例のように、ブラウザーを使って実際の URL にアクセスします。

https://azure-javaweb-app-1601463451101.azurewebsites.net

Screenshot that shows the deployed web app on Azure App Service.

コマンド ラインからログ ストリームを確認する

ログ ストリームにアクセスするには、次の CLI コマンドを実行します。

az webapp log tail -g azure-javaweb-app -n azure-javaweb-app-1601463451101

次の結果が表示されます。

Screenshot that shows the execution of the log stream.

演習の概要

このユニットでは、Java Web アプリケーションを作成してパッケージ化する方法、Maven Plugin for Azure Web Apps を使う方法、Azure App Service にアプリケーションをデプロイする方法について学習しました。 これらの手順は、JSF アプリケーションだけでなく、ほとんどの Java Web アプリケーションにも適用できます。