<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>弹性策略 on Dapr 文档库</title><link>https://docs.dapr.io/zh-hans/operations/resiliency/policies/</link><description>Recent content in 弹性策略 on Dapr 文档库</description><generator>Hugo</generator><language>zh-hans</language><atom:link href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/index.xml" rel="self" type="application/rss+xml"/><item><title>超时弹性策略</title><link>https://docs.dapr.io/zh-hans/operations/resiliency/policies/timeouts/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/zh-hans/operations/resiliency/policies/timeouts/</guid><description>&lt;p>网络调用可能因多种原因失败，导致应用程序无限期等待响应。通过设置超时时长，您可以切断那些无响应的服务，释放资源以处理新请求。&lt;/p>
&lt;p>超时是可选策略，可用于提前终止长时间运行的操作。设置一个现实的超时时长，反映生产环境中的实际响应时间。如果超出了超时时长：&lt;/p>
&lt;ul>
&lt;li>正在进行的操作将被终止（如果可能）。&lt;/li>
&lt;li>返回错误。&lt;/li>
&lt;/ul>
&lt;h2 id="超时策略格式">超时策略格式&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#204a87;font-weight:bold">spec&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">policies&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#8f5902;font-style:italic"># 超时是指定的时长。&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">timeouts&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">timeoutName&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">timeout1&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">general&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">5s&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">important&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">60s&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">largeResponse&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">10s&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="规范元数据">规范元数据&lt;/h3>
&lt;p>| 字段 | 详情 | 示例 |
| timeoutName | 超时策略的名称 | &lt;code>timeout1&lt;/code> |
| general | 标记为&amp;quot;general&amp;quot;的超时时长。使用 Go 的 &lt;a href="https://pkg.go.dev/time#ParseDuration">time.ParseDuration&lt;/a> 格式。没有设置最大值。 | &lt;code>15s&lt;/code>、&lt;code>2m&lt;/code>、&lt;code>1h30m&lt;/code> |
| important | 标记为&amp;quot;important&amp;quot;的超时时长。使用 Go 的 &lt;a href="https://pkg.go.dev/time#ParseDuration">time.ParseDuration&lt;/a> 格式。没有设置最大值。 | &lt;code>15s&lt;/code>、&lt;code>2m&lt;/code>、&lt;code>1h30m&lt;/code> |
| largeResponse | 等待大型响应的超时时长。使用 Go 的 &lt;a href="https://pkg.go.dev/time#ParseDuration">time.ParseDuration&lt;/a> 格式。没有设置最大值。 | &lt;code>15s&lt;/code>、&lt;code>2m&lt;/code>、&lt;code>1h30m&lt;/code> |&lt;/p>
&lt;blockquote>
&lt;p>如果未指定超时值，策略不会强制执行时间，默认为您根据请求客户端设置的值。&lt;/p></description></item><item><title>断路器弹性策略</title><link>https://docs.dapr.io/zh-hans/operations/resiliency/policies/circuit-breakers/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/zh-hans/operations/resiliency/policies/circuit-breakers/</guid><description>&lt;p>当其他应用程序/服务/组件出现较高的故障率时，会使用断路器策略。断路器通过监控请求并在满足特定条件时切断到受影响服务的所有流量来减少负载。&lt;/p>
&lt;p>在一定数量的请求失败后，断路器会&amp;quot;跳闸&amp;quot;或打开以防止级联故障。通过这样做，断路器为服务提供从故障中恢复的时间，而不是用事件淹没它。&lt;/p>
&lt;p>断路器还可以进入&amp;quot;半开&amp;quot;状态，允许部分流量通过以查看系统是否已恢复。&lt;/p>
&lt;p>一旦请求恢复成功，断路器将进入&amp;quot;关闭&amp;quot;状态，并允许流量完全恢复。&lt;/p>
&lt;h2 id="断路器策略格式">断路器策略格式&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#204a87;font-weight:bold">spec&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">policies&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">circuitBreakers&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">pubsubCB&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">maxRequests&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#0000cf;font-weight:bold">1&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">interval&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">8s&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">timeout&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">45s&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#204a87;font-weight:bold">trip&lt;/span>&lt;span style="color:#000;font-weight:bold">:&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#000">consecutiveFailures &amp;gt; 8&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="规格元数据">规格元数据&lt;/h2>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>重试选项&lt;/th>
 &lt;th>描述&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>&lt;code>maxRequests&lt;/code>&lt;/td>
 &lt;td>断路器处于半开状态（从故障中恢复）时允许通过的最大请求数。默认为 &lt;code>1&lt;/code>。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>interval&lt;/code>&lt;/td>
 &lt;td>断路器用于清除其内部计数的循环时间周期。如果设置为 0 秒，则永远不会清除。默认为 &lt;code>0s&lt;/code>。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>timeout&lt;/code>&lt;/td>
 &lt;td>打开状态的持续时间（直接在故障之后），直到断路器切换到半开状态。默认为 &lt;code>60s&lt;/code>。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>trip&lt;/code>&lt;/td>
 &lt;td>由断路器评估的 &lt;a href="https://github.com/google/cel-spec">Common Expression Language (CEL)&lt;/a> 表达式。当表达式评估为 true 时，断路器跳闸并变为打开状态。默认为 &lt;code>consecutiveFailures &amp;gt; 5&lt;/code>。其他可能的值是 &lt;code>requests&lt;/code> 和 &lt;code>totalFailures&lt;/code>，其中 &lt;code>requests&lt;/code> 表示电路打开之前成功或失败调用的数量，&lt;code>totalFailures&lt;/code> 表示电路打开之前失败尝试的总数（不一定是连续的）。示例：&lt;code>requests &amp;gt; 5&lt;/code> 和 &lt;code>totalFailures &amp;gt;3&lt;/code>。&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h2 id="后续步骤">后续步骤&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/default-policies/">了解默认弹性策略的更多信息&lt;/a>&lt;/li>
&lt;li>了解更多：
&lt;ul>
&lt;li>&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/retries-overview/">重试策略&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/timeouts/">超时策略&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="相关链接">相关链接&lt;/h2>
&lt;p>尝试以下弹性快速入门之一：&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://docs.dapr.io/zh-hans/getting-started/quickstarts/resiliency/resiliency-serviceinvo-quickstart/">弹性：服务到服务&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://docs.dapr.io/zh-hans/getting-started/quickstarts/resiliency/resiliency-state-quickstart/">弹性：状态管理&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>默认弹性策略</title><link>https://docs.dapr.io/zh-hans/operations/resiliency/policies/default-policies/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/zh-hans/operations/resiliency/policies/default-policies/</guid><description>&lt;p>在弹性中，您可以设置具有广泛范围的默认策略。这是通过保留关键字完成的，让 Dapr 知道何时应用该策略。有 3 种默认策略类型：&lt;/p>
&lt;ul>
&lt;li>&lt;code>DefaultRetryPolicy&lt;/code>&lt;/li>
&lt;li>&lt;code>DefaultTimeoutPolicy&lt;/code>&lt;/li>
&lt;li>&lt;code>DefaultCircuitBreakerPolicy&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>如果定义了这些策略，它们将用于对服务、应用程序或组件的每个操作。它们还可以通过附加额外关键字来修改以更加具体。具体策略遵循以下模式，&lt;code>Default%sRetryPolicy&lt;/code>、&lt;code>Default%sTimeoutPolicy&lt;/code> 和 &lt;code>Default%sCircuitBreakerPolicy&lt;/code>。其中 &lt;code>%s&lt;/code> 被替换为策略的目标。&lt;/p>
&lt;p>下表列出了所有可能的默认策略关键字以及它们如何转换为策略名称。&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>关键字&lt;/th>
 &lt;th>目标操作&lt;/th>
 &lt;th>示例策略名称&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>&lt;code>App&lt;/code>&lt;/td>
 &lt;td>服务调用。&lt;/td>
 &lt;td>&lt;code>DefaultAppRetryPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>Actor&lt;/code>&lt;/td>
 &lt;td>Actor 调用。&lt;/td>
 &lt;td>&lt;code>DefaultActorTimeoutPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>Component&lt;/code>&lt;/td>
 &lt;td>所有组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultComponentCircuitBreakerPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>ComponentInbound&lt;/code>&lt;/td>
 &lt;td>所有入站组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultComponentInboundRetryPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>ComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有出站组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultComponentOutboundTimeoutPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>StatestoreComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有状态存储组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultStatestoreComponentOutboundCircuitBreakerPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>PubsubComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有出站发布订阅（发布）组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultPubsubComponentOutboundRetryPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>PubsubComponentInbound&lt;/code>&lt;/td>
 &lt;td>所有入站发布订阅（订阅）组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultPubsubComponentInboundTimeoutPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>BindingComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有出站绑定（调用）组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultBindingComponentOutboundCircuitBreakerPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>BindingComponentInbound&lt;/code>&lt;/td>
 &lt;td>所有入站绑定（读取）组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultBindingComponentInboundRetryPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>SecretstoreComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有密钥存储组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultSecretstoreComponentTimeoutPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>ConfigurationComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有配置组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultConfigurationComponentOutboundCircuitBreakerPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;code>LockComponentOutbound&lt;/code>&lt;/td>
 &lt;td>所有锁组件操作。&lt;/td>
 &lt;td>&lt;code>DefaultLockComponentOutboundRetryPolicy&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h2 id="策略层次解析">策略层次解析&lt;/h2>
&lt;p>如果正在执行的操作匹配策略类型，并且没有针对它的更具体的策略，则会应用默认策略。对于每个目标类型（app、actor 和 component），优先级最高的策略是命名策略，即专门针对该构造的策略。&lt;/p>
&lt;p>如果不存在，则策略从最具体到最广泛应用。&lt;/p>
&lt;h2 id="默认策略与内置重试如何协同工作">默认策略与内置重试如何协同工作&lt;/h2>
&lt;p>在&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/override-default-retries/">内置重试&lt;/a>的情况下，默认策略不会阻止内置重试策略运行。两者一起使用，但仅在特定情况下。&lt;/p>
&lt;p>对于服务和 Actor 调用，内置重试专门处理连接到远程边车时的问题（在需要时）。由于这些对 Dapr 运行时的稳定性很重要，因此它们不会被禁用，&lt;strong>除非&lt;/strong>为操作专门引用了命名策略。在某些情况下，可能会同时从内置重试和默认重试策略进行额外重试，但这可以防止过于薄弱的默认策略降低边车的可用性/成功率。&lt;/p>
&lt;p>应用程序的策略解析层次，从最具体到最广泛：&lt;/p></description></item></channel></rss>