由 AWS Step Functions 所实现的状态机 (State Machine); OrderWorkflow; 描述如下:

State Machine implemented by AWS Step Functions; OrderWorkflow; The description is as follows:

OrderWorkflow 状态机 (State Machine)
  • 由 AWS Step Functions 所实现的状态机; OrderWorkflow; 接受 JSON 当作是输入。
  • State machines implemented by AWS Step Functions; OrderWorkflow; Accepts JSON as input.
  • 当 OrderWorkflow 完成初使化; OrderWorkflow Initialization; 后, 微服务; BookHotelClient; 的 lambda 函式 (lambda function) 便会被 AWS Step Functions 所调用。
  • When OrderWorkflow completes the initialization, OrderWorkflow Initialization;  The lambda function of microservices; BookHotelClient; is invoked by AWS step functions.
  • 当 AWS Step Functions 调用微服务; BookHotelClient; 的 lambda 函式时, 会先解析前一个状态; OrderWorkflow Initialization; 的输入, 并将解析的结果, 以 JSON 的型式, 作为微服务; BookHotelClient; lambda 函式的输入。
  • When AWS Step Functions calls a lambda function of microservice, BookHotelClient, AWS Step Function will parse the input of the previous state; OrderWorkflow Initialization. In the form of JSON, the result of the parsing as a microservice; BookHotelClient; Input to the lambda function.
  • 当微服务; BookHotelClient; lambda 函式执行完毕后, 便会回传一JSON 型式的结果。此时, 工作流 (workflow) 上的控制权又回到了状态机; OrderWorkflow; OrderWorkflow 将直接再回传由微服务; BookHotelClient; lambda 函式所回传的 JSON; 而不会作任何的修改。
  • AFTER THE LAMBDA FUNCTION EXECUTES, a JSON pattern passes back when microservices, BookHotelClient. At this point, control on the workflow returns to the state machine; OrderWorkflow; OrderWorkflow will be directly re-passed back by the microservices; BookHotelClient; JSON passed around by lambda function, And no changes will make.

AWS Step Functions 主要是以 Amazon States Language; https://states-language.net/; 来开发状态机 (State Machine)。

AWS Step Functions primarily based on the Amazon States Language;  https://states-language.net/; to develop the State Machine.

JsonPath:

当某个状态的输入是如下的 JSON 时:

When the input for a state is JSON  like the following:

{
  “foo”: 123,
  “bar”: [“a”, “b”, “c”],
 “car” : {
    “cdr”: true
 }
} 

如下的 JsonPath, 将会传回:

The following JsonPath will pass back:

 JsonPath  回传值 (The return value)
 $.foo   123
 $.bar   [“a”, “b”, “c”]
 $.car.cdr   true

Parameters:

Parameters 就是 key-value 的集合。可作为是状态机; OrderWorkflow; 调用微服务时的输入。

Parameters are the set of key values. Available as a state machine;  OrderWorkflow;  Input when invoking the microservices.

Parameters 中的 key-value:

Key-value in Parameters:

  • value : 由 JsonPath 的方式, 获得所选择的值。
  • value: By way of JsonPath, the selected value obtain.
  • Key : key 的名字后必须要是以 .$ 作为结束。
  • Key: The name of the key must end with .$.
状态机; OrderWorkflow; 调用微服务; BookHotelClient (the state machine; OrderWorkflow; Invoke microservices; BookHotelClient)

如上图所示, 状态机; OrderWorkflow; 调用微服务; BookHotelClient; 时输入的 Parameters 是:

As shown in the figure above,  the state machine;  OrderWorkflow;  Invoke microservices;  BookHotelClient;  The input Parameters when the time is :

 "Parameters": {
       "buyer_id.$": "$.purchase.buyer_id",
       "start_date.$": "$.hotel.start_date",
       "end_date.$": "$.hotel.end_date",
       "near.$": "$.museum.museum_name"
  }

Resource:

以  ARN (Amazon Resource Name) 明确的指定状态机; OrderWorkflow; 所需的资源。

Specified set state machines; OrderWorkflow; required resources in ARN (Amazon Resource Name).

Resource: 状态机; OrderWorkflow; 需调用微服务; BookHotelClient (the state machine, OrderWorkflow, needs to call the microservice, BookHotelClient)

如上图所示; 状态机; OrderWorkflow; 需调用微服务; BookHotelClient; 也就是说, 状态机; OrderWorkflow; 所需要的资源是: 微服务; BookHotelClient。

As shown in the figure above, the state machine, OrderWorkflow, needs to call the microservice, BookHotelClient; that is, the state machine; OrderWorkflow; required resources are: microservices; BookHotelClient。

以 ARN (Amazon Resource Name) 明确的指定状态机; OrderWorkflow; 所需的资源是, 微服务; BookHotelClient:

Specified state machines in ARN (Amazon Resource Name); OrderWorkflow; The resources required are, microservices; BookHotelClient:

"Resource": "arn:aws:lambda:region:accountId:function:BookHotelClient:$LATEST"

ResultPath: 以 AWS Step Functions 所开发的状态机 (State Machine) 的状态的 “输出”, 可以是:

The “output” of a state machine developed in AWS Step Functions can be:

  • 状态输入的复制
  • Replication of status inputs
  • 状态所产生的输出
  • The output produced by the state
  • 状态的输入与输出的组合
  • A combination of input and output of a state

ResultPath 只能使用 “JsonPath来表达:

ResultPath can only use “JsonPath” to express :

  • “ResultPath” : “$” ; 表示状态的 “输出” 就只会有状态的 Lambda function 所产生的输出; 不会包含状态的输入。
  • “ResultPath”: “$”;  The “output” that represents the state will only have the output produced by the stateful Lambda function; No input to the status included.
状态的 “输出” 就只会有状态的 Lambda function 所产生的输出; 不会包含状态的输入 (The “output” that represents the state will only have the output produced by the stateful Lambda function; No input to the status included.)

如上图所示::

As shown in the image above:

状态的输入是:

The inputs to the status are:

{
  "comment": "This is a test of the input and output of a Task state.",
  "details": "Default example",
  "who": "AWS Step Functions"
}

状态的 Lambda function 的产出是:

  • The output of the Lambda function of the state is :
"Hello, AWS Step Functions!"

当 ResultPath 是被设定成是 “ResultPath”: “$” 的时候, 状态的输出就是:只会有状态的 Lambda function 所产生的输出; 不会包含状态的输入。

When ResultPath sets to be “ResultPath”: “$” , the output of the state is: There will only be output from a stateful Lambda function; No input to the status included.

"Hello, AWS Step Functions!"
  • “ResultPath” : null ; 表示状态的 “输入” 会直接的复制成状态的输出; 状态的输出, 不会包含状态的Lambda function 所产生的输出。
  • “ResultPath”: null; The “input” representing the state copies into the output of the state;  The production of the state does not contain the work produced by the lambda function of the state.
“ResultPath” : null ; 表示状态的 “输入” 会直接的复制成状态的输出 (The “input” representing the state copies into the output of the state)

如上图所示:

As shown in the image above:

状态的输入是:

The input for the status is :

{
  "comment": "This is a test of the input and output of a Task state.",
  "details": "Default example",
  "who": "AWS Step Functions"
}

状态的 Lambda function 的产出是:

The output of the Lambda function of the state is :

"Hello, AWS Step Functions!"

当 ResultPath 是被设定成是 “ResultPath”: “null” 的时候, 状态的输出就是:

When ResultPath sets to be “ResultPath”: “null”,  the output of the state is:

{
  "comment": "This is a test of the input and output of a Task state.",
  "details": "Default example",
  "who": "AWS Step Functions"
}

状态的输出就只会有状态的 “输入”; 而不会有状态的 Lambda function 所产生的输出。

The state’s output will only have the “input” of the state; There will be no output from the stated Lambda function.

  • 使用 ResultPath 使得状态的输出能包含状态的 “输入” 与状态的 Lambda function 所产生的输出
  • Using ResultPath makes the state’s output contain the “input” of the state and the work produced by the lambda function of the state.
使用 ResultPath 使得状态的输出能包含状态的 “输入” 与状态的 Lambda function 所产生的输出 (Using ResultPath makes the state’s output contain the “input” of the state and the work produced by the lambda function of the state.)

如上图所示:

As shown in the image above:

状态的输入是:

The input for the status is :

{      
  "comment": "This is a test of the input and output of a Task state.",
  "details": "Default example",
  "who": "AWS Step Functions"
} 

状态的 Lambda function 的产出是:

The output of the Lambda function of the state is :

"Hello, AWS Step Functions!"

当 ResultPath 是被设定成是 “ResultPath”: “$.taskresult” 的时候, 状态的输出就是:

When ResultPath sets to be “ResultPath”: “$.taskresult”, the output of the state is:

 {
      "comment": "This is a test of input and output of a Task state.",
      "details": "Default behavior example",
      "who": "AWS Step Functions",
      "taskresult": "Hello, AWS Step Functions!"
 } 

状态的输出是由状态的输入“附加” 上状态的 Lambda function 的产出所构成的。

The state’s output comprises the state’s lambda function on which the state’s input attaches.

  • 使用 ResultPath 使得状态的输出是由状态的 Lambda function 所产生的输出 ”附加” 在状态的输入的某个 JSON 节点内所构成的
  • We are using ResultPath that the state’s output produces the Lambda function of the state “attached” to a JSON node of the input of the state Within the composition.

状态的输入是:

The input for the status is :

{
  "comment": "An input comment.",
  "strings": {
    "string1": "foo",
    "string2": "bar",
    "string3": "baz"
  },
  "who": "AWS Step Functions"
} 

状态的 Lambda function 的产出是:

The output of the Lambda function of the state is :

"Hello, AWS Step Functions!"

当 ResultPath 是被设定成是 “ResultPath”: “$.strings.lambdaresult” 的时候, 状态的输出就是:

When ResultPath is set to be “ResultPath”: “$.strings.lambdaresult”, the output of the state is:

 {
  "comment": "An input comment.",
  "strings": {
    "string1": "foo",
    "string2": "bar",
    "string3": "baz",
    "lambdaresult": "Hello, AWS Step Functions!"
  },
  "who": "AWS Step Functions"
}

状态的输出是由状态的 Lambda function 所产生的输出附加在状态的输入的 strings 节点内所构成的。

The state’s output comprises work produced by the state’s Lambda function attached to the strings node of the state’s input.

  • 使用 ResultPath 使得状态的输出是由状态的 Lambda function 所产生的输出 ”更新” 在状态的输入的某个 JSON 节点内所构成的
  • Using ResultPath makes a state’s output is the output of the state’s lambda function “updates” within one of the JSON nodes of the input to the state.
使用 ResultPath 使得状态的输出是由状态的 Lambda function 所产生的输出 ”更新” 在状态的输入的某个JSON节点内所构成的 (Using ResultPath makes a state’s output is the output of the state’s lambda function “updates” within one of the JSON nodes of the input to the state.)

如上图所示:

As shown in the figure above:

状态的输入是:

The input for the status is :

{
    "comment": "This is a test of the input and output of a Task state.",
    "details": "Default example",
    "who": "AWS Step Functions"
} 

状态的 Lambda function 的产出是:

The output of the Lambda function of the state is :

Hello, AWS Step Functions!

当 ResultPath 是被设定成是 “ResultPath”: “$.comment” 的时候, 状态的输出就是:

When ResultPath is set to be “ResultPath”: “$.comment”, the output of the state is:

{
  "comment": "Hello, AWS Step Functions!",
  "details": "Default behavior example",
  "who": "AWS Step Functions",
}

状态的输出就是:将状态的输入的 comment 节点的值; “Hello, AWS Step Functions!”; 被由状态的 Lambda function 所产生的输出所取代。

The state’s output is the value of the input comment node of the state; “Hello, AWS Step Functions!”;  Replaced by the output produced by the lambda function of the state.

 

  • 在本文中, 我们将使用 AWS Step Functions, 以编排 (Orchestrate) 的方式, 使得微服务; BookHotelClient, BookMuseumClient; 可共同的完成, 为客户预订酒店与旅游景点门票的服务
  • In this article, we will use AWS Step Functions to orchestrate the way microservices, BookHotelClient, BookMuseumClient; Can be joint for customers to book hotel and tourist attraction tickets service.

如下图所示:

As shown in the following figure :

客户仅需提供旅游的开始日期、结束日期、想去参观的旅游景点; 例如: 某个博物馆。

AWS Step Functions 便以编排 (Orchestrate) 的方式, 使得微服务; BookHotelClient, BookMuseumClient; 可为客户预订距离旅游景点最近的酒店并预订旅游景点的门票。

The customer only needs to provide the start date, end date, and tourist attractions to visit, such as a museum.

AWS Step Functions makes microservices in an Orchestrate manner; BookHotelClient, BookMuseumClient; can book the hotel closest to the tourist attraction and book tickets for the tourist attraction for customers.

AWS Step Functions 以编排 (Orchestrate) 的方式, 使得微服务; BookHotelClient, BookMuseumClient; 可为客户预订距离旅游景点最近的酒店并预订旅游景点的门票 (AWS Step Functions makes microservices in an Orchestrate manner; BookHotelClient, BookMuseumClient; can book the hotel closest to the tourist attraction and book tickets for the tourist attraction for customers.)

在本文中, 客户一定要先确定能预订得到距离旅游景点最近的酒店以后, 才会去预订旅游景点的门票。

而当客户无法成功预订得到旅游景点门票的时候, 客户先前所预订的酒店, 将会被自动的被取消; 退订。

In this article, customers must make sure that they can book the hotel closest to the tourist attraction before booking tickets for the tourist attraction.

When the customer cannot successfully book tickets to the tourist attraction, the hotel previously secured will be automatically canceled; Unsubscribe.

也就是说, 微服务; BookHotelClient; 一定要先能成功的执行完毕, 另一个微服务; BookMuseumClient; 才会被调用。

That is, microservices; BookHotelClient; must execute successfully first; then, another microservice, BookMuseumClient, will be called.

而当微服务; BookMuseumClient; 执行失败的时候, 由微服务; BookHotelClient; 所产生的预订酒店的记录, 将会被删除。

And when microservices, BookMuseumClient, fails, the microservices, BookHotelClient; the resulting hotel reservation will be deleted.

在本文中, 我们将使用 AWS Step Functions 来实现一状态机 (State Machine); OrderWorkflow。

This article will use AWS Step Functions to implement a State Machine; OrderWorkflow。

这个由 AWS Step Functions 所实现的状态机; OrderWorkflow; 将以编排 (Orchestrate) 的方式, 来保证多个微服务之间协作的工作流 (workflow) 可如预期般的被执行:

AWS Step Functions implement this state machine; OrderWorkflow; Workflows that orchestrate to ensure that collaboration between multiple microservices can perform as expected:

  • 微服务; BookHotelClient; 一定要先能成功的执行完毕, 另一个微服务; BookMuseumClient; 才会被调用。
  • Microservices; BookHotelClient; Be sure to execute first successfully; another microservice, BookMuseumClient, will be called.
  • 当微服务; BookMuseumClient; 执行失败的时候, 由微服务; BookHotelClient; 所产生的预订酒店的记录, 将会被删除。
  • When microservices; BookMuseumClient; fails, the microservice; BookHotelClient; The resulting hotel reservation history will be deleted

微服务; BookHotelClient; 的输入与输出, 如下图所示:

Microservices; BookHotelClient; The inputs and outputs are in the following figure:

微服务; BookHotelClient (Microservices; BookHotelClient)

微服务; BookMuseumClient; 的输入与输出, 如下图所示:

Microservices; BookMuseumClient; The inputs and outputs are in the following figure:

微服务; BookMuseumClient (Microservices; BookMuseumClient)

AWS Step Functions 所实现一状态机 (State Machine); OrderWorkflow; 的输入与输出, 如下图所示:

AWS Step Functions implements a State Machine; OrderWorkflow; The inputs and outputs are in the following figure:

状态机 (State Machine); OrderWorkflow

我们将使用 ResultPath, 使得状态机; OrderWorkflow; 的输出是由合并微服务; BookHotelClient、 BookMuseumClient; 的输出所构成的:

We will use ResultPath, which makes the state machine; OrderWorkflow; the output is composed of merged microservices; BookHotelClient、BookMuseumClient; the production of  :

  • “ResultPath”: “$.hotel”; 使得微服务; BookHotelClient; 的输出, 可以 “更新” 状态机; OrderWorkflow; 的输入的 “hotel” 节点内的 key-value。
  • “ResultPath”: “$.hotel;  makes microservices ;  BookHotelClient;  the output of the machine can be “updated” ; OrderWorkflow; the key-value within the input “hotel” node.
  • “ResultPath”: “$.museum”; 使得微服务; BookMuseumClient; 的输出, 可以 “更新” 状态机; OrderWorkflow; 的输入的 “museum” 节点内的 key-value。
  • “ResultPath”: “$.museum”; make microservices; BookMuseumClient;  the output of the machine can be “updated”; OrderWorkflow; The key-value within the input “museum” node.

状态机; OrderWorkflow; 的代码如下:

State machines; OrderWorkflow; The code is as follows:

{
  "Comment": "An OrderWorkflow example of the Amazon States Language using Task states",
  "StartAt": "BookHotelState",
  "States" : {
    "BookHotelState": {
     "Type": "Task",
     "Resource": "arn:aws:lambda:region:accountId:function:BookHotelClient:$LATEST",
     "Parameters": {
       "buyer_id.$": "$.purchase.buyer_id",
       "start_date.$": "$.hotel.start_date",
       "end_date.$": "$.hotel.end_date",
       "near.$": "$.museum.museum_name"
     },
      "ResultPath": "$.hotel",
      "Next": "BookMuseumState"
   },
   "BookMuseumState": {
     "Type": "Task",
     "Resource": "arn:aws:lambda:region:accountId:function:BookMuseumClient:$LATEST",
     "Parameters": {
       "buyer_id.$": "$.purchase.buyer_id",
       "museum_name.$": "$.museum.museum_name",
       "when.$": "$.museum.when"
     },
     "ResultPath": "$.museum",
     "End": true
   }
  }
}

状态机; OrderWorkflow; 的状态图如下:

State machines; OrderWorkflow; The status diagram is as follows:

状态机; OrderWorkflow (State machines; OrderWorkflow)

 

 

状态机; OrderWorkflow; 的源代码:

State machines; OrderWorkflow; Source code :

https://github.com/KenFang/Order_Workflow_Step_Function/blob/master/OrderWorkflowState.json

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据