Saga API

对saga的Pos处理及请求

  1. POST /requests

描述

  • 根据JSON格式按顺序定义请求和恢复策略,按照下面的示例,将它们放到http body中。
  1. {
  2. "policy": "",
  3. "requests": [
  4. {
  5. "id": "",
  6. "type": "",
  7. "serviceName": "",
  8. "parents": [
  9. ],
  10. "transaction": {
  11. "method": "",
  12. "path": "",
  13. "params": {
  14. }
  15. },
  16. "compensation": {
  17. "method": "",
  18. "path": "",
  19. "params": {
  20. }
  21. }
  22. }
  23. ]
  24. }

JSON parameters:

  • policy - support BackwardRecovery or ForwardRecovery.
  • requests - transactions array.

    • id - request id. It should be unique among this collection of requests.
    • type - support rest for now.
    • serviceName - user-defined service name.
    • parents - request ids. It means this request is only executed after all requests in the parents field are completed.
    • transaction - user-defined transaction that executed by the Saga.
      • method - user-defined, HTTP method.
      • path - user-defined, HTTP path.
      • params - support form,json,body,query.
    • compensation - user-defined compensation that executed by the Saga.
      • method - user-defined, HTTP method.
      • path - user-defined, HTTP path.
      • params - support form,json,body,query.
  • 设置content type到 text/plain.

  • 发送他们到Saga service.

request的例子

  1. curl -XPOST -H "Content-Type: text/plain" -d @./request.json http://<docker.host.ip:saga.port>/requests

response的例子

  1. success

Status codes

  • 200 – no error
  • 400 – bad parameter
  • 500 – server error

获取全部Saga事件

  1. GET /events

秒偶数

获取全部Saga事件

request的例子

  1. curl -XGET http://<docker.host.ip:saga.port>/events

response的例子

  1. {
  2. "88658e73-eff5-4d31-887e-019201d6b560": [
  3. {
  4. "id": 1,
  5. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  6. "creationTime": "2017-09-15T01:15:40Z",
  7. "type": "SagaStartedEvent",
  8. "contentJson": "{\"policy\": \"BackwardRecovery\", \"requests\": [{\"id\": \"request-car\", \"type\": \"rest\", \"serviceName\": \"car-rental-service\", \"transaction\": {\"path\": \"/rentals\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/rentals\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}}, {\"id\": \"request-hotel\", \"type\": \"rest\", \"serviceName\": \"hotel-reservation-service\", \"transaction\": {\"path\": \"/reservations\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/reservations\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}}, {\"id\": \"request-flight\", \"type\": \"rest\", \"serviceName\": \"flight-booking-service\", \"transaction\": {\"path\": \"/bookings\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/bookings\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}}, {\"id\": \"request-payment\", \"type\": \"rest\", \"parents\": [\"request-car\", \"request-flight\", \"request-hotel\"], \"serviceName\": \"payment-service\", \"transaction\": {\"path\": \"/payments\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/payments\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}}]}"
  9. },
  10. {
  11. "id": 2,
  12. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  13. "creationTime": "2017-09-15T01:15:40Z",
  14. "type": "TransactionStartedEvent",
  15. "contentJson": "{\"id\": \"request-flight\", \"type\": \"rest\", \"parents\": [], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"flight-booking-service\", \"transaction\": {\"path\": \"/bookings\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/bookings\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}"
  16. },
  17. {
  18. "id": 3,
  19. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  20. "creationTime": "2017-09-15T01:15:40Z",
  21. "type": "TransactionStartedEvent",
  22. "contentJson": "{\"id\": \"request-car\", \"type\": \"rest\", \"parents\": [], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"car-rental-service\", \"transaction\": {\"path\": \"/rentals\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/rentals\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}"
  23. },
  24. {
  25. "id": 4,
  26. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  27. "creationTime": "2017-09-15T01:15:40Z",
  28. "type": "TransactionStartedEvent",
  29. "contentJson": "{\"id\": \"request-hotel\", \"type\": \"rest\", \"parents\": [], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"hotel-reservation-service\", \"transaction\": {\"path\": \"/reservations\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/reservations\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}"
  30. },
  31. {
  32. "id": 5,
  33. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  34. "creationTime": "2017-09-15T01:15:40Z",
  35. "type": "TransactionEndedEvent",
  36. "contentJson": "{\"request\": {\"id\": \"request-flight\", \"type\": \"rest\", \"parents\": [], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"flight-booking-service\", \"transaction\": {\"path\": \"/bookings\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/bookings\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}, \"response\": {\"body\": \"{\\n \\\"statusCode\\\": 200,\\n \\\"content\\\": \\\"Flight booked with id 5b3c462a-b5d4-45b8-b5e4-8c9aa7d1c069 for customer mike\\\"\\n}\"}}"
  37. },
  38. {
  39. "id": 6,
  40. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  41. "creationTime": "2017-09-15T01:15:40Z",
  42. "type": "TransactionEndedEvent",
  43. "contentJson": "{\"request\": {\"id\": \"request-hotel\", \"type\": \"rest\", \"parents\": [], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"hotel-reservation-service\", \"transaction\": {\"path\": \"/reservations\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/reservations\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}, \"response\": {\"body\": \"{\\n \\\"statusCode\\\": 200,\\n \\\"content\\\": \\\"Hotel reserved with id eb2366e1-411d-4352-84fb-6b5ab446ec81 for customer mike\\\"\\n}\"}}"
  44. },
  45. {
  46. "id": 7,
  47. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  48. "creationTime": "2017-09-15T01:15:41Z",
  49. "type": "TransactionEndedEvent",
  50. "contentJson": "{\"request\": {\"id\": \"request-car\", \"type\": \"rest\", \"parents\": [], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"car-rental-service\", \"transaction\": {\"path\": \"/rentals\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/rentals\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}, \"response\": {\"body\": \"{\\n \\\"statusCode\\\": 200,\\n \\\"content\\\": \\\"Car rented with id 3c22da64-d4ac-4870-b9bb-54b603721925 for customer mike\\\"\\n}\"}}"
  51. },
  52. {
  53. "id": 8,
  54. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  55. "creationTime": "2017-09-15T01:15:41Z",
  56. "type": "TransactionStartedEvent",
  57. "contentJson": "{\"id\": \"request-payment\", \"type\": \"rest\", \"parents\": [\"request-car\", \"request-flight\", \"request-hotel\"], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"payment-service\", \"transaction\": {\"path\": \"/payments\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/payments\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}"
  58. },
  59. {
  60. "id": 9,
  61. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  62. "creationTime": "2017-09-15T01:15:41Z",
  63. "type": "TransactionEndedEvent",
  64. "contentJson": "{\"request\": {\"id\": \"request-payment\", \"type\": \"rest\", \"parents\": [\"request-car\", \"request-flight\", \"request-hotel\"], \"fallback\": {\"type\": \"NOP\"}, \"serviceName\": \"payment-service\", \"transaction\": {\"path\": \"/payments\", \"method\": \"post\", \"params\": {\"form\": {\"customerId\": \"mike\"}}}, \"compensation\": {\"path\": \"/payments\", \"method\": \"put\", \"params\": {\"form\": {\"customerId\": \"mike\"}}, \"retries\": 3}}, \"response\": {\"body\": \"{\\n \\\"statusCode\\\": 200,\\n \\\"content\\\": \\\"Payment made for customer mike and remaining balance is 200\\\"\\n}\"}}"
  65. },
  66. {
  67. "id": 10,
  68. "sagaId": "88658e73-eff5-4d31-887e-019201d6b560",
  69. "creationTime": "2017-09-15T01:15:41Z",
  70. "type": "SagaEndedEvent",
  71. "contentJson": "{}"
  72. }
  73. ]
  74. }

Status codes

  • 200 – no error