Now let's build the project with maven command : mvn clean install
https://github.com/dhanuka84/SAGA-Microservices-Zeebe/tree/main/src/zeebe-saga-spring-bootThen Build the docker images with maven spring-boot plugin:
mvn spring-boot:build-image
- To deploy Zeebe cluster, you have to go through same steps given in previous post.
Deploy MongoDB Cluster
MONGO_REPLICASET_HOST=mongo docker-compose -f src/zeebe-saga-spring-boot/docker/docker-compose-mongo.yaml up
Deploy Microservices
docker-compose -f src/zeebe-saga-spring-boot/docker/docker-compose-micros.yaml up
Test Microservices Health with Actuator ( status, liveness, readiness)
dhanuka@dhanuka:~$ curl http://localhost:8081/actuator/health | jq '. | {status: .status, liveness: .components.livenessState.status, readiness: .components.readinessState.status,}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 352 100 352 0 0 592 0 --:--:-- --:--:-- --:--:-- 593
{
"status": "UP",
"liveness": "UP",
"readiness": "UP"
}
dhanuka@dhanuka:~$ curl http://localhost:8083/actuator/health | jq '. | {status: .status, liveness: .components.livenessState.status, readiness: .components.readinessState.status,}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 352 100 352 0 0 824 0 --:--:-- --:--:-- --:--:-- 822
{
"status": "UP",
"liveness": "UP",
"readiness": "UP"
}
dhanuka@dhanuka:~$ curl http://localhost:8084/actuator/health | jq '. | {status: .status, liveness: .components.livenessState.status, readiness: .components.readinessState.status,}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 352 100 352 0 0 675 0 --:--:-- --:--:-- --:--:-- 675
{
"status": "UP",
"liveness": "UP",
"readiness": "UP"
}
dhanuka@dhanuka:~$ curl http://localhost:8082/actuator/health | jq '. | {status: .status, liveness: .components.livenessState.status, readiness: .components.readinessState.status,}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 352 100 352 0 0 820 0 --:--:-- --:--:-- --:--:-- 822
{
"status": "UP",
"liveness": "UP",
"readiness": "UP"
}
Deploy Zeebe BPMN Process
dhanuka@dhanuka:~$ zbctl deploy workflows/saga-example.bpmn --insecure
{
"key": "2251799813703663",
"processes": [
{
"bpmnProcessId": "trip-booking",
"version": 1,
"processDefinitionKey": "2251799813703662",
"resourceName": "workflows/saga-example.bpmn"
}
]
}
Create a booking via Rest Call to Booking Microservice
dhanuka@dhanuka:~$ curl --location --request POST 'http://localhost:8081/booking/' \
--header 'Content-Type: application/json' \
--data-raw '{
"id" : "0",
"clientId":"123",
"resourceId":"987",
"fromDate":"2021-02-22T14:52:44.494264+01:00",
"toDate":"2021-03-06T14:52:44.495451+01:00",
"createdAt":"2021-02-10T14:52:44.495469+01:00",
"active":false
}'
{"id":"0","clientId":"123","houseBookingId":"642c957b-f7ed-45d9-8719-b11fa451dbfa","carBookingId":"dbb44830-2cb6-4c02-ab5d-bf0850e69bb0","flightBookingId":"b573f7ca-cab9-48ab-9bdf-cc08e5e199ae","fromDate":"2021-02-22T13:52:44.494264Z","toDate":"2021-03-06T13:52:44.495451Z","createdAt":"2021-02-10T13:52:44.495469Z","active":false}
- Once you login to Zeebe dashboard via http://localhost:8080/ , you can select the process id (trip-booking) and then version, then you can select the process instance
- Then you can see the happy path of the work flow.
Java Code Explanation
1. Where is Zeebee Process Instance Created?
- As you can see, the instance was created in line 34.
Key Points:
Line 34 : Spring WebFlux, which provides reactive programming support for web applications
https://www.baeldung.com/spring-webflux
Line 37: Creating a variable called bookingResult with Booking data.
Line 40: Usage of Completable Futures.
Line 49: Save the Trip Booking entity when received hotel booking id, car booking id and flight booking id.
So all the operations within and outside the microservice will be asynchronous and reactive.
2. Zeebee Task polling/requesting for jobs, then execute and response back to broker.
- Let’s take Hotel Booking as an example.
Key Points:
Line 41,42,43 : Access job data.
Line 45 : Validate the job based on headers
Line 50: Access Booking Info, based on requestName (bookingResult) variable value, which was created when process instance creation.
Line 64: Create Hotel Booking and reactively update Zeebe with response.
- Note that now the resultName variable value of response is bookHotelResult.
Failure Path Testing
- You have to change the BPMN configuration to enable simulateError as below.
Deploy workflow process next version
dhanuka@dhanuka:~$ zbctl deploy workflows/saga-example.bpmn --insecure
{
"key": "2251799813703663",
"processes": [
{
"bpmnProcessId": "trip-booking",
"version": 2,
"processDefinitionKey": "2251799813703662",
"resourceName": "workflows/saga-example.bpmn"
}
]
}
Create a booking via Rest Call to Booking Microservice
dhanuka@dhanuka:~$ curl --location --request POST 'http://localhost:8081/booking/' \
--header 'Content-Type: application/json' \
--data-raw '{
"id" : "0",
"clientId":"123",
"resourceId":"987",
"fromDate":"2021-02-22T14:52:44.494264+01:00",
"toDate":"2021-03-06T14:52:44.495451+01:00",
"createdAt":"2021-02-10T14:52:44.495469+01:00",
"active":false
}'
- Now you can see the failure path of the workflow
- We failed the work flow from Flight Booking task.
- The failure will be propagated to other tasks which is polling on same instance_id .
No comments:
Post a Comment