<?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/retries/</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/retries/index.xml" rel="self" type="application/rss+xml"/><item><title>重试弹性策略</title><link>https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/retries-overview/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/retries-overview/</guid><description>&lt;p>请求可能因瞬时错误而失败，例如遇到网络拥塞、重新路由到过载实例等。有时，请求也可能因已设置的其他弹性策略而失败，例如触发了定义的超时或熔断器策略。&lt;/p>
&lt;p>在这些情况下，配置 &lt;code>retries&lt;/code> 可以：&lt;/p>
&lt;ul>
&lt;li>将相同请求发送到不同的实例，或&lt;/li>
&lt;li>在条件清除后重试发送请求。&lt;/li>
&lt;/ul>
&lt;p>重试和超时协同工作，超时确保您的系统在需要时快速失败，而重试从临时故障中恢复。&lt;/p>
&lt;p>Dapr 提供&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/default-policies/">默认弹性策略&lt;/a>，您可以使用&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/override-default-retries/">用户定义的重试策略覆盖它们。&lt;/a>&lt;/p>


&lt;div class="alert alert-warning" role="alert">
&lt;h4 class="alert-heading">重要&lt;/h4>

 每个&lt;a href="https://docs.dapr.io/zh-hans/reference/components-reference/supported-pubsub/">发布订阅组件&lt;/a>都有自己的内置重试行为。显式应用 Dapr 弹性策略不会覆盖这些隐式重试策略。相反，弹性策略会增强内置重试，这可能导致消息重复聚集。

&lt;/div>

&lt;h2 id="重试策略格式">重试策略格式&lt;/h2>
&lt;p>&lt;strong>示例 1&lt;/strong>&lt;/p>
&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"># Retries are named templates for retry configurations and are instantiated for life of the operation.&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">retries&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">pubsubRetry&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">policy&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">constant&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">duration&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">maxRetries&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">10&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>&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">retryForever&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">policy&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">exponential&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">maxInterval&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">15s&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">maxRetries&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 style="color:#8f5902;font-style:italic"># Retry indefinitely&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>示例 2&lt;/strong>&lt;/p>
&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">retries&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">retry5xxOnly&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">policy&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">constant&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">duration&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">maxRetries&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">3&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">matching&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">httpStatusCodes&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:#4e9a06">&amp;#34;429,500-599&amp;#34;&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#8f5902;font-style:italic"># retry the HTTP status codes in this range. All others are not retried. &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">gRPCStatusCodes&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:#4e9a06">&amp;#34;1-4,8-11,13,14&amp;#34;&lt;/span>&lt;span style="color:#f8f8f8;text-decoration:underline"> &lt;/span>&lt;span style="color:#8f5902;font-style:italic"># retry gRPC status codes in these ranges and separate single codes.&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;p>以下重试选项是可配置的：&lt;/p></description></item><item><title>覆盖默认重试弹性策略</title><link>https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/override-default-retries/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/zh-hans/operations/resiliency/policies/retries/override-default-retries/</guid><description>&lt;p>Dapr 为任何不成功的请求（如故障和临时错误）提供&lt;a href="https://docs.dapr.io/zh-hans/operations/resiliency/policies/default-policies/">默认重试&lt;/a>。在弹性规范中，您可以通过使用保留的命名关键字定义策略来覆盖 Dapr 的默认重试逻辑。例如，定义一个名为 &lt;code>DaprBuiltInServiceRetries&lt;/code> 的策略可以覆盖通过服务间请求在边车之间发生故障时的默认重试。策略覆盖不会应用于特定目标。&lt;/p>
&lt;blockquote>
&lt;p>注意：虽然您可以使用更稳健的重试来覆盖默认值，但不能使用低于提供的默认值的值进行覆盖，也不能完全移除默认重试。这可以防止意外的停机。&lt;/p>&lt;/blockquote>
&lt;p>下表描述了 Dapr 的默认重试以及用于覆盖它们的策略关键字：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>功能&lt;/th>
 &lt;th>覆盖关键字&lt;/th>
 &lt;th>默认重试行为&lt;/th>
 &lt;th>描述&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>服务调用&lt;/td>
 &lt;td>DaprBuiltInServiceRetries&lt;/td>
 &lt;td>每次调用重试的退避间隔为 1 秒，最多重试 3 次。&lt;/td>
 &lt;td>边车对边车的请求（服务调用方法调用）失败并导致 gRPC 代码 &lt;code>Unavailable&lt;/code> 或 &lt;code>Unauthenticated&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Actors&lt;/td>
 &lt;td>DaprBuiltInActorRetries&lt;/td>
 &lt;td>每次调用重试的退避间隔为 1 秒，最多重试 3 次。&lt;/td>
 &lt;td>边车对边车的请求（Actor 方法调用）失败并导致 gRPC 代码 &lt;code>Unavailable&lt;/code> 或 &lt;code>Unauthenticated&lt;/code>&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Actor 提醒&lt;/td>
 &lt;td>DaprBuiltInActorReminderRetries&lt;/td>
 &lt;td>每次调用重试采用指数退避，初始间隔为 500ms，最大间隔为 60s，持续时间为 15 分钟&lt;/td>
 &lt;td>向状态存储持久化 Actor 提醒失败的请求&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>初始化重试&lt;/td>
 &lt;td>DaprBuiltInInitializationRetries&lt;/td>
 &lt;td>每次调用执行 3 次重试，采用指数退避，初始间隔为 500ms，持续时间为 10 秒&lt;/td>
 &lt;td>向应用程序发出请求以检索给定规范时的故障。例如，检索订阅、组件或弹性规范失败&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>下面的弹性规范示例展示了如何使用保留的命名关键字 &amp;lsquo;DaprBuiltInServiceRetries&amp;rsquo; 来覆盖_所有_服务调用请求的默认重试。&lt;/p>
&lt;p>同时还定义了一个名为 &amp;lsquo;retryForever&amp;rsquo; 的重试策略，该策略仅应用于 appB 目标。appB 使用 &amp;lsquo;retryForever&amp;rsquo; 重试策略，而所有其他应用程序服务调用重试故障则使用被覆盖的 &amp;lsquo;DaprBuiltInServiceRetries&amp;rsquo; 默认策略。&lt;/p></description></item></channel></rss>