LoginSignup
4
1

More than 5 years have passed since last update.

Azure FunctionsをJava + VS Codeで試してみた(Maven for Java編)

Last updated at Posted at 2018-09-04

最近勉強中のJavaをAzure Functionsで動かしてみました。
できるだけVS Code拡張機能のMaven for Javaを使っています。
とりあえずサンプルが動くところまで。
Azure FunctionsのJavaは現時点ではまだプレビューですね。

動作環境

  • Windows 7
  • Visual Studio Code 1.24.1
    • 拡張機能:Java Extension Pack 0.3.0
  • JDK 1.8
  • Apache Maven 3.5.3
  • Azure CLI 2.0.32
  • .Net Core SDK 2.1
  • Node.js 7.2.1
  • npm 4.0.5
  • Azure Functions Core Tools 2.0.1-beta.35

事前インストール

Mavenプロジェクト作成

  • VS Codeで「Ctrl + Shift + P」を押しコマンドパレットを表示します。
  • [Maven: Generate from Maven Archetype]を選択します。
    image.png

  • 空フォルダを作成し、[Select Destination Folder]を選択します。
    image.png

  • しばらくすると以下のような画面になるので、[More ...]を選択します。
    image.png

  • コマンドパレットに[functions]と入力すると、[azure-functions-archetype]が表示されるので選択します。
    image.png

  • VS Codeのターミナルウィンドウで、以下内容を聞かれるのでそれぞれ入力していきます。

Define value for property 'groupId': [任意のGroupIDを入力する]
Define value for property 'artifactId': [任意のArtifactIDを入力する]
Define value for property 'version' 1.0-SNAPSHOT: : [そのままEnter]
Define value for property 'package' testgrp: : [そのままEnter]
Define value for property 'appName' testid-yyyymmddxxxxxxx: : [任意のアプリケーション名を入力する。これがFunction Appの名称になります]
Define value for property 'appRegion' westus: : [japaneast]
Define value for property 'resourceGroup' java-functions-group: : [任意のリソースグループ名]
確認メッセージが出るのでYを押す
  • 先程指定したフォルダをVS Codeで開きます。
  • MAVEN PROJECTSより、該当フォルダを選択して、[右クリック]→[clean]を選択します。
    image.png

  • MAVEN PROJECTSより、該当フォルダを選択して、[右クリック]→[package]を選択します。
    image.png

ローカル実行

  • [Ctrl + Shift + P]でコマンドパレットを開き、[Maven: Execute commands]を選択します。
    image.png

  • 対象プロジェクトのartifactIdを選択します。
    image.png

  • [Custom goals ...]を選択します。
    image.png

  • [azure-functions:run]と入力し、Enterを押します。
    image.png

  • functionsのロゴが出てローカル起動されます。
    image.png

  • ローカルのURLが発行されます。
    image.png

  • とりあえずテストしてみましょう。ブラウザで以下URLで実行してみます。

http://localhost:7071/api/HttpTrigger-Java?name=Azure
  • 問題なく実行できてますね!
    image.png

Functionsデプロイ

  • ではFunctionsにデプロイしてみます。[Ctrl + Shift + P]でコマンドパレットを開き、[Maven: Execute commands]を選択します。
    image.png

  • 対象プロジェクトのartifactIdを選択します。
    image.png

  • [Custom goals ...]を選択します。
    image.png

  • [azure-functions:deploy]と入力し、Enterを押します。
    image.png

  • Azure PortalでFunction Appを見てみると新規作成されていることが分かります。
    image.png

  • ではテストしてみましょう。ブラウザで以下URLで実行してみます。[Your AppName]には、functinos作成時に指定した値を入力してください。

https://[Your AppName].azurewebsites.net/api/HttpTrigger-Java?name=Azure
  • うまくいきました!(少し時間が掛かります)
    image.png

まとめ

とりあえずサンプルプログラムを動作させてみただけですが、Functions楽しい!
スワップとかCI/CDとか運用どうするのとか気になること山盛りなのでこれから徐々に深掘りしていきたいと思います。

参考にしたサイト

Java と Maven を使用して初めての関数を作成する (プレビュー)
javaでAzure functions

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