快速入门:服务到组件的弹性

通过状态管理API开始使用Dapr的弹性能力

通过模拟系统故障来观察 Dapr 的弹性能力。 在本快速入门中,您将:

  • 执行一个微服务应用程序,通过 Dapr 的状态管理 API 持续地存储和检索状态。
  • 通过模拟系统故障来触发弹性策略。
  • 解决故障,微服务应用程序将恢复。

Diagram showing the resiliency applied to Dapr APIs

在继续快速入门之前,请选择您首选的特定语言 Dapr SDK。

先决条件

对于此示例,您将需要:

第1步:设置环境

克隆在Quickstarts存储库中提供的示例

  1. git clone https://github.com/dapr/quickstarts.git

在一个终端窗口中,导航到 order-processor 目录。

  1. cd ../state_management/python/sdk/order-processor

安装依赖项:

  1. pip3 install -r requirements.txt

步骤 2:运行应用程序

与 Dapr sidecar 一起运行 order-processor 服务。 然后 Dapr sidecar 加载位于资源目录中的弹性规范:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Resiliency
  3. metadata:
  4. name: myresiliency
  5. scopes:
  6. - checkout
  7. spec:
  8. policies:
  9. retries:
  10. retryForever:
  11. policy: constant
  12. maxInterval: 5s
  13. maxRetries: -1
  14. circuitBreakers:
  15. simpleCB:
  16. maxRequests: 1
  17. timeout: 5s
  18. trip: consecutiveFailures >= 5
  19. targets:
  20. apps:
  21. order-processor:
  22. retry: retryForever
  23. circuitBreaker: simpleCB
  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- python3

应用程序启动后,order-processor服务将orderId键/值对写入和读取到statestore Redis实例 在statestore.yaml组件中定义

  1. == APP == Saving Order: { orderId: '1' }
  2. == APP == Getting Order: { orderId: '1' }
  3. == APP == Saving Order: { orderId: '2' }
  4. == APP == Getting Order: { orderId: '2' }
  5. == APP == Saving Order: { orderId: '3' }
  6. == APP == Getting Order: { orderId: '3' }
  7. == APP == Saving Order: { orderId: '4' }
  8. == APP == Getting Order: { orderId: '4' }

步骤 3:引入故障

在执行 dapr init 时,通过停止在开发机器上初始化的 Redis 容器实例来模拟故障。 一旦实例停止,order-processor 服务的写入和读取操作将开始失败。

由于 resiliency.yaml 规范将 statestore 定义为组件目标,所有失败的请求将应用重试和断路器策略:

  1. targets:
  2. components:
  3. statestore:
  4. outbound:
  5. retry: retryForever
  6. circuitBreaker: simpleCB

在新的终端窗口中,运行以下命令以停止Redis:

  1. docker stop dapr_redis

一旦 Redis 停止,请求开始失败,并应用名为 retryForever 的重试策略。 下面的输出显示了来自order-processor服务的日志:

  1. INFO[0006] Error processing operation component[statestore] output. Retrying...

根据retryForever策略,重试将无限期地对每个失败的请求进行,每隔5秒一次。

  1. retryForever:
  2. policy: constant
  3. maxInterval: 5s
  4. maxRetries: -1

一旦连续失败5次重试,断路器策略 simpleCB,将被触发,断路器打开,停止所有请求:

  1. INFO[0026] Circuit breaker "simpleCB-statestore" changed state from closed to open
  1. circuitBreakers:
  2. simpleCB:
  3. maxRequests: 1
  4. timeout: 5s
  5. trip: consecutiveFailures >= 5

超过5秒后,断路器将切换到半开状态,允许一个请求通过以验证故障是否已解决。 如果请求继续失败,将会跳回到打开状态。

  1. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  2. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open
  3. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  4. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed

只要停止 Redis 容器,这种半开/开放行为将继续下去。

第3步:移除故障

一旦您重新启动您机器上的Redis容器,应用程序将无缝恢复,继续接受订单请求。

  1. docker start dapr_redis
  1. INFO[0036] Recovered processing operation component[statestore] output.
  2. == APP == Saving Order: { orderId: '5' }
  3. == APP == Getting Order: { orderId: '5' }
  4. == APP == Saving Order: { orderId: '6' }
  5. == APP == Getting Order: { orderId: '6' }
  6. == APP == Saving Order: { orderId: '7' }
  7. == APP == Getting Order: { orderId: '7' }
  8. == APP == Saving Order: { orderId: '8' }
  9. == APP == Getting Order: { orderId: '8' }
  10. == APP == Saving Order: { orderId: '9' }
  11. == APP == Getting Order: { orderId: '9' }

先决条件

对于此示例,您将需要:

第1步:设置环境

克隆在Quickstarts存储库中提供的示例

  1. git clone https://github.com/dapr/quickstarts.git

在一个终端窗口中,导航到 order-processor 目录。

  1. cd ../state_management/javascript/sdk/order-processor

安装依赖项:

  1. npm install

步骤 2:运行应用程序

与 Dapr sidecar 一起运行 order-processor 服务。 然后 Dapr sidecar 加载位于资源目录中的弹性规范:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Resiliency
  3. metadata:
  4. name: myresiliency
  5. scopes:
  6. - checkout
  7. spec:
  8. policies:
  9. retries:
  10. retryForever:
  11. policy: constant
  12. maxInterval: 5s
  13. maxRetries: -1
  14. circuitBreakers:
  15. simpleCB:
  16. maxRequests: 1
  17. timeout: 5s
  18. trip: consecutiveFailures >= 5
  19. targets:
  20. apps:
  21. order-processor:
  22. retry: retryForever
  23. circuitBreaker: simpleCB
  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- npm start

应用程序启动后,order-processor服务将orderId键/值对写入和读取到statestore Redis实例 在statestore.yaml组件中定义

  1. == APP == Saving Order: { orderId: '1' }
  2. == APP == Getting Order: { orderId: '1' }
  3. == APP == Saving Order: { orderId: '2' }
  4. == APP == Getting Order: { orderId: '2' }
  5. == APP == Saving Order: { orderId: '3' }
  6. == APP == Getting Order: { orderId: '3' }
  7. == APP == Saving Order: { orderId: '4' }
  8. == APP == Getting Order: { orderId: '4' }

步骤 3:引入故障

在执行 dapr init 时,通过停止在开发机器上初始化的 Redis 容器实例来模拟故障。 一旦实例停止,order-processor 服务的写入和读取操作将开始失败。

由于 resiliency.yaml 规范将 statestore 定义为组件目标,所有失败的请求将应用重试和断路器策略:

  1. targets:
  2. components:
  3. statestore:
  4. outbound:
  5. retry: retryForever
  6. circuitBreaker: simpleCB

在新的终端窗口中,运行以下命令以停止Redis:

  1. docker stop dapr_redis

一旦 Redis 停止,请求开始失败,并应用名为 retryForever 的重试策略。 下面的输出显示了来自order-processor服务的日志:

  1. INFO[0006] Error processing operation component[statestore] output. Retrying...

根据retryForever策略,重试将无限期地对每个失败的请求进行,每隔5秒一次。

  1. retryForever:
  2. policy: constant
  3. maxInterval: 5s
  4. maxRetries: -1

一旦连续失败5次重试,断路器策略 simpleCB,将被触发,断路器打开,停止所有请求:

  1. INFO[0026] Circuit breaker "simpleCB-statestore" changed state from closed to open
  1. circuitBreakers:
  2. simpleCB:
  3. maxRequests: 1
  4. timeout: 5s
  5. trip: consecutiveFailures >= 5

超过5秒后,断路器将切换到半开状态,允许一个请求通过以验证故障是否已解决。 如果请求继续失败,将会跳回到打开状态。

  1. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  2. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open
  3. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  4. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed

只要停止 Redis 容器,这种半开/开放行为将继续下去。

第3步:移除故障

一旦您重新启动您机器上的Redis容器,应用程序将无缝恢复,继续接受订单请求。

  1. docker start dapr_redis
  1. INFO[0036] Recovered processing operation component[statestore] output.
  2. == APP == Saving Order: { orderId: '5' }
  3. == APP == Getting Order: { orderId: '5' }
  4. == APP == Saving Order: { orderId: '6' }
  5. == APP == Getting Order: { orderId: '6' }
  6. == APP == Saving Order: { orderId: '7' }
  7. == APP == Getting Order: { orderId: '7' }
  8. == APP == Saving Order: { orderId: '8' }
  9. == APP == Getting Order: { orderId: '8' }
  10. == APP == Saving Order: { orderId: '9' }
  11. == APP == Getting Order: { orderId: '9' }

先决条件

对于此示例,您将需要:

第1步:设置环境

克隆在Quickstarts存储库中提供的示例

  1. git clone https://github.com/dapr/quickstarts.git

在一个终端窗口中,导航到 order-processor 目录。

  1. cd ../state_management/csharp/sdk/order-processor

安装依赖项:

  1. dotnet restore
  2. dotnet build

步骤 2:运行应用程序

与 Dapr sidecar 一起运行 order-processor 服务。 然后 Dapr sidecar 加载位于资源目录中的弹性规范:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Resiliency
  3. metadata:
  4. name: myresiliency
  5. scopes:
  6. - checkout
  7. spec:
  8. policies:
  9. retries:
  10. retryForever:
  11. policy: constant
  12. maxInterval: 5s
  13. maxRetries: -1
  14. circuitBreakers:
  15. simpleCB:
  16. maxRequests: 1
  17. timeout: 5s
  18. trip: consecutiveFailures >= 5
  19. targets:
  20. apps:
  21. order-processor:
  22. retry: retryForever
  23. circuitBreaker: simpleCB
  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- dotnet run

应用程序启动后,order-processor服务将orderId键/值对写入和读取到statestore Redis实例 在statestore.yaml组件中定义

  1. == APP == Saving Order: { orderId: '1' }
  2. == APP == Getting Order: { orderId: '1' }
  3. == APP == Saving Order: { orderId: '2' }
  4. == APP == Getting Order: { orderId: '2' }
  5. == APP == Saving Order: { orderId: '3' }
  6. == APP == Getting Order: { orderId: '3' }
  7. == APP == Saving Order: { orderId: '4' }
  8. == APP == Getting Order: { orderId: '4' }

步骤 3:引入故障

在执行 dapr init 时,通过停止在开发机器上初始化的 Redis 容器实例来模拟故障。 一旦实例停止,order-processor 服务的写入和读取操作将开始失败。

由于 resiliency.yaml 规范将 statestore 定义为组件目标,所有失败的请求将应用重试和断路器策略:

  1. targets:
  2. components:
  3. statestore:
  4. outbound:
  5. retry: retryForever
  6. circuitBreaker: simpleCB

在新的终端窗口中,运行以下命令以停止Redis:

  1. docker stop dapr_redis

一旦 Redis 停止,请求开始失败,并应用名为 retryForever 的重试策略。 下面的输出显示了来自order-processor服务的日志:

  1. INFO[0006] Error processing operation component[statestore] output. Retrying...

根据retryForever策略,重试将无限期地对每个失败的请求进行,每隔5秒一次。

  1. retryForever:
  2. policy: constant
  3. maxInterval: 5s
  4. maxRetries: -1

一旦连续失败5次重试,断路器策略 simpleCB,将被触发,断路器打开,停止所有请求:

  1. INFO[0026] Circuit breaker "simpleCB-statestore" changed state from closed to open
  1. circuitBreakers:
  2. simpleCB:
  3. maxRequests: 1
  4. timeout: 5s
  5. trip: consecutiveFailures >= 5

超过5秒后,断路器将切换到半开状态,允许一个请求通过以验证故障是否已解决。 如果请求继续失败,将会跳回到打开状态。

  1. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  2. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open
  3. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  4. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed

只要停止 Redis 容器,这种半开/开放行为将继续下去。

第3步:移除故障

一旦您重新启动您机器上的Redis容器,应用程序将无缝恢复,继续接受订单请求。

  1. docker start dapr_redis
  1. INFO[0036] Recovered processing operation component[statestore] output.
  2. == APP == Saving Order: { orderId: '5' }
  3. == APP == Getting Order: { orderId: '5' }
  4. == APP == Saving Order: { orderId: '6' }
  5. == APP == Getting Order: { orderId: '6' }
  6. == APP == Saving Order: { orderId: '7' }
  7. == APP == Getting Order: { orderId: '7' }
  8. == APP == Saving Order: { orderId: '8' }
  9. == APP == Getting Order: { orderId: '8' }
  10. == APP == Saving Order: { orderId: '9' }
  11. == APP == Getting Order: { orderId: '9' }

先决条件

对于此示例,您将需要:

第1步:设置环境

克隆在Quickstarts存储库中提供的示例

  1. git clone https://github.com/dapr/quickstarts.git

在一个终端窗口中,导航到 order-processor 目录。

  1. cd ../state_management/java/sdk/order-processor

安装依赖项:

  1. mvn clean install

步骤 2:运行应用程序

与 Dapr sidecar 一起运行 order-processor 服务。 然后 Dapr sidecar 加载位于资源目录中的弹性规范:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Resiliency
  3. metadata:
  4. name: myresiliency
  5. scopes:
  6. - checkout
  7. spec:
  8. policies:
  9. retries:
  10. retryForever:
  11. policy: constant
  12. maxInterval: 5s
  13. maxRetries: -1
  14. circuitBreakers:
  15. simpleCB:
  16. maxRequests: 1
  17. timeout: 5s
  18. trip: consecutiveFailures >= 5
  19. targets:
  20. apps:
  21. order-processor:
  22. retry: retryForever
  23. circuitBreaker: simpleCB
  1. dapr run --app-id order-processor --resources-path ../../../resources/ -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

应用程序启动后,order-processor服务将orderId键/值对写入和读取到statestore Redis实例 在statestore.yaml组件中定义

  1. == APP == Saving Order: { orderId: '1' }
  2. == APP == Getting Order: { orderId: '1' }
  3. == APP == Saving Order: { orderId: '2' }
  4. == APP == Getting Order: { orderId: '2' }
  5. == APP == Saving Order: { orderId: '3' }
  6. == APP == Getting Order: { orderId: '3' }
  7. == APP == Saving Order: { orderId: '4' }
  8. == APP == Getting Order: { orderId: '4' }

步骤 3:引入故障

在执行 dapr init 时,通过停止在开发机器上初始化的 Redis 容器实例来模拟故障。 一旦实例停止,order-processor 服务的写入和读取操作将开始失败。

由于 resiliency.yaml 规范将 statestore 定义为组件目标,所有失败的请求将应用重试和断路器策略:

  1. targets:
  2. components:
  3. statestore:
  4. outbound:
  5. retry: retryForever
  6. circuitBreaker: simpleCB

在新的终端窗口中,运行以下命令以停止Redis:

  1. docker stop dapr_redis

一旦 Redis 停止,请求开始失败,并应用名为 retryForever 的重试策略。 下面的输出显示了来自order-processor服务的日志:

  1. INFO[0006] Error processing operation component[statestore] output. Retrying...

根据retryForever策略,重试将无限期地对每个失败的请求进行,每隔5秒一次。

  1. retryForever:
  2. policy: constant
  3. maxInterval: 5s
  4. maxRetries: -1

一旦连续失败5次重试,断路器策略 simpleCB,将被触发,断路器打开,停止所有请求:

  1. INFO[0026] Circuit breaker "simpleCB-statestore" changed state from closed to open
  1. circuitBreakers:
  2. simpleCB:
  3. maxRequests: 1
  4. timeout: 5s
  5. trip: consecutiveFailures >= 5

超过5秒后,断路器将切换到半开状态,允许一个请求通过以验证故障是否已解决。 如果请求继续失败,将会跳回到打开状态。

  1. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  2. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open
  3. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  4. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed

只要停止 Redis 容器,这种半开/开放行为将继续下去。

第3步:移除故障

一旦您重新启动您机器上的Redis容器,应用程序将无缝恢复,继续接受订单请求。

  1. docker start dapr_redis
  1. INFO[0036] Recovered processing operation component[statestore] output.
  2. == APP == Saving Order: { orderId: '5' }
  3. == APP == Getting Order: { orderId: '5' }
  4. == APP == Saving Order: { orderId: '6' }
  5. == APP == Getting Order: { orderId: '6' }
  6. == APP == Saving Order: { orderId: '7' }
  7. == APP == Getting Order: { orderId: '7' }
  8. == APP == Saving Order: { orderId: '8' }
  9. == APP == Getting Order: { orderId: '8' }
  10. == APP == Saving Order: { orderId: '9' }
  11. == APP == Getting Order: { orderId: '9' }

先决条件

对于此示例,您将需要:

第1步:设置环境

克隆在Quickstarts存储库中提供的示例

  1. git clone https://github.com/dapr/quickstarts.git

在一个终端窗口中,导航到 order-processor 目录。

  1. cd ../state_management/go/sdk/order-processor

安装依赖项:

  1. go build .

步骤 2:运行应用程序

与 Dapr sidecar 一起运行 order-processor 服务。 然后 Dapr sidecar 加载位于资源目录中的弹性规范:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Resiliency
  3. metadata:
  4. name: myresiliency
  5. scopes:
  6. - checkout
  7. spec:
  8. policies:
  9. retries:
  10. retryForever:
  11. policy: constant
  12. maxInterval: 5s
  13. maxRetries: -1
  14. circuitBreakers:
  15. simpleCB:
  16. maxRequests: 1
  17. timeout: 5s
  18. trip: consecutiveFailures >= 5
  19. targets:
  20. apps:
  21. order-processor:
  22. retry: retryForever
  23. circuitBreaker: simpleCB
  1. dapr run --app-id order-processor --resources-path ../../../resources -- go run .

应用程序启动后,order-processor服务将orderId键/值对写入和读取到statestore Redis实例 在statestore.yaml组件中定义

  1. == APP == Saving Order: { orderId: '1' }
  2. == APP == Getting Order: { orderId: '1' }
  3. == APP == Saving Order: { orderId: '2' }
  4. == APP == Getting Order: { orderId: '2' }
  5. == APP == Saving Order: { orderId: '3' }
  6. == APP == Getting Order: { orderId: '3' }
  7. == APP == Saving Order: { orderId: '4' }
  8. == APP == Getting Order: { orderId: '4' }

步骤 3:引入故障

在执行 dapr init 时,通过停止在开发机器上初始化的 Redis 容器实例来模拟故障。 一旦实例停止,order-processor 服务的写入和读取操作将开始失败。

由于 resiliency.yaml 规范将 statestore 定义为组件目标,所有失败的请求将应用重试和断路器策略:

  1. targets:
  2. components:
  3. statestore:
  4. outbound:
  5. retry: retryForever
  6. circuitBreaker: simpleCB

在新的终端窗口中,运行以下命令以停止Redis:

  1. docker stop dapr_redis

一旦 Redis 停止,请求开始失败,并应用名为 retryForever 的重试策略。 下面的输出显示了来自 order-processor 服务的日志:

  1. INFO[0006] Error processing operation component[statestore] output. Retrying...

根据retryForever策略,重试将无限期地对每个失败的请求进行,每隔5秒一次。

  1. retryForever:
  2. policy: constant
  3. maxInterval: 5s
  4. maxRetries: -1

一旦连续失败5次重试,断路器策略 simpleCB,将被触发,断路器打开,停止所有请求:

  1. INFO[0026] Circuit breaker "simpleCB-statestore" changed state from closed to open
  1. circuitBreakers:
  2. simpleCB:
  3. maxRequests: 1
  4. timeout: 5s
  5. trip: consecutiveFailures >= 5

超过5秒后,断路器将切换到半开状态,允许一个请求通过以验证故障是否已解决。 如果请求继续失败,将会跳回到打开状态。

  1. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  2. INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open
  3. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open
  4. INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed

只要停止 Redis 容器,这种半开/开放行为将继续下去。

第3步:移除故障

一旦您重新启动您机器上的Redis容器,应用程序将无缝恢复,继续接受订单请求。

  1. docker start dapr_redis
  1. INFO[0036] Recovered processing operation component[statestore] output.
  2. == APP == Saving Order: { orderId: '5' }
  3. == APP == Getting Order: { orderId: '5' }
  4. == APP == Saving Order: { orderId: '6' }
  5. == APP == Getting Order: { orderId: '6' }
  6. == APP == Saving Order: { orderId: '7' }
  7. == APP == Getting Order: { orderId: '7' }
  8. == APP == Saving Order: { orderId: '8' }
  9. == APP == Getting Order: { orderId: '8' }
  10. == APP == Saving Order: { orderId: '9' }
  11. == APP == Getting Order: { orderId: '9' }

告诉我们您的想法

我们一直在努力改进我们的快速入门示例,并重视您的反馈。 您觉得此快速入门有帮助吗? 您有改进的建议吗?

加入我们的discord频道参与讨论。

下一步

了解有关弹性功能以及它如何与Dapr的构建块API一起工作。

探索 Dapr 教程 >>