設定開發環境

本頁說明如何設定開發環境,以使用 App Engine 標準環境適用的 Cloud Endpoints Frameworks 建構及部署後端 API。本頁會使用 Endpoints Frameworks 2.0 版基本架構程式碼範例,說明開始進行設定所需的基本軟體和元件。

建議您透過使用 Apache Maven 和 App Engine 外掛程式 (以 Google Cloud CLI 為基礎) 一文所述的 endpoints-skeleton-archetypehello-endpoints-archetype,建立新的 Endpoints Frameworks 2.0 版專案。

如要進一步瞭解使用 Endpoints Frameworks 部署範例 API 的所需步驟,請參閱開始在 App Engine 上使用 Endpoints Frameworks 的相關教學課程。

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. 記下 Google Cloud 專案 ID,以便在稍後使用。
  7. 安裝並設定所需的軟體

    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 範例中的程式碼範例開始編寫 API。

      • web.xml 是用來設定 servlet 的標準檔案。

      • appengine-web.xml 會定義使用 Java 8 將 API 部署到 App Engine 標準環境所需的資訊。

      • pom.xml 包含專案和設定資訊,供 Maven 用來建構專案及將 API 部署到 App Engine。

      • build.gradle 包含專案和設定資訊,供 Gradle 用來建構專案及將 API 部署到 App Engine。

    設定建構檔

    本節說明範例程式碼中的 Maven pom.xml 和 Gradle build.gradle 檔案。只要再輸入 Google Cloud 專案 ID 做為主機名稱,這些建構檔隨時可用來開始建立 API。

    關於基本設定

    本節將說明建構檔所需的基本設定。

    Maven

    基本依附元件

    pom.xml 需要以下內容所示的基本依附元件,才能建立 API:

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

    套用外掛程式

    系統會套用下列外掛程式來予以啟用:

    <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-pluginjava 外掛程式的延伸,可另外提供對組合網頁應用程式的支援。
    • 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 Frameworks 使用與 DNS 相容的名稱來識別特定服務。由於 Google Cloud 專案 ID 一定不重複,因此您應該使用 Google Cloud 專案 ID 做為 API 服務名稱的一部分。

    您必須在建構檔中新增 Google Cloud 專案 ID,才能為服務設定主機名稱。主機名稱應採以下格式:

    YOUR_PROJECT_ID.appspot.com

    在您將 API 部署至 App Engine 時,系統會自動建立名稱格式為 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 servlet

    Endpoints servlet 會處理傳入的要求,並將其轉送至在 App Engine 上執行的後端服務。您的 API 需要 Endpoints servlet 才能由 Cloud 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>

    後續步驟