Latihan - Menyebarkan aplikasi web Java ke Azure App Service

Selesai

Di unit ini, Anda akan menggunakan aplikasi web ke Azure App Service.

Apa itu Azure App Service?

Azure menyediakan Azure App Service sebagai platform as a service (PaaS) untuk menjalankan Tomcat. Ini memiliki lingkungan Windows dan Linux, keamanan, penyeimbangan beban, penskalaan otomatis, dan integrasi Azure DevOps. Anda dapat meninggalkan os dan manajemen Tomcat ke Azure dan berkonsentrasi pada pembuatan aplikasi.

Screenshot that shows the Azure portal screen.

Dapatkan sampel aplikasi JSF

Untuk menyebarkan aplikasi web Java, Anda bisa mendapatkan aplikasi web PrimeFaces JavaServer Faces (JSF) dari GitHub seperti yang ditunjukkan di sini:

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

Setelah mengkloning, Anda akan melihat file berikut di direktori:

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

Maven Plugin untuk Azure App Service

Microsoft menyediakan Maven Plugin untuk Azure App Service untuk memudahkan pengembang Java menyebarkan aplikasi ke Azure. Dengan menggunakan plug-in ini, Anda dapat dengan mudah mengonfigurasi dan menyebarkan aplikasi Anda ke Azure. Jalankan perintah berikut untuk menggunakan Maven Plugin untuk Azure App Service.

Mengonfigurasi Maven Plugin untuk Azure App Service

Untuk mengonfigurasi Maven Plugin untuk Azure App Service, jalankan perintah berikut:

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

Setelah perintah, beberapa pertanyaan akan muncul di perintah, jadi masukkan dan pilih item yang sesuai dan atur. Lihat opsi berikut:

Item Nilai input
Langganan Memilih langganan Azure Anda
Tentukan nilai untuk OS 1: Linux
Tentukan nilai untuk tingkat harga P1v2
Tentukan nilai untuk versi Java 1: Java 8 atau 2: Java 11
Tentukan nilai untuk runtime bahasa umum stack 3: TOMCAT 9.0
Konfirmasi (Y/T) Y

Setelah Anda menjalankan perintah, hasil ini akan muncul:

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

Anda akan melihat bagian baru di bagian <plugins> di file Anda pom.xml.

Jika Anda ingin mengubah nama grup sumber daya, nama instans, dan lokasi penyebaran, ubah <resourceGroup>, <appName>, dan <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>

Kompilasikan dan sebarkan ke Azure App Service

Sekarang setelah pengaturan untuk menyebarkan ke Azure App Service selesai, kompilasi kode sumber lagi:

mvn clean package

Setelah dikompilasi, gunakan perintah Maven Plugin untuk Azure Web Apps untuk menyebarkan aplikasi Anda. Jalankan perintah berikut:

mvn azure-webapp:deploy

Ketika penyebaran selesai, pesan berikut akan menjadi output.

[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 publik aplikasi yang disebarkan ditampilkan di Successfully deployed the artifact to baris . Akses URL Anda dengan browser, seperti dalam contoh berikut:

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

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

Konfirmasikan aliran log dari baris perintah

Untuk mengakses aliran log, jalankan perintah CLI berikut:

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

Anda akan mendapatkan hasil berikut:

Screenshot that shows the execution of the log stream.

Ringkasan latihan

Di unit ini, Anda mempelajari cara membuat dan mengemas aplikasi web Java, cara menggunakan Maven Plugin untuk Azure Web Apps, dan cara menyebarkan aplikasi Anda ke Azure App Service. Langkah-langkah ini berlaku tidak hanya untuk aplikasi JSF tetapi juga sebagian besar aplikasi web Java.