Java で Cloud Run functions の HTTP 関数を作成してデプロイする(第 1 世代)
このガイドでは、Java ランタイムを使用して Cloud Run functions の関数を記述するプロセスを説明します。Cloud Run functions には次の 2 つのタイプがあります。
- HTTP 関数。標準的な HTTP リクエストから呼び出します。
- イベント ドリブン関数。Pub/Sub トピックのメッセージや Cloud Storage バケットの変更など、Cloud インフラストラクチャのイベントを処理するために使用します。
このドキュメントでは、Maven または Gradle のいずれかを使用して、単純な HTTP 関数を作成し、ビルドする方法を説明します。
始める前に
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
- Google Cloud SDK をインストールして初期化します。
gcloud
コンポーネントを更新してインストールします。gcloud components update
- 開発環境を準備します。
関数コードで使用するため、ローカル システムにディレクトリを作成します。
Linux または Mac OS X:
mkdir ~/helloworld cd ~/helloworld
Windows:
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld
ソース ディレクトリとソースファイルを格納するプロジェクト構造を作成します。
mkdir -p src/main/java/functions touch src/main/java/functions/HelloWorld.java
HelloWorld.java
ファイルに次の内容を追加します。この例の関数は、「Hello, World!」という挨拶を出力します。
関数コードで使用するため、ローカル システムにディレクトリを作成します。
Linux または Mac OS X:
mkdir ~/helloworld-gradle cd ~/helloworld-gradle
Windows:
mkdir %HOMEPATH%\helloworld-gradle cd %HOMEPATH%\helloworld-gradle
ソース ディレクトリとソースファイルを格納するプロジェクト構造を作成します。
mkdir -p src/main/java/functions touch src/main/java/functions/HelloWorld.java
HelloWorld.java
ファイルに次の内容を追加します。この例の関数は、「Hello, World!」という挨拶を出力します。
関数がデプロイされたら、
httpsTrigger.url
プロパティをメモするか、次のコマンドを使用して検索します。 次のようになります。gcloud functions describe my-first-function
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/my-first-function
ブラウザで、この URL にアクセスします。
Hello World!
というメッセージが表示されます。
関数を作成する
このセクションでは、関数を作成する方法について説明します。
Maven
Gradle
依存関係を指定する
次に、依存関係を設定します。
Maven
上記で作成した helloworld
ディレクトリにディレクトリを変更し、pom.xml
ファイルを作成します。
cd ~/helloworld
touch pom.xml
Maven を使用して依存関係を管理するには、プロジェクトの pom.xml ファイル内にある <dependencies>
セクションで依存関係を指定します。この演習では、次の内容を pom.xml
ファイルにコピーします。
Maven に基づく完全なサンプルについては、helloworld をご覧ください。
Gradle
上記で作成した helloworld-gradle
ディレクトリにディレクトリを変更し、build.gradle
ファイルを作成します。
cd ~/helloworld-gradle
touch build.gradle
Gradle を使用して依存関係を管理するには、プロジェクトの build.gradle ファイル内で依存関係を指定します。この演習では、次の内容を build.gradle
ファイルにコピーします。この build.gradle
ファイルには、関数をローカルで実行するためのカスタムタスクが含まれています。
Gradle に基づく完全なサンプルについては、helloworld-gradle をご覧ください。
ローカルでビルドしてテストする
関数をデプロイする前に、ローカルでビルドしてテストできます。
Maven
次のコマンドを実行して、関数がビルドしたことを確認します。
mvn compile
もう 1 つのオプションは、mvn package
コマンドを使用して、Java コードをコンパイルし、テストを実行して、ターゲット ディレクトリ内の JAR ファイルにコードをパッケージ化するというものです。Maven ビルドのライフサイクルについて詳しくは、こちらをご覧ください。
関数をテストするには、次のコマンドを実行します。
mvn function:run
Gradle
次のコマンドを実行して、関数がビルドしたことを確認します。
gradle build
関数をテストするには、次のコマンドを実行します。
gradle runFunction -Prun.functionTarget=functions.HelloWorld
テストが正常に完了すると、ウェブブラウザでアクセスできる URL(http://localhost:8080/
)が表示され、動作中の関数を見ることができます。Hello World!
というメッセージが表示されます。
別のターミナル ウィンドウから curl
を使用して、この関数にリクエストを送信することもできます。
curl localhost:8080
# Output: Hello World!
関数をデプロイする
Maven
HTTP トリガーを使用して関数をデプロイするには、helloworld
ディレクトリで次のコマンドを実行します。
gcloud functions deploy my-first-function --no-gen2 --entry-point functions.HelloWorld --runtime java17 --trigger-http --memory 512MB --allow-unauthenticated
my-first-function
は関数が Google Cloud コンソールで識別される登録名で、--entry-point
は関数の完全修飾クラス名(FQN)を指定します。Gradle
HTTP トリガーを使用して関数をデプロイするには、helloworld-gradle
ディレクトリで次のコマンドを実行します。
gcloud functions deploy my-first-function --no-gen2 --entry-point functions.HelloWorld --runtime java17 --trigger-http --memory 512MB --allow-unauthenticated
my-first-function
は関数が Google Cloud コンソールで識別される登録名で、--entry-point
は関数の完全修飾クラス名(FQN)を指定します。デプロイされた関数をテストする
ログを表示する
Cloud Run functions のログは、Google Cloud CLI を使用して、また Cloud Logging UI で表示できます。
コマンドライン ツールを使用する
gcloud CLI を使用して関数のログを表示するには、logs read
コマンドの後に関数の名前を続けます。
gcloud functions logs read my-first-function
出力は次のようになります。
LEVEL NAME EXECUTION_ID TIME_UTC LOG D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.791 Function execution started D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.958 Function execution took 168 ms, finished with status code: 200 ...
Logging ダッシュボードを使用する
Google Cloud コンソールで Cloud Run functions のログを確認することもできます。