API 要求執行


本教學課程將說明用途,說明如何使用應用程式整合功能,為 ingress API 要求路由及擷取資訊。

總覽

在本教學課程中,您將建立整合項目,並使用 API 觸發事件接收擷取客戶資訊的 API 要求。視 API 要求位置而定,整合作業會從 MySQL 資料庫或 Oracle 資料庫擷取客戶詳細資料。

API 訂單履行圖 API 訂單履行圖

目標

本教學課程將說明如何在整合中完成下列工作:

  • 新增 API 觸發條件。
  • 新增及設定 MySQL 和 Oracle 連線的連接器工作。
  • 新增及設定資料對應工作,以便擷取及對應 API 酬載。

費用

在本教學課程中,您將使用下列 Google Cloud 的計費元件:

本教學課程的操作說明旨在將您的資源使用量保持在 Google Cloud 一律免費方案的限制範圍內。

完成本文件所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱「清除所用資源」。

事前準備

  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. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the following roles to the service account: Secret Manager Viewer (roles/secretmanager.viewer), Secret Manager Secret Accessor (roles/secretmanager).

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    6. Click Continue.
    7. Click Done to finish creating the service account.

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

    Go to project selector

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

  7. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the following roles to the service account: Secret Manager Viewer (roles/secretmanager.viewer), Secret Manager Secret Accessor (roles/secretmanager).

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    6. Click Continue.
    7. Click Done to finish creating the service account.

準備環境

設定 MySQL 資料庫

連線至 MySQL 伺服器,並建立本教學課程中要使用的資料庫和資料表。本教學課程使用含有 Customers 資料表的資料庫,其中包含下列資料列:

  +-------------+---------------+--------------------+------------------+
  | customer_id | customer_name | customer_emailID   | customer_city    |
  +-------------+---------------+--------------------+------------------+
  |           1 | Alex          | test-01@test.com   | New York         |
  |           2 | Dana          | test-02@test.com   | Seattle          |
  +-------------+---------------+--------------------+------------------+
  

如要瞭解如何建立及使用 MySQL 資料庫,請參閱 MySQL 說明文件

設定 Oracle 資料庫

連線至 Oracle 資料庫,並建立要用於本教學課程的資料表。本教學課程使用含有 Customers 資料表的資料庫,其中包含下列資料列:

  +-------------+---------------+--------------------+------------------+
  | customer_id | customer_name | customer_emailID   | customer_city    |
  +-------------+---------------+--------------------+------------------+
  |           1 | Amal          | test-01@test.com   | Delhi            |
  |           2 | Hao           | test-02@test.com   | Beijing          |
  +-------------+---------------+--------------------+------------------+
  

如要瞭解如何建立及使用 Oracle 資料庫,請參閱 Oracle 說明文件

設定連線

在本教學課程中,您必須在 Integration Connectors 中設定下列連線:

MySQL 連線
在 Integration Connectors 中設定 MySQL 連線,以便取得 Customer 資料表實體。如要瞭解如何設定 MySQL 連接器,請參閱「MySQL 連接器」。
Oracle 資料庫連線
在 Integration Connectors 中設定 Oracle DB 連線,以取得 Customer 表格實體。如要瞭解如何設定 MySQL 連接器,請參閱「Oracle DB 連接器」。

建立整合

如要建立新的整合,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Application Integration」頁面。

    前往「應用程式整合」

  2. 按一下左側導覽選單中的「整合」,開啟「整合」頁面。
  3. 按一下 「建立整合」,然後在「建立整合」頁面中提供下列詳細資料:
    1. 整合名稱:輸入 api_fulfilment
    2. 說明:輸入 Demo integration created for tutorial
    3. 區域:支援的區域清單中選取「us-central1」。
    4. 按一下「建立」開啟整合服務編輯器。

設定整合觸發條件

成功建立整合後,您可以新增及設定整合觸發事件,針對特定事件叫用整合。

在本教學課程中,我們會使用 API 觸發條件,針對每個新的 API 要求叫用整合功能。如要在 api_fulfilment 整合中新增及設定 API 觸發條件,請按照下列步驟操作:

  1. 在整合服務編輯器中,按一下「觸發條件」,即可顯示可用的觸發條件清單。
  2. 在整合服務編輯器中,按一下並放置「API trigger」元素。

    應用程式整合會自動填入觸發事件標籤、觸發事件名稱和觸發事件 ID。觸發事件 ID 是機器產生的 ID,格式為 api_trigger/TRIGGER_NAME,用於 API 要求。

  3. 您不需要為這個觸發條件進行進一步設定。

建立整合變數

整合變數類似於程式設計語言中使用的變數。在執行整合作業的過程中,您可以讀取及寫入變數中的資料。如要建立本教學課程所需的變數,請執行下列操作:

  1. 按一下設計工具導覽列中的 (切換面板),即可顯示「變數」窗格。
  2. 按一下「+ 建立」,然後建立下列整合變數:
    變數名稱 資料類型 變數類型
    location STRING 整合輸入內容
    customer_id STRING 整合作業輸入內容
    customer_record JSON 整合作業的輸出內容

從 MySQL 資料庫擷取顧客資料

如果 ingress API 要求來自 US 位置,請按照下列步驟從 MySQL 資料庫擷取顧客資料。

連線至 MySQL 資料庫

請按照下列步驟新增及設定 Connectors 工作,以便從 MySQL Customers 資料表擷取客戶詳細資料:

  1. 在整合服務編輯器中,按一下「Tasks」,即可顯示可用任務清單。
  2. 在整合服務編輯器中,按一下並放置「Connectors」元素。
  3. 在設計工具中選取「Connectors」任務元素,即可查看「Connectors」任務設定窗格
  4. 按一下 ,然後將「Connectors」標籤重新命名為 Fetch MySQL Data
  5. 按一下「設定連接器」
  6. 在「連接器工作編輯器」頁面中執行下列操作:
    1. 在「選取連線」下方,選擇 us-central1 區域。
    2. 從可用連線清單中選取 Salesforce 連線。
    3. 點選「下一步」
    4. 在「Type」下方,選擇「Entities」
    5. 在「設定實體/動作」下方,從可用實體清單中選取「客戶」,然後選取「取得」做為「操作」
    6. 按一下「完成」,完成連線設定並關閉窗格。

將客戶 ID 對應至 MySQL 連接器

資料對應工作會將客戶 ID 對應至 MySQL 連接器。如要在整合中新增及設定資料對應項目,請按照下列步驟操作:

  1. 在整合服務編輯器中,按一下「Tasks」,即可顯示可用任務清單。
  2. 在整合服務編輯器中按一下並放置「Data Mapping」(資料對應) 元素。
  3. 在設計工具中選取「Data Mapping」元素,即可查看任務設定窗格。
  4. 按一下 ,然後將「資料對應」工作重新命名為 Map to MySQL
  5. 按一下「Open Data Mapping Editor」

    資料對應編輯器可讓您使用可用的轉換函式,將輸入變數對應至所需的輸出變數。接著,輸出內容可做為任何其他整合作業或觸發事件的變數。如要進一步瞭解對應變數,請參閱「新增對應」。

  6. 將觸發條件 (customer_id) 整合變數對應至自動產生的連接器 (entityId(Fetch MySQL Data)) 輸入變數。

    對應完成後,請關閉窗格。系統會自動儲存所有變更。

  7. 對應至 MySQL 任務新增邊緣連線擷取 MySQL 資料連接器。

擷取 MySQL 客戶詳細資料

如要從 MySQL 連接器擷取客戶詳細資料,請執行下列操作:

  1. 在整合服務編輯器中,按一下「Tasks」,即可顯示可用任務清單。
  2. 在整合服務編輯器中按一下並放置「Data Mapping」(資料對應) 元素。
  3. 在設計工具中,按一下「Data Mapping」(資料對應) 元素,即可查看任務設定窗格。
  4. 按一下 ,然後將「資料對應」工作重新命名為 Get MySQL data
  5. 按一下「Open Data Mapping Editor」
  6. 將 MySQL connectorOutputPayload (Fetch MySQL Data) 輸出變數對應至 customer_record 整合變數。

    對應完成後,請關閉窗格。系統會自動儲存所有變更。

  7. 擷取 MySQL 資料連接器新增邊緣連線取得 MySQL 資料工作。

從 Oracle 資料庫擷取顧客資料

如果 ingress API 要求來自 APAC 位置,請按照下列步驟從 Oracle 資料庫擷取顧客資料。

連線至 Oracle 資料庫

請按照下列步驟新增及設定「Connector」作業,以便從 Oracle 的「Customers」資料表擷取客戶詳細資料:

  1. 在整合服務編輯器中,按一下「Tasks」,即可顯示可用任務清單。
  2. 在整合服務編輯器中,按一下並放置「Connectors」元素。
  3. 在設計工具中選取「Connectors」任務元素,即可查看「Connectors」任務設定窗格
  4. 按一下 ,然後將「Connectors」標籤重新命名為 Fetch Oracle Data
  5. 按一下「設定連接器」
  6. 在「連接器工作編輯器」頁面中執行下列操作:
    1. 在「選取連線」下方,選擇 us-central1 區域。
    2. 從可用連線清單中選取 Salesforce 連線。
    3. 點選「下一步」
    4. 在「Type」下方,選擇「Entities」
    5. 在「設定實體/動作」下方,從可用實體清單中選取「客戶」,然後選取「取得」做為「操作」
    6. 按一下「完成」,完成連線設定並關閉窗格。

將客戶 ID 對應至 Oracle 連接器

如要在整合中新增及設定資料對應項目,請按照下列步驟操作:

  1. 在整合服務編輯器中,按一下「Tasks」,即可顯示可用任務清單。
  2. 在整合服務編輯器中按一下並放置「Data Mapping」(資料對應) 元素。
  3. 在設計工具中選取「Data Mapping」元素,即可查看任務設定窗格。
  4. 按一下 ,然後將「資料對應」工作重新命名為 Map to Oracle
  5. 按一下「Open Data Mapping Editor」
  6. 將觸發條件 (customer_id) 整合變數對應至自動產生的連接器 (entityId(Fetch Oracle Data)) 輸入變數。

    對應完成後,請關閉窗格。系統會自動儲存所有變更。

  7. 對應至 Oracle 工作新增邊緣連線擷取 Oracle 資料連接器。

擷取 Oracle 客戶詳細資料

如要從 Oracle 連接器擷取客戶詳細資料,請執行下列操作:

  1. 在整合服務編輯器中,按一下「Tasks」,即可顯示可用任務清單。
  2. 在整合服務編輯器中按一下並放置「Data Mapping」(資料對應) 元素。
  3. 在設計工具中,按一下「Data Mapping」(資料對應) 元素,即可查看任務設定窗格。
  4. 按一下 ,然後將「資料對應」工作重新命名為 Get Oracle data
  5. 按一下「Open Data Mapping Editor」
  6. 將 Oracle 輸出變數 connectorOutputPayload (Fetch Oracle Data) 對應至 customer_record 整合變數。

    對應完成後,請關閉窗格。系統會自動儲存所有變更。

  7. 從「擷取 Oracle 資料」連接器新增邊緣連線至「取得 Oracle 資料」工作。

新增邊緣條件

邊緣條件可讓您指定整合項目必須符合哪些條件,才能將控制權傳遞至透過邊緣連結的任務。您已新增及設定必要的任務,現在可以新增邊緣條件來定義整合作業的流程。

下列邊緣條件會根據 location 整合變數控制整合流程:

  1. 使用下列邊緣條件,從 API 觸發條件新增至「對應至 MySQL」工作:
      $location$ = "US"
  2. 使用下列邊緣條件,從 API 觸發條件新增另一個邊緣連結至「Map to Oracle」工作:
      $location$ = "APAC"

整合流程範例

下圖顯示透過本教學課程建立的整合項目範例版面配置。

顯示整合流程的示例圖片 顯示整合流程的示例圖片

發布整合

如要發布整合項目,請在整合服務編輯器工具列中按一下「發布」

成功發布整合後,您可以查看及檢查已發布的整合項目的執行記錄。如要查看記錄,請按一下「查看這項整合的執行記錄」顯示執行記錄圖示的圖片。系統隨即會顯示「執行記錄」頁面。

測試整合

透過整合服務編輯器測試整合

如要透過整合編輯器測試整合,請執行下列步驟:

  1. 在整合服務編輯器的工具列中,按一下 「Test」

    「Test Integration」窗格隨即顯示。

  2. 系統會提示您輸入整合輸入變數的測試值。為了進行這項測試,請按照下列步驟操作:
    • customer_id:輸入 1
    • location:輸入 APAC
  3. 按一下「測試整合」

整合作業已成功執行,並顯示下列客戶值:

{ 
  "CUSTOMER_ID": 1,
  "CUSTOMER_NAME": "Amal"
  "CUSTOMER_EMAILID": "test-01@test.com "
  "CUSTOMER_CITY": "Delhi"
}

如要進一步瞭解測試,請參閱「測試及發布整合」。

使用 REST API 呼叫進行測試

整合功能已發布並執行,您可以使用下列 POST REST 呼叫來叫用:

網址:
https://integrations.googleapis.com/v1/projects/project-name/locations/-/integrations/api_fulfilment:execute 
要求主體:
{"trigger_id":"api_trigger/api_fulfilment_API_1"}

範例:使用自訂輸入內容提出 API 要求

curl -v -X POST -H "Content-Type: application/json" 'https://integrations.googleapis.com/v1/projects/connectors-ip-test/locations/us-central1/integrations/api-fulfillment-integration:execute' -H "Authorization: Bearer $(gcloud auth AUTH_PROFILE)" -d  '{ "triggerId": "api_trigger/api-fulfillment-integration_API_1", "inputParameters": { "customer_id": {"string_value": 1}, "location" : {"string_value" : "US"}} }'

API 會傳回整合執行回應,其中包含下列所有 JSON:

  { 
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "Dana"
    "CUSTOMER_EMAILID": "test-02@test.com "
    "CUSTOMER_CITY": "Seattle"
  }
  

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請取消發布整合,並刪除在整合連接器中建立的連結。

  • 如要取消發布這項整合,請在整合服務編輯器的工具列中按一下「取消發布」。詳情請參閱刪除整合
  • 如要瞭解如何刪除連線,請參閱「管理連接器」。