透過使用者介面附加和設定政策

本頁適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

將政策式功能新增至 API Proxy 的程序分為兩個步驟:

  1. 設定政策類型的例項。
  2. 將政策執行個體附加至流程。

下圖顯示政策和流程之間的關係。如您所見,政策會以處理步驟的形式附加至流程。如要為 API 設定所需的行為,您需要稍微瞭解流程。(這個主題在「設定流程」一節中已提及)。

政策和流程之間的關係

SpikeArrest 政策是一種常用的政策。SpikeArrest 可防止訊息流量突然增加,進而淹沒後端服務。

將政策附加至流程

全新 Proxy 編輯器

如要將政策附加至流程,請按照下列步驟操作:

  1. 選取 API 代理程式,然後前往「Develop」檢視畫面。
  2. 在左側窗格中,按一下「政策」列中的「+」按鈕。
  3. 在「建立政策」對話方塊中,點選「選取政策類型」欄位,然後向下捲動至「流量管理」,然後選取「SpikeArrest」

  4. 您可以視需要變更政策的顯示名稱名稱

    建立政策對話方塊。

    完成後,按一下「建立」即可建立政策。

  5. 建立 尖峰流量防範政策後,您可以將該政策附加至 PreFlow 中的步驟。如要這樣做,請在左側窗格中依序選取「Proxy Endpoints」>「default」>「PreFlow」

    Proxy Explorer 中 PreFlow 選取的目標端點。

  6. 在視覺編輯器右下方的「回覆」窗格中,按一下「PreFlow」旁的「+」按鈕:

    按一下「回應」窗格中「PreFlow」旁的 + 按鈕。

  7. 在「新增政策步驟」對話方塊中,選取「尖峰流量防範」政策。
  8. 按一下「新增」,附加政策。

    「Response」窗格現在會顯示「尖峰流量防範」政策:

    回應窗格顯示的尖峰流量防範政策。

    文字編輯器會顯示下列代理程式 XML 程式碼:

    <ProxyEndpoint name="default">
      <PreFlow name="PreFlow">
        <Request/>
        <Response>
          <Step><
            <Name>SA-</Name>
          </Step>
        </Response>
      </PreFlow>
      <Flows/>
      <PostFlow name="PostFlow">
        <Request/>
        <Response/>
      </PostFlow>
      <HTTPProxyConnection>
        <BasePath>/myproxy</BasePath>
      </HTTPProxyConnection>
      <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
      </RouteRule>
    </ProxyEndpoint>
      

Detaching policies from a flow

To detach a policy from a flow, click the three-dot menu next to the flow and select Delete policy step

Classic Proxy Editor

To attach a policy to a flow:

  1. Select an API proxy and navigate to the Develop view.
  2. In the API Proxy Editor, select the flow to which you want to attach a policy in the Navigator pane.
    For example, the PreFlow flow is selected for the default API proxy endpoint in the following figure:
    Select flow to which you want to attach policy
  3. In the Design view, click +Step associated with the request or response flow to attach a policy. For example:
    Click +Step
    The Add Step dialog opens displaying a categorized list of policies.
    List of policies in categories
  4. Scroll and select the policy type that you want to attach to the selected flow.
  5. Modify the following fields.
    • Display Name: Unique display name for the policy. The UI will generate a default name, but it is advisable to create a descriptive name for the policy. This will ensure that other developers in your organization have an easy time understanding what the policy is intended to do.

    • Name: Unique name for the policy. The policy name matches the display name with the following exceptions:

      • Spaces are replaced by dashes.

      • Consecutive dashes are replaced by a single dash.

      • Except for dashes, underscores, and spaces (which are replaced, as noted above), characters that are not alphanumeric, such as pound signs, percent signs, ampersands, and so on, are removed.

  6. Click Add.

    The policy is attached to the selected flow.

After the policy is attached, you’ll see the policy displayed in the Designer view for the flow, in this example the PreFlow of the default proxy endpoint, and in the Policies list in the Navigation pane. The Code view, which displays the XML for the newly attached policy, displays below the Designer view. Apigee generates an instance of the policy that contains a set of reasonable default values.

Policies in the PreFlow in the Designer and Code view

Adding a policy without attaching it to a flow

To add a policy without attaching it to a flow, which is useful for Handling faults, click + in the Policies bar in the Navigator view to open the Add Policy dialog and add the policy (as described in steps 4 through 6 in the previous section).

Click + to add policy

A policy that is not attached to any flow is flagged with the detached icon in the Policies list, as shown adjacent to the API key message policy shown in the previous figure.

After you create the policy, you can attach it to a flow by dragging and dropping it from the Policies list to the desired flow in the Designer view.

Detaching policies from a flow

To detach a policy from a Flow, select the Flow. Mouse over the icon for the policy in the Designer view of the Flow. Click the X in the circle that appears in the icon.

Deleting a policy instance

To delete a policy instance, mouse over the entry for the policy in the Navigator view. Click the X in the circle that appears to the right of the entry.

Configuring policies in the UI

When the UI generates a policy instance, it applies reasonable default values for common settings. You may need to modify these settings to meet your requirements.

For example:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SpikeArrest async="false" continueOnError="false" enabled="true" name="spikearrest-1">
  <DisplayName>SpikeArrest-1</DisplayName>
  <FaultRules/>
  <Properties/>
  <Identifier ref="request.header.some-header-name"/>
  <MessageWeight ref="request.header.weight"/>
  <Rate>30ps</Rate>
</SpikeArrest>

您可以直接在「Code」檢視畫面中編輯政策的 XML 設定,舉例來說,尖峰封鎖政策的訊息峰值速率一開始會設為每秒 30 則訊息。您可以變更政策的 XML 中 <Rate> 元素值,來變更峰值費率。如要進一步瞭解如何設定政策,請參閱政策參考資料

您也可以將政策剪下並貼到「Code」檢視畫面。這是重複使用 GitHub 範例中政策的絕佳方法。

在「Code」檢視畫面中變更政策定義後,變更內容會顯示在「Property Inspector」中。反之亦然。在「Property Inspector」中進行變更,變更內容就會顯示在「Code」檢視畫面中的 XML 中。

如需本主題所用配額政策的相關說明文件,請參閱「配額政策」。

使用 Gemini Code Assist 程式碼說明與政策

如果您有 Gemini Code Assist,可以直接在 Cloud 控制台的 Apigee UI 中使用這項工具,瞭解政策和政策元素:

  • 在「Code」檢視畫面中編輯政策時,請醒目顯示部分政策程式碼,例如 XML 元素或屬性。你也可以標示多個元素。
  • 按一下 Gemini Code Assist 圖示,如下所示。 側邊面板會顯示政策和您醒目顯示的元素相關資訊。

Gemini Code Assist 程式碼說明