開発環境のセットアップ

このページでは、App Engine スタンダード環境用の Cloud Endpoints Frameworks を使用してバックエンド API を構築およびデプロイする開発環境をセットアップする方法について説明します。このページでは、Endpoints Frameworks バージョン 2.0 スケルトンのコードサンプルを使用して、最初に必要となる基本的なソフトウェアとコンポーネントについて説明します。

Endpoints Frameworks バージョン 2.0 の新しいプロジェクトを作成するには、Apache Maven と App Engine プラグインの使用(Google Cloud CLI ベース)で説明されている endpoints-skeleton-archetype または hello-endpoints-archetype を使用できます。

Endpoints Frameworks を使用してサンプル API をデプロイするのに必要な手順について詳しく理解するには、App Engine での Endpoints Frameworks スタートガイドのチュートリアルをご覧ください。

始める前に

  1. Google Cloud アカウントにログインします。Google Cloud を初めて使用する場合は、アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud プロジェクトで課金が有効になっていることを確認します

  4. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  5. Google Cloud プロジェクトで課金が有効になっていることを確認します

  6. 後で必要になるので、Google Cloud プロジェクト ID はメモしておいてください。

必須ソフトウェアをインストールして構成する

  1. Java 8 がインストールされていない場合は、Oracle のサイトから Java Development Kit(JDK)をダウンロードしてインストールします。
  2. Maven または Gradle をインストールします。

    Maven

    1. Apache Maven バージョン 3.3.9 以降をダウンロードします
    2. Maven をインストールし、ローカル開発環境に合わせて構成します。

    Gradle

  3. Google Cloud CLI をダウンロードして初期化します
  4. 次のコマンドを実行します。
    1. gcloud CLI が、Google Cloud にある対象のデータとサービスにアクセスできるよう許可されていることを確認します。
      gcloud auth login
    2. 次のアプリケーションのデフォルト認証情報を使用します。
      gcloud auth application-default login
    3. Google Cloud SDK app-engine-java のコンポーネントをインストールします。
      gcloud components install app-engine-java
    4. Google Cloud SDK とすべてのコンポーネントを最新バージョンに更新します。
      gcloud components update
  5. App Engine アプリケーションを作成します。
    1. デフォルト プロジェクトを Google Cloud プロジェクト ID に設定します。
      gcloud config set project YOUR_PROJECT_ID

      YOUR_PROJECT_ID を Google Cloud プロジェクト ID に置き換えます。他にも Google Cloud プロジェクトがあり、gcloud を使用してそのプロジェクトを管理する場合は、gcloud CLI 構成の管理をご覧ください。

    2. App Engine アプリケーションを作成するリージョンを選択します。リージョンの一覧については、App Engine のロケーションをご覧ください。
    3. App Engine アプリケーションを作成します。YOUR_PROJECT_ID は実際の Google Cloud プロジェクト ID に、YOUR_REGION は App Engine アプリケーションを作成するリージョンに置き換えます。
        gcloud app create \
        --project=YOUR_PROJECT_ID \
        --region=YOUR_REGION
      

Endpoints Frameworks のスケルトン サンプルを入手する

Endpoints Frameworks バージョン 2.0 のスケルトン サンプルには、Maven と Gradle 向けの必要なビルド スクリプトが含まれています。また、初めて API を作成する際に必要なファイルも含まれています。

  1. ローカルマシンにサンプル リポジトリのクローンを作成します。

     git clone https://github.com/GoogleCloudPlatform/java-docs-samples
    
  2. Endpoints Frameworks スケルトン サンプルを含むディレクトリに移動します。

     cd java-docs-samples/appengine-java8/endpoints-v2-skeleton/
    

    サンプルのディレクトリ構造は以下のとおりです。

    Endpoints Frameworks スケルトン サンプルのレイアウト

    • MyApi.java には、API を作成する際に使用できる空のクラスが含まれています。作業開始にあたって役立つサンプルコードについては、echo のサンプルをご覧ください。

    • web.xmlサーブレットの構成に使用される標準ファイルです。

    • appengine-web.xml は、Java 8 を使用して App Engine スタンダード環境に API をデプロイする際に必要な情報を定義します。

    • pom.xml には、プロジェクトをビルドして API を App Engine にデプロイするために Maven が使用するプロジェクトと構成の情報が含まれています。

    • build.gradle には、プロジェクトをビルドして API を App Engine にデプロイするために Gradle が使用するプロジェクトと構成の情報が含まれています。

ビルドファイルを構成する

このセクションでは、サンプルコードに含まれている Maven の pom.xml ファイルと Gradle の build.gradle ファイルについて説明します。ビルドファイルを使用して、API の作成をすぐに開始できます。独自の Google Cloud プロジェクト ID を入力すると、それをホスト名で使用できるようになります。

最小構成について

このセクションでは、ビルドファイルに必要な最小構成について説明します。

Maven

最小の依存関係

作業開始にあたって pom.xml に必要な最小限の依存関係を以下に示します。

<dependencies>
  <!-- Compile/runtime dependencies -->
  <dependency>
    <groupId>com.google.endpoints</groupId>
    <artifactId>endpoints-framework</artifactId>
    <version>2.2.2</version>
  </dependency>
  <dependency>
    <groupId>com.google.appengine</groupId>
    <artifactId>appengine-api-1.0-sdk</artifactId>
    <version>2.0.23</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <type>jar</type>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
  </dependency>
</dependencies>

  • endpoints-framework 依存関係には、API を作成するために必要なクラスが含まれます。
  • <atrack-type="tasks" l10n-attrs-original-order="href,track-type,track-name,track-metadata-position" l10n-encrypted-href="8uk1g2YxVmQFExiHWQoDVLU3uURKQq2GHK/Q1ZkBJNo=" track-metadata-position="body" track-name="internalLink">App Engine Standard API SDK(appengine-api-sdk)は、プロジェクトを App Engine スタンダード環境にデプロイするために必要です。</atrack-type="tasks">
  • 標準の javax.servlet パッケージは、サーブレットをビルドするために必要となります。
  • 標準の javax.inject パッケージは、依存関係の挿入をサポートするために必要となります。

プラグインを適用する

以下のプラグインが適用され、有効になります。

<build>
  <!-- for hot reload of the web application-->
  <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>3.4.0</version>
      <configuration>
        <webResources>
          <resources>
            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
            <filtering>true</filtering>
            <targetPath>WEB-INF</targetPath>
          </resources>
        </webResources>
      </configuration>
    </plugin>
    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>appengine-maven-plugin</artifactId>
      <version>2.5.0</version>
      <configuration>
        <!-- deploy configuration -->
        <projectId>GCLOUD_CONFIG</projectId>
        <version>GCLOUD_CONFIG</version>
      </configuration>
    </plugin>
    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>endpoints-framework-maven-plugin</artifactId>
      <version>2.1.0</version>
      <configuration>
        <!-- plugin configuration -->
        <!--
        You must replace YOUR_PROJECT_ID with your
        Google Cloud Project Id
      -->
        <hostname>YOUR_PROJECT_ID.appspot.com</hostname>
      </configuration>
    </plugin>
  </plugins>
</build>
  • maven-war-plugin により java プラグインが拡張され、ウェブ アプリケーションの組み立てサポートが追加されます。
  • appengine-maven-plugin は、App Engine で API を実行するために必要となります。
  • endpoints-framework-maven-plugin は、Endpoints Frameworks プロジェクトをビルドするためのタスクと構成を提供します。

Gradle

プラグインの依存関係

API をビルドするために必要なプラグインを以下に示します。

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:2.1.0'
    classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.5.0'
  }
}

プラグインを適用する

以下のプラグインが適用され、Gradle ビルド スクリプトで有効になります。

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'com.google.cloud.tools.endpoints-framework-server'
apply plugin: 'com.google.cloud.tools.appengine'

  • java プラグインは、Java 固有のコンパイルおよびビルドステップをプロジェクトに追加します。
  • war プラグインにより java プラグインが拡張され、ウェブ アプリケーションの組み立てサポートが追加されます。
  • endpoints-framework-server プラグインは、Endpoints Frameworks Gradle プラグインにサーバー側のサポートを提供します。
  • appengine プラグインは、App Engine で API を実行するために必要となります。

プロジェクトの依存関係

プロジェクトでは以下の依存関係が使用されます。

dependencies {
    compile 'com.google.endpoints:endpoints-framework:2.2.2'
    compile 'com.google.appengine:appengine-api-1.0-sdk:2.0.23'

    compile 'javax.inject:javax.inject:1'
    compileOnly 'javax.servlet:javax.servlet-api:3.1.0'
}

  • endpoints-framework 依存関係には、API を作成するために必要なクラスが含まれます。
  • App Engine Standard API SDKappengine-api-sdk)は、プロジェクトを App Engine スタンダード環境にデプロイするために必要となります。
  • 標準の javax.servlet パッケージは、サーブレットをビルドするために必要となります。
  • 標準の javax.inject パッケージは、依存関係の挿入をサポートするために必要となります。

サービスのホスト名を定義する

Endpoints Frameworks は DNS と互換性のある名前を使用してサービスを一意に識別します。Google Cloud プロジェクト ID はグローバルに一意であることが保証されているので、API のサービス名の一部として Google Cloud プロジェクト ID を使用してください。

サービスのホスト名を構成するには Google Cloud プロジェクト ID をビルドファイルに追加する必要があります。ホスト名は次の形式にします。

YOUR_PROJECT_ID.appspot.com

App Engine に API をデプロイすると、YOUR_PROJECT_ID.appspot.com という形式の名前を持つ DNS エントリが自動的に作成されます。ホスト名は、Cloud Endpoints サービスの名前であり、API へのリクエスト送信に使用するドメイン名でもあります。

Maven

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>endpoints-framework-maven-plugin</artifactId>
  <version>2.1.0</version>
  <configuration>
    <!-- plugin configuration -->
    <!--
    You must replace YOUR_PROJECT_ID with your
    Google Cloud Project Id
  -->
    <hostname>YOUR_PROJECT_ID.appspot.com</hostname>
  </configuration>
</plugin>

hostname フィールドで、YOUR_PROJECT_ID を実際の Google Cloud プロジェクト ID に置き換えます。

Gradle

// You must replace YOUR_PROJECT_ID with your Google Cloud Project Id
def projectId = 'YOUR_PROJECT_ID'

endpointsServer {
  // Endpoints Framework Plugin server-side configuration
  hostname = "${projectId}.appspot.com"
}

変数 projectID に実際の Google Cloud プロジェクト ID を設定します。例: def projectId = 'example-project-12345'

Endpoints サーブレットの構成

Endpoints サーブレットは受信リクエストを処理し、App Engine で実行されているバックエンド サービスにこれらのリクエストを転送します。API を Endpoints で管理するには、Endpoints サーブレットが必要です。

web.xml の詳細については、デプロイ記述子: web.xml をご覧ください。

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!-- Wrap the backend with Endpoints Frameworks v2. -->
    <servlet>
        <servlet-name>EndpointsServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value>com.example.skeleton.MyApi</param-value>
        </init-param>
    </servlet>
    <!-- Route API method requests to the backend. -->
    <servlet-mapping>
        <servlet-name>EndpointsServlet</servlet-name>
        <url-pattern>/_ah/api/*</url-pattern>
    </servlet-mapping>
</web-app>

App Engine のデプロイの構成

appengine-web.xml ファイルは、API のデプロイ時に App Engine スタンダード環境の構成を定義するために使用されます。詳細については、appengine-web.xml リファレンスをご覧ください。

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <runtime>java8</runtime>
    <threadsafe>true</threadsafe>

    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
    </system-properties>
</appengine-web-app>

次のステップ