This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

可靠性快速入门

开始探索 Dapr 的可靠性组件

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

通过Dapr的状态管理API来了解其弹性功能

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

  • 运行一个微服务应用程序,该应用程序通过Dapr的状态管理API持续保存和检索状态。
  • 通过模拟系统故障来触发弹性策略。
  • 解决故障后,微服务应用程序将恢复。
显示应用于Dapr API的弹性示意图

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

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆快速入门仓库中提供的示例

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

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

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

安装依赖项

pip3 install -r requirements.txt 

步骤2:运行应用程序

在Dapr边车的支持下运行order-processor服务。然后,Dapr边车会加载位于资源目录中的弹性规范:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - order-processor

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        duration: 5s
        maxRetries: -1

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s
        trip: consecutiveFailures >= 5

  targets:
    components:
      statestore:
        outbound:
          retry: retryForever
          circuitBreaker: simpleCB
dapr run --app-id order-processor --resources-path ../../../resources/ -- python3

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

== APP == Saving Order:  { orderId: '1' }
== APP == Getting Order:  { orderId: '1' }
== APP == Saving Order:  { orderId: '2' }
== APP == Getting Order:  { orderId: '2' }
== APP == Saving Order:  { orderId: '3' }
== APP == Getting Order:  { orderId: '3' }
== APP == Saving Order:  { orderId: '4' }
== APP == Getting Order:  { orderId: '4' }

步骤3:引入故障

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

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

  targets:
    components:
      statestore:
        outbound:
          retry: retryForever
          circuitBreaker: simpleCB

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

docker stop dapr_redis

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

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

根据retryForever策略,重试将以5秒间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

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

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

INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open 
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed  

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

步骤3:移除故障

当您在机器上重新启动Redis容器后,应用程序将无缝恢复并继续之前的操作。

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

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆快速入门仓库中提供的示例

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

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

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

安装依赖项

npm install

步骤2:运行应用程序

在Dapr边车的支持下运行order-processor服务。然后,Dapr边车会加载位于资源目录中的弹性规范:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB
dapr run --app-id order-processor --resources-path ../../../resources/ -- npm start

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

== APP == Saving Order:  { orderId: '1' }
== APP == Getting Order:  { orderId: '1' }
== APP == Saving Order:  { orderId: '2' }
== APP == Getting Order:  { orderId: '2' }
== APP == Saving Order:  { orderId: '3' }
== APP == Getting Order:  { orderId: '3' }
== APP == Saving Order:  { orderId: '4' }
== APP == Getting Order:  { orderId: '4' }

步骤3:引入故障

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

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

  targets:
    components:
      statestore:
        outbound:
          retry: retryForever
          circuitBreaker: simpleCB

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

docker stop dapr_redis

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

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

根据retryForever策略,重试将以5秒间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

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

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

INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open 
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed  

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

步骤3:移除故障

当您在机器上重新启动Redis容器后,应用程序将无缝恢复并继续之前的操作。

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

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆快速入门仓库中提供的示例

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

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

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

安装依赖项

dotnet restore
dotnet build

步骤2:运行应用程序

在Dapr边车的支持下运行order-processor服务。然后,Dapr边车会加载位于资源目录中的弹性规范:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB
dapr run --app-id order-processor --resources-path ../../../resources/ -- dotnet run

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

== APP == Saving Order:  { orderId: '1' }
== APP == Getting Order:  { orderId: '1' }
== APP == Saving Order:  { orderId: '2' }
== APP == Getting Order:  { orderId: '2' }
== APP == Saving Order:  { orderId: '3' }
== APP == Getting Order:  { orderId: '3' }
== APP == Saving Order:  { orderId: '4' }
== APP == Getting Order:  { orderId: '4' }

步骤3:引入故障

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

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

  targets:
    components:
      statestore:
        outbound:
          retry: retryForever
          circuitBreaker: simpleCB

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

docker stop dapr_redis

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

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

根据retryForever策略,重试将以5秒间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

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

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

INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open 
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed  

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

步骤3:移除故障

当您在机器上重新启动Redis容器后,应用程序将无缝恢复并继续之前的操作。

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

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆快速入门仓库中提供的示例

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

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

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

安装依赖项

mvn clean install

步骤2:运行应用程序

在Dapr边车的支持下运行order-processor服务。然后,Dapr边车会加载位于资源目录中的弹性规范:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB
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组件中定义

== APP == Saving Order:  { orderId: '1' }
== APP == Getting Order:  { orderId: '1' }
== APP == Saving Order:  { orderId: '2' }
== APP == Getting Order:  { orderId: '2' }
== APP == Saving Order:  { orderId: '3' }
== APP == Getting Order:  { orderId: '3' }
== APP == Saving Order:  { orderId: '4' }
== APP == Getting Order:  { orderId: '4' }

步骤3:引入故障

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

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

  targets:
    components:
      statestore:
        outbound:
          retry: retryForever
          circuitBreaker: simpleCB

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

docker stop dapr_redis

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

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

根据retryForever策略,重试将以5秒间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

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

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

INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open 
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed  

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

步骤3:移除故障

当您在机器上重新启动Redis容器后,应用程序将无缝恢复并继续之前的操作。

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

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆快速入门仓库中提供的示例

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

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

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

安装依赖项

go build .

步骤2:运行应用程序

在Dapr边车的支持下运行order-processor服务。然后,Dapr边车会加载位于资源目录中的弹性规范:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB
dapr run --app-id order-processor --resources-path ../../../resources -- go run .

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

== APP == Saving Order:  { orderId: '1' }
== APP == Getting Order:  { orderId: '1' }
== APP == Saving Order:  { orderId: '2' }
== APP == Getting Order:  { orderId: '2' }
== APP == Saving Order:  { orderId: '3' }
== APP == Getting Order:  { orderId: '3' }
== APP == Saving Order:  { orderId: '4' }
== APP == Getting Order:  { orderId: '4' }

步骤3:引入故障

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

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

  targets:
    components:
      statestore:
        outbound:
          retry: retryForever
          circuitBreaker: simpleCB

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

docker stop dapr_redis

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

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

根据retryForever策略,重试将以5秒间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

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

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

INFO[0031] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0031] Circuit breaker "simpleCB-statestore" changed state from half-open to open 
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from open to half-open  
INFO[0036] Circuit breaker "simpleCB-statestore" changed state from half-open to closed  

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

步骤3:移除故障

当您在机器上重新启动Redis容器后,应用程序将无缝恢复并继续之前的操作。

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

告诉我们您的想法!

我们正在不断努力改进我们的快速入门示例,并重视您的反馈。您觉得这个快速入门有帮助吗?您有改进建议吗?

加入我们的discord频道讨论。

下一步

了解更多关于弹性功能及其如何与Dapr的构建块API协作。

探索Dapr教程 >>

2 - 入门指南:服务间的弹性

通过服务调用API开始使用Dapr的弹性功能

通过模拟系统故障来测试Dapr的弹性功能。在本入门指南中,您将会:

  • 运行两个微服务应用程序:checkoutorder-processorcheckout将持续向order-processor发起Dapr服务调用请求。
  • 通过模拟系统故障来触发弹性机制。
  • 移除故障以让微服务应用程序恢复。
显示应用于Dapr API的弹性图示

在继续入门指南之前,选择您偏好的Dapr SDK语言版本。

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆入门指南仓库中提供的示例

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

步骤2:运行order-processor服务

在终端窗口中,从入门指南目录的根目录导航到order-processor目录。

cd service_invocation/python/http/order-processor

安装依赖项:

pip3 install -r requirements.txt

运行order-processor服务及其Dapr sidecar。

dapr run --app-port 8001 --app-id order-processor --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3501 -- python3 app.py

步骤3:运行checkout服务应用程序

在新的终端窗口中,从入门指南目录的根目录导航到checkout目录。

cd service_invocation/python/http/checkout

安装依赖项:

pip3 install -r requirements.txt

运行checkout服务及其Dapr sidecar。

dapr run --app-id checkout --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3500 -- python3 app.py

Dapr sidecar随后加载位于资源目录中的弹性配置:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

步骤4:查看服务调用输出

当两个服务和sidecar都在运行时,注意订单如何通过Dapr服务调用从checkout服务传递到order-processor服务。

checkout服务输出:

== APP == Order passed: {"orderId": 1}
== APP == Order passed: {"orderId": 2}
== APP == Order passed: {"orderId": 3}
== APP == Order passed: {"orderId": 4}

order-processor服务输出:

== APP == Order received: {"orderId": 1}
== APP == Order received: {"orderId": 2}
== APP == Order received: {"orderId": 3}
== APP == Order received: {"orderId": 4}

步骤5:引入故障

通过停止order-processor服务来模拟故障。一旦实例停止,来自checkout服务的服务调用操作将开始失败。

由于resiliency.yaml配置将order-processor服务定义为弹性目标,所有失败的请求将应用重试和断路器策略:

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

order-processor窗口中,停止服务:

CTRL + C

一旦第一个请求失败,名为retryForever的重试策略将被应用:

INFO[0005] Error processing operation endpoint[order-processor, order-processor:orders]. Retrying...  

重试将以5秒的间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

INFO[0025] Circuit breaker "order-processor:orders" changed state from closed to open  
circuitBreakers:
  simpleCB:
  maxRequests: 1
  timeout: 5s 
  trip: consecutiveFailures >= 5

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

INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open   
INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open     

这种半开/打开行为将持续到order-processor服务停止为止。

步骤6:移除故障

一旦您重新启动order-processor服务,应用程序将无缝恢复,继续接受订单请求。

order-processor服务终端中,重新启动应用程序:

dapr run --app-port 8001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- python3 app.py

checkout服务输出:

== APP == Order passed: {"orderId": 5}
== APP == Order passed: {"orderId": 6}
== APP == Order passed: {"orderId": 7}
== APP == Order passed: {"orderId": 8}
== APP == Order passed: {"orderId": 9}
== APP == Order passed: {"orderId": 10}

order-processor服务输出:

== APP == Order received: {"orderId": 5}
== APP == Order received: {"orderId": 6}
== APP == Order received: {"orderId": 7}
== APP == Order received: {"orderId": 8}
== APP == Order received: {"orderId": 9}
== APP == Order received: {"orderId": 10}

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆入门指南仓库中提供的示例

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

步骤2:运行order-processor服务

在终端窗口中,从入门指南目录的根目录导航到order-processor目录。

cd service_invocation/javascript/http/order-processor

安装依赖项:

npm install

运行order-processor服务及其Dapr sidecar。

dapr run --app-port 5001 --app-id order-processor --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3501 -- npm start

步骤3:运行checkout服务应用程序

在新的终端窗口中,从入门指南目录的根目录导航到checkout目录。

cd service_invocation/javascript/http/checkout

安装依赖项:

npm install

运行checkout服务及其Dapr sidecar。

dapr run --app-id checkout --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3500 -- npm start

Dapr sidecar随后加载位于资源目录中的弹性配置:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

步骤4:查看服务调用输出

当两个服务和sidecar都在运行时,注意订单如何通过Dapr服务调用从checkout服务传递到order-processor服务。

checkout服务输出:

== APP == Order passed: {"orderId": 1}
== APP == Order passed: {"orderId": 2}
== APP == Order passed: {"orderId": 3}
== APP == Order passed: {"orderId": 4}

order-processor服务输出:

== APP == Order received: {"orderId": 1}
== APP == Order received: {"orderId": 2}
== APP == Order received: {"orderId": 3}
== APP == Order received: {"orderId": 4}

步骤5:引入故障

通过停止order-processor服务来模拟故障。一旦实例停止,来自checkout服务的服务调用操作将开始失败。

由于resiliency.yaml配置将order-processor服务定义为弹性目标,所有失败的请求将应用重试和断路器策略:

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

order-processor窗口中,停止服务:

CMD + C
CTRL + C

一旦第一个请求失败,名为retryForever的重试策略将被应用:

INFO[0005] Error processing operation endpoint[order-processor, order-processor:orders]. Retrying...  

重试将以5秒的间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

INFO[0025] Circuit breaker "order-processor:orders" changed state from closed to open  
circuitBreakers:
  simpleCB:
  maxRequests: 1
  timeout: 5s 
  trip: consecutiveFailures >= 5

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

INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open   
INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open     

这种半开/打开行为将持续到order-processor服务停止为止。

步骤6:移除故障

一旦您重新启动order-processor服务,应用程序将无缝恢复,继续接受订单请求。

order-processor服务终端中,重新启动应用程序:

dapr run --app-port 5001 --app-id order-processor --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3501 -- npm start

checkout服务输出:

== APP == Order passed: {"orderId": 5}
== APP == Order passed: {"orderId": 6}
== APP == Order passed: {"orderId": 7}
== APP == Order passed: {"orderId": 8}
== APP == Order passed: {"orderId": 9}
== APP == Order passed: {"orderId": 10}

order-processor服务输出:

== APP == Order received: {"orderId": 5}
== APP == Order received: {"orderId": 6}
== APP == Order received: {"orderId": 7}
== APP == Order received: {"orderId": 8}
== APP == Order received: {"orderId": 9}
== APP == Order received: {"orderId": 10}

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆入门指南仓库中提供的示例

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

步骤2:运行order-processor服务

在终端窗口中,从入门指南目录的根目录导航到order-processor目录。

cd service_invocation/csharp/http/order-processor

安装依赖项:

dotnet restore
dotnet build

运行order-processor服务及其Dapr sidecar。

dapr run --app-port 7001 --app-id order-processor --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3501 -- dotnet run

步骤3:运行checkout服务应用程序

在新的终端窗口中,从入门指南目录的根目录导航到checkout目录。

cd service_invocation/csharp/http/checkout

安装依赖项:

dotnet restore
dotnet build

运行checkout服务及其Dapr sidecar。

dapr run --app-id checkout --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3500 -- dotnet run

Dapr sidecar随后加载位于资源目录中的弹性配置:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

步骤4:查看服务调用输出

当两个服务和sidecar都在运行时,注意订单如何通过Dapr服务调用从checkout服务传递到order-processor服务。

checkout服务输出:

== APP == Order passed: {"orderId": 1}
== APP == Order passed: {"orderId": 2}
== APP == Order passed: {"orderId": 3}
== APP == Order passed: {"orderId": 4}

order-processor服务输出:

== APP == Order received: {"orderId": 1}
== APP == Order received: {"orderId": 2}
== APP == Order received: {"orderId": 3}
== APP == Order received: {"orderId": 4}

步骤5:引入故障

通过停止order-processor服务来模拟故障。一旦实例停止,来自checkout服务的服务调用操作将开始失败。

由于resiliency.yaml配置将order-processor服务定义为弹性目标,所有失败的请求将应用重试和断路器策略:

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

order-processor窗口中,停止服务:

CMD + C
CTRL + C

一旦第一个请求失败,名为retryForever的重试策略将被应用:

INFO[0005] Error processing operation endpoint[order-processor, order-processor:orders]. Retrying...  

重试将以5秒的间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

INFO[0025] Circuit breaker "order-processor:orders" changed state from closed to open  
circuitBreakers:
  simpleCB:
  maxRequests: 1
  timeout: 5s 
  trip: consecutiveFailures >= 5

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

INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open   
INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open     

这种半开/打开行为将持续到order-processor服务停止为止。

步骤6:移除故障

一旦您重新启动order-processor服务,应用程序将无缝恢复,继续接受订单请求。

order-processor服务终端中,重新启动应用程序:

dapr run --app-port 7001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- dotnet run

checkout服务输出:

== APP == Order passed: {"orderId": 5}
== APP == Order passed: {"orderId": 6}
== APP == Order passed: {"orderId": 7}
== APP == Order passed: {"orderId": 8}
== APP == Order passed: {"orderId": 9}
== APP == Order passed: {"orderId": 10}

order-processor服务输出:

== APP == Order received: {"orderId": 5}
== APP == Order received: {"orderId": 6}
== APP == Order received: {"orderId": 7}
== APP == Order received: {"orderId": 8}
== APP == Order received: {"orderId": 9}
== APP == Order received: {"orderId": 10}

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆入门指南仓库中提供的示例

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

步骤2:运行order-processor服务

在终端窗口中,从入门指南目录的根目录导航到order-processor目录。

cd service_invocation/java/http/order-processor

安装依赖项:

mvn clean install

运行order-processor服务及其Dapr sidecar。

dapr run --app-id order-processor --resources-path ../../../resources/ --app-port 9001 --app-protocol http --dapr-http-port 3501 -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

步骤3:运行checkout服务应用程序

在新的终端窗口中,从入门指南目录的根目录导航到checkout目录。

cd service_invocation/java/http/checkout

安装依赖项:

mvn clean install

运行checkout服务及其Dapr sidecar。

dapr run --app-id checkout --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3500 -- java -jar target/CheckoutService-0.0.1-SNAPSHOT.jar

Dapr sidecar随后加载位于资源目录中的弹性配置:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

步骤4:查看服务调用输出

当两个服务和sidecar都在运行时,注意订单如何通过Dapr服务调用从checkout服务传递到order-processor服务。

checkout服务输出:

== APP == Order passed: {"orderId": 1}
== APP == Order passed: {"orderId": 2}
== APP == Order passed: {"orderId": 3}
== APP == Order passed: {"orderId": 4}

order-processor服务输出:

== APP == Order received: {"orderId": 1}
== APP == Order received: {"orderId": 2}
== APP == Order received: {"orderId": 3}
== APP == Order received: {"orderId": 4}

步骤5:引入故障

通过停止order-processor服务来模拟故障。一旦实例停止,来自checkout服务的服务调用操作将开始失败。

由于resiliency.yaml配置将order-processor服务定义为弹性目标,所有失败的请求将应用重试和断路器策略:

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

order-processor窗口中,停止服务:

CMD + C
CTRL + C

一旦第一个请求失败,名为retryForever的重试策略将被应用:

INFO[0005] Error processing operation endpoint[order-processor, order-processor:orders]. Retrying...  

重试将以5秒的间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

INFO[0025] Circuit breaker "order-processor:orders" changed state from closed to open  
circuitBreakers:
  simpleCB:
  maxRequests: 1
  timeout: 5s 
  trip: consecutiveFailures >= 5

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

INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open   
INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open     

这种半开/打开行为将持续到order-processor服务停止为止。

步骤6:移除故障

一旦您重新启动order-processor服务,应用程序将无缝恢复,继续接受订单请求。

order-processor服务终端中,重新启动应用程序:

dapr run --app-id order-processor --resources-path ../../../resources/ --app-port 9001 --app-protocol http --dapr-http-port 3501 -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

checkout服务输出:

== APP == Order passed: {"orderId": 5}
== APP == Order passed: {"orderId": 6}
== APP == Order passed: {"orderId": 7}
== APP == Order passed: {"orderId": 8}
== APP == Order passed: {"orderId": 9}
== APP == Order passed: {"orderId": 10}

order-processor服务输出:

== APP == Order received: {"orderId": 5}
== APP == Order received: {"orderId": 6}
== APP == Order received: {"orderId": 7}
== APP == Order received: {"orderId": 8}
== APP == Order received: {"orderId": 9}
== APP == Order received: {"orderId": 10}

前提条件

对于此示例,您将需要:

步骤1:设置环境

克隆入门指南仓库中提供的示例

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

步骤2:运行order-processor服务

在终端窗口中,从入门指南目录的根目录导航到order-processor目录。

cd service_invocation/go/http/order-processor

安装依赖项:

go build .

运行order-processor服务及其Dapr sidecar。

dapr run --app-port 6001 --app-id order-processor --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3501 -- go run .

步骤3:运行checkout服务应用程序

在新的终端窗口中,从入门指南目录的根目录导航到checkout目录。

cd service_invocation/go/http/checkout

安装依赖项:

go build .

运行checkout服务及其Dapr sidecar。

dapr run --app-id checkout --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3500 -- go run .

Dapr sidecar随后加载位于资源目录中的弹性配置:

apiVersion: dapr.io/v1alpha1
kind: Resiliency
metadata:
  name: myresiliency
scopes:
  - checkout

spec:
  policies:
    retries:
      retryForever:
        policy: constant
        maxInterval: 5s
        maxRetries: -1 

    circuitBreakers:
      simpleCB:
        maxRequests: 1
        timeout: 5s 
        trip: consecutiveFailures >= 5

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

步骤4:查看服务调用输出

当两个服务和sidecar都在运行时,注意订单如何通过Dapr服务调用从checkout服务传递到order-processor服务。

checkout服务输出:

== APP == Order passed: {"orderId": 1}
== APP == Order passed: {"orderId": 2}
== APP == Order passed: {"orderId": 3}
== APP == Order passed: {"orderId": 4}

order-processor服务输出:

== APP == Order received: {"orderId": 1}
== APP == Order received: {"orderId": 2}
== APP == Order received: {"orderId": 3}
== APP == Order received: {"orderId": 4}

步骤5:引入故障

通过停止order-processor服务来模拟故障。一旦实例停止,来自checkout服务的服务调用操作将开始失败。

由于resiliency.yaml配置将order-processor服务定义为弹性目标,所有失败的请求将应用重试和断路器策略:

  targets:
    apps:
      order-processor:
        retry: retryForever
        circuitBreaker: simpleCB

order-processor窗口中,停止服务:

CMD + C
CTRL + C

一旦第一个请求失败,名为retryForever的重试策略将被应用:

INFO[0005] Error processing operation endpoint[order-processor, order-processor:orders]. Retrying...  

重试将以5秒的间隔无限期地继续每个失败的请求。

retryForever:
  policy: constant
  maxInterval: 5s
  maxRetries: -1 

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

INFO[0025] Circuit breaker "order-processor:orders" changed state from closed to open  
circuitBreakers:
  simpleCB:
  maxRequests: 1
  timeout: 5s 
  trip: consecutiveFailures >= 5

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

INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open   
INFO[0030] Circuit breaker "order-processor:orders" changed state from open to half-open  
INFO[0030] Circuit breaker "order-processor:orders" changed state from half-open to open     

这种半开/打开行为将持续到order-processor服务停止为止。

步骤6:移除故障

一旦您重新启动order-processor服务,应用程序将无缝恢复,继续接受订单请求。

order-processor服务终端中,重新启动应用程序:

dapr run --app-port 6001 --app-id order-processor --resources-path ../../../resources/ --app-protocol http --dapr-http-port 3501 -- go run .

checkout服务输出:

== APP == Order passed: {"orderId": 5}
== APP == Order passed: {"orderId": 6}
== APP == Order passed: {"orderId": 7}
== APP == Order passed: {"orderId": 8}
== APP == Order passed: {"orderId": 9}
== APP == Order passed: {"orderId": 10}

order-processor服务输出:

== APP == Order received: {"orderId": 5}
== APP == Order received: {"orderId": 6}
== APP == Order received: {"orderId": 7}
== APP == Order received: {"orderId": 8}
== APP == Order received: {"orderId": 9}
== APP == Order received: {"orderId": 10}

告诉我们您的想法!

我们正在不断努力改进我们的入门指南示例,重视您的反馈。您觉得这个入门指南有帮助吗?您有改进建议吗?

加入我们的discord频道讨论。

下一步

访问此链接以获取有关Dapr弹性的更多信息。

探索Dapr教程 >>