LoginSignup
2
1

More than 1 year has passed since last update.

Vert.xのWebアプリケーションをAzure App Serviceで動かす

Last updated at Posted at 2021-05-01

これまで、いくつかのJava製WebフレームワークをAzure上で動かしてきました。
- Spring Boot on Azure
- Helidon on Azure
- Quarkus on Azure
今日はVert.xを試してみます。

プロジェクトはVert.x Starterで作りました。

Dependenciesは何も選択しませんでした。
スクリーンショット 2021-05-01 21.57.56.png

ローカル環境での実行

HelidonQuarkusを試したときと同じ環境を使いました。

Starterを解凍するとMainVerticle.javaが入っています。コードはそのまま使います。

package com.example.starter;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }).listen(8888, http -> {
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
  }
}

ビルドします。

mvn clean package

実行します。

java -jar target/*.jar

localhostにアクセスして以下表示になればOKです。

スクリーンショット 2021-05-01 22.05.18.png

Azureでの実行

続いてAzureにデプロイするための準備です。最初にAzure CLIでログインしておきます。

az login

次にpom.xmlのbuildセクションに以下を追加します。

<plugin>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-webapp-maven-plugin</artifactId>
    <version>1.14.0</version>
</plugin>

以下コマンドを実行します。

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

対話式に構成を聞かれるので、linux、Java 8を選びました。

続いて手動でappSettingsを追加します。ポートはVert.xのコードにある通り8888にしました。

<plugins> 
  <plugin> 
    <groupId>com.microsoft.azure</groupId>  
    <artifactId>azure-webapp-maven-plugin</artifactId>  
    <version>1.14.0</version>  
    <configuration> 
      <schemaVersion>v2</schemaVersion>  
      <subscriptionId>xxxxx</subscriptionId>  
      <resourceGroup>demo-xxxxxxxxxxx-rg</resourceGroup>  
      <appName>demo-xxxxxxxxxx</appName>  
      <pricingTier>P1v2</pricingTier>  
      <region>westeurope</region>  
      <runtime> 
        <os>Linux</os>  
        <javaVersion>Java 8</javaVersion>  
        <webContainer>Java SE</webContainer> 
      </runtime>  
      <appSettings> 
        <property> 
          <name>PORT</name>  
          <value>8888</value> 
        </property>  
        <property> 
          <name>WEBSITES_PORT</name>  
          <value>8888</value> 
        </property>  
        <property> 
          <name>WEBSITES_CONTAINER_START_TIME_LIMIT</name>  
          <value>900</value> 
        </property> 
      </appSettings>  
      <deployment> 
        <resources> 
          <resource> 
            <directory>${project.basedir}/target</directory>  
            <includes> 
              <include>*.jar</include> 
            </includes> 
          </resource> 
        </resources> 
      </deployment> 
    </configuration> 
  </plugin> 
</plugins> 

ビルドしてデプロイします。

mvn clean package
mvn azure-webapp:deploy

問題なく表示されました。この感じだとどんなWebフレームワークでもAzure App Service上で動きそうです。

スクリーンショット 2021-05-01 20.04.05.png

2
1
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
2
1