本页面适用于 Apigee 和 Apigee Hybrid。
查看 Apigee Edge 文档。
向 API 代理添加基于政策的功能是两步流程:
- 配置政策类型的实例。
- 将政策实例附加到流。
下图显示了政策与流之间的关系。如您所见,政策作为处理步骤附加到流中。如需为您的 API 配置所需行为,您需要对流有一定了解。(本主题在前面的配置流中有介绍。)
一种常用的政策是 SpikeArrest 政策。SpikeArrest 可防止可能会导致后端服务出现故障的消息流量突然增加。
将政策附加到流
新版代理编辑器
如需将政策附加到流,请执行以下操作:
- 选择 API 代理并导航到开发视图。
- 在左侧窗格中,点击+行中的 + 按钮。
在创建政策对话框中,点击选择政策类型字段,然后向下滚动到流量管理并选择 SpikeArrest。
(可选)您可以更改政策的显示名和名称。
完成后,点击创建以创建该政策。
- 现在您已经创建了 SpikeArrest 政策,接下来可以将其附加到 PreFlow 中的一个步骤。为此,请在左侧窗格中选择代理端点 > 默认 > PreFlow:
- 点击可视化编辑器右下角PreFlow窗格中 PreFlow 旁边的 + 按钮:
- 在添加政策步骤对话框中,选择 SpikeArrest 政策。
点击添加以附加政策。
SpikeArrest 政策现在显示在响应窗格中:
代理的以下 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:
- Select an API proxy and navigate to the Develop view.
- 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:
- In the Design view, click +Step associated with the request or response
flow to attach a policy. For example:
The Add Step dialog opens displaying a categorized list of policies.
- Scroll and select the policy type that you want to attach to the selected flow.
- 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.
-
-
- 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.
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).
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>
您可以通过在代码视图中直接编辑 XML 配置来配置政策。例如,Splik Arrest 政策的峰值消息速率最初设置为每秒 30 条消息。您可以通过更改政策的 XML 中的 <Rate
> 元素值来更改峰值速率。如需详细了解如何配置政策,请参阅政策参考。
您还可以将政策剪切并粘贴到代码视图中。这是重复使用 GitHub 中提供的示例的好方法。
当您在代码视图中更改政策定义时,更改会反映在属性检查器中。反过来也是如此:在属性检查器 中进行更改,更改会显示在代码视图的 XML 中。
如需查看本主题中所用配额政策的相关文档,请参阅配额政策。
将 Gemini Code Assist 代码说明与政策结合使用
如果您有 Gemini Code Assist,则可以使用它直接在 Cloud 控制台中的 Apigee 界面中了解政策和政策元素:
- 在代码视图中修改政策时,突出显示部分政策代码,例如 XML 元素或属性。您还可以突出显示多个元素。
- 点击 Gemini Code Assist 图标,如下所示。 侧边栏会显示有关政策和您所突出显示的元素的信息。