快速入门:服务到组件的弹性
通过状态管理API开始使用Dapr的弹性能力
通过模拟系统故障来观察 Dapr 的弹性能力。 在本快速入门中,您将:
- 执行一个微服务应用程序,通过 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 sidecar 一起运行 order-processor
服务。 然后 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
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 sidecar 一起运行 order-processor
服务。 然后 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
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 sidecar 一起运行 order-processor
服务。 然后 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
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' }
先决条件
对于此示例,您将需要:
- Dapr CLI 和初始化环境.
- Java JDK 11(或更高版本):
- Oracle JDK,或者
- OpenJDK
Apache Maven,3.x版本。
第1步:设置环境
git clone https://github.com/dapr/quickstarts.git
在一个终端窗口中,导航到 order-processor
目录。
cd ../state_management/java/sdk/order-processor
安装依赖项:
mvn clean install
步骤 2:运行应用程序
与 Dapr sidecar 一起运行 order-processor
服务。 然后 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
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 sidecar 一起运行 order-processor
服务。 然后 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
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一起工作。