Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add saga example #231

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ updates:
- "/openapi-contract-first"
- "/platform-http-security-keycloak"
- "/rest-json"
- "/saga"
- "/timer-log-kotlin"
- "/timer-log-main"
- "/timer-log"
Expand Down
2 changes: 1 addition & 1 deletion .github/generate-test-groups.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int groupId = 0

// Distribute example projects across a bounded set of test groups and output as JSON
new File(".").eachFileRecurse { file ->
if (file.getName() == "pom.xml") {
if (file.getName() == "pom.xml" && file.getParentFile().getParentFile().getName() == ".") {
if (GROUPS[groupId] == null) {
GROUPS[groupId] = [:]
GROUPS[groupId].name = "group-${String.format("%02d", groupId + 1)}"
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ nb-configuration.xml

# Quarkus
quarkus.log*

# Saga
payment.*
flight.*
train.*
app.*

2 changes: 1 addition & 1 deletion aws-lambda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<description>Camel Quarkus Example :: Deploying a Camel Route in AWS Lambda</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion cluster-leader-election/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<description>Camel Quarkus Example :: Cluster leader election</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion cxf-soap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<description>Camel Quarkus Example :: CXF SOAP</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
5 changes: 5 additions & 0 deletions docs/modules/ROOT/attachments/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
"description": "Demonstrates how to create a REST service using the Camel REST DSL and Jackson.",
"link": "https://github.com/apache/camel-quarkus-examples/tree/main/rest-json"
},
{
"title": "Saga and LRA",
"description": "Shows how to use saga and lra",
"link": "https://github.com/apache/camel-quarkus-examples/tree/main/saga"
},
{
"title": "Timer Hello World",
"description": "Uses the Camel timer component to output a Hello world message to the console",
Expand Down
2 changes: 1 addition & 1 deletion file-bindy-ftp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: File Bindy FTP</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion file-split-log-xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: File To Log XML DSL</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion health/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: Health Check</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion http-log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: HTTP to Log</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion jdbc-datasource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<name>Camel Quarkus :: Examples :: Jdbc - DatataSource - Log</name>
<description>Camel Quarkus Example :: Connect to Database using Datasource</description>
<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion jms-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<name>Camel Quarkus :: Examples :: JMS JPA</name>
<description>Camel Quarkus Example :: JMS JPA</description>
<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>
<quarkiverse-artemis.version>3.3.0</quarkiverse-artemis.version>

Expand Down
2 changes: 1 addition & 1 deletion jpa-idempotent-repository/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<description>Camel Quarkus Example :: JPA Idempotent Repository</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion jta-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<name>Camel Quarkus :: Examples :: JTA JPA</name>
<description>Camel Quarkus Example :: JTA JPA</description>
<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: Kafka</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion kamelet-chucknorris/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: Kamelet Chuck Norris</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<!-- TODO: https://github.com/apache/camel-quarkus/issues/3156 -->
Expand Down
2 changes: 1 addition & 1 deletion message-bridge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: Configure XA Transactions and connection pooling</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion observability/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<properties>

<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion openapi-contract-first/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: openapi-contract-first</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion platform-http-security-keycloak/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: Platform HTTP Security Keycloak</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
2 changes: 1 addition & 1 deletion rest-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<description>Camel Quarkus Example :: Rest Json</description>

<properties>
<quarkus.platform.version>3.11.0</quarkus.platform.version>
<quarkus.platform.version>3.12.0.CR1</quarkus.platform.version>
<camel-quarkus.platform.version>3.12.0-SNAPSHOT</camel-quarkus.platform.version>

<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down
170 changes: 170 additions & 0 deletions saga/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
= Saga and LRA: A Camel Quarkus example
:cq-example-description: An example that shows how to use saga and lra

{cq-description}

TIP: Check the https://camel.apache.org/camel-quarkus/latest/first-steps.html[Camel Quarkus User guide] for prerequisites
and other general information.

=== How it works

There are 4 services as participants of the Saga:

- payment-service: it emulates a real payment transaction and it will be used by both flight-service and train-service
- flight-service: it emulates the booking of a flight ticket and it uses the payment-service to execute a payment transaction
- train-service: it emulates the reservation of a train seat and it uses the payment-service to execute a payment transaction
- app: is the starting point and it emulates a user that starts the transaction to buy both flight and train tickets

The starting point is a REST endpoint that creates a request for a new reservation
and there is 15% probability that the payment service fails.

==== Logical view

image::doc-resources/logic-diagram.png[]

==== Compensating a failure

image::doc-resources/compesate-diagram.png[]

==== Technical view

image::doc-resources/tech-diagram.png[]

The communication between services and LRA coordinator (blue connectors) is via HTTP protocol,
so every service expose REST endpoints called by the LRA, moreover it calls LRA via REST endpoint

The communication between services (red connectors) is via AMQ broker (using OPENWIRE protocol),
implemented using Camel JMS component and RequestReply EIP
obtaining a synchronous behavior using asynchronous protocol

==== Analyzing logs

In the logs there will be all the messages about the execution of the service.

First the app starts the saga LRA, passing the id to the entry point REST

[source,shell]
----
curl -X POST http://localhost:8084/api/saga?id=1
----

in the log

[source]
----
Executing saga #1 with LRA http://localhost:8080/lra-coordinator/0_ffff7f000001_8aad_62d16f11_2
----

where the URL contains the id of the LRA and the number of the saga is the value of the parameter passed to the rest in the starting point

We're expecting that if the payment is ok, the message in the payment service will be:

[source]
----
Paying train for order #1

Payment train done for order #1 with payment transaction xxxxx

Payment flight done for order #1 with payment transaction xxxxx
----

the value of the payment transaction is the `JMSCorrelationID` used in the RequestReply EIP in the payment service

If the random failure occurs, the log in the payment service will be

[source]
----
Payment flight for saga #65 fails!

Payment for order #65 has been cancelled
----

It means that the compensation for the payment has been called, so we expect that in the flight service there will be a log

[source]
----
Flight purchase #65 has been cancelled due to payment failure
----

in the train service

[source]
----
Train purchase #65 has been cancelled due to payment failure
----

in the app

[source]
----
Transaction http://localhost:8080/lra-coordinator/0_ffff7f000001_8aad_62d16f11_74 has been cancelled due to flight or train failure
----


=== Package and run the application

Once you are done with developing you may want to package and run the application.

TIP: Find more details about the JVM mode and Native mode in the Package and run section of
https://camel.apache.org/camel-quarkus/latest/first-steps.html#_package_and_run_the_application[Camel Quarkus User guide]

==== External systems

Start Artemis:

[source, shell]
----
docker run --name artemis \
-e AMQ_USER=admin -e AMQ_PASSWORD=admin \
-d -p 61616:61616 \
quay.io/artemiscloud/activemq-artemis-broker
----

Start Narayana LRA Coordinator Server:

[source, shell]
----
docker run --network host -e QUARKUS_HTTP_PORT=8080 quay.io/jbosstm/lra-coordinator:latest
----

==== JVM mode

[source,shell]
----
$ mvn clean package
----

==== Native mode

IMPORTANT: Native mode requires having GraalVM and other tools installed. Please check the Prerequisites section
of https://camel.apache.org/camel-quarkus/latest/first-steps.html#_prerequisites[Camel Quarkus User guide].

To prepare a native executable using GraalVM, run the following command:

[source,shell]
----
$ mvn clean package -Pnative
----

==== Run script to execute services locally

[source,shell]
----
./run.sh
----

It will generate all the java processes and start in dev mode, logs are stored in the `.log` files, and process id in the `.pid` files

==== Run script to stop services locally

[source,shell]
----
./stop.sh
----

This command will kill the running processes, remove the PID files; the log files will be left


== Feedback

Please report bugs and propose improvements via https://github.com/apache/camel-quarkus/issues[GitHub issues of Camel Quarkus] project.
Binary file added saga/doc-resources/compesate-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added saga/doc-resources/logic-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added saga/doc-resources/tech-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading