Skip to content

Commit

Permalink
Fix apache#1304: fix the saga example to use default ports
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Mar 17, 2020
1 parent a2f9629 commit 4037ad2
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 229 deletions.
6 changes: 3 additions & 3 deletions examples/saga/Flight.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void configure() throws Exception {
restConfiguration().port("8080");

LRASagaService service = new LRASagaService();
service.setCoordinatorUrl("http://lra-coordinator:8080");
service.setLocalParticipantUrl("http://flight:8080");
service.setCoordinatorUrl("http://lra-coordinator");
service.setLocalParticipantUrl("http://flight");
getContext().addService(service);

rest("/api").post("/flight/buy")
Expand All @@ -39,7 +39,7 @@ public void configure() throws Exception {
.option("id", header("id"))
.compensation("direct:cancelPurchase")
.log("Buying flight #${header.id}")
.to("http://payment:8080/api/pay?bridgeEndpoint=true&type=flight")
.to("undertow:http://payment/api/pay?bridgeEndpoint=true&type=flight")
.log("Payment for flight #${header.id} done");

from("direct:cancelPurchase")
Expand Down
4 changes: 2 additions & 2 deletions examples/saga/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void configure() throws Exception {
restConfiguration().port("8080");

LRASagaService service = new LRASagaService();
service.setCoordinatorUrl("http://lra-coordinator:8080");
service.setLocalParticipantUrl("http://payment:8080");
service.setCoordinatorUrl("http://lra-coordinator");
service.setLocalParticipantUrl("http://payment");
getContext().addService(service);

rest("/api/").post("/pay")
Expand Down
46 changes: 36 additions & 10 deletions examples/saga/Readme.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
# Saga example
This example is from [camel-saga-quickstart](https://github.com/nicolaferraro/camel-saga-quickstart/) and could work with the camel-k.

* Start the lra-coordinator by using the openshift
This example is from [camel-saga-quickstart](https://github.com/nicolaferraro/camel-saga-quickstart/) adapted to work with camel-k.

Make sure Camel K is installed in your namespace, or execute the following command to install it:

```
oc create -f lra-coordinator.yaml
kamel install
```
* Or start the lra-coordinator by using the kubernetes

* Start the lra-coordinator by using the `oc` or `kubectl` tool:
```
kubectl create -f lra-coordiantor-kub.yaml
kubectl apply -f lra-coordinator.yaml
```

* Start the three demo services
```
kamel run -t container.service-port=8080 --dependency=camel-rest --dependency=camel-undertow --dependency=camel-lra examples/saga/Flight.java
kamel run -t container.service-port=8080 --dependency=camel-rest --dependency=camel-undertow --dependency=camel-lra examples/saga/Train.java
kamel run -t container.service-port=8080 --dependency=camel-rest --dependency=camel-undertow --dependency=camel-lra examples/saga/Payment.java
kamel run -d camel-undertow -d camel-lra Payment.java
kamel run -d camel-lra Flight.java
kamel run -d camel-lra Train.java
```

* Start the saga application
```
kamel run -t container.service-port=8080 --dependency=camel-rest --dependency=camel-undertow --dependency=camel-lra examples/saga/Saga.java
kamel run -d camel-lra -t service.auto=false Saga.java
```

Then you can use ```kamel logs saga``` to check the output of the transactions.

Focusing on one of the services, e.g. the flight service, you will notice that when unexpected events are found,
the operation is subsequently cancelled, e.g.:

E.g. running:
```
kamel logs flight
```

Possible workflow:
```
flight-7c8df48b88-6pzwt integration 2020-03-02 10:56:30.148 INFO [default-workqueue-2] route2 - Buying flight #18
flight-7c8df48b88-6pzwt integration 2020-03-02 10:56:30.165 ERROR [XNIO-1 I/O-1] DefaultErrorHandler - Failed delivery for (MessageId: ID-flight-7c8df48b88-6pzwt-1583146351094-0-106 on ExchangeId: ID-flight-7c8df48b88-6pzwt-1583146351094-0-105). Exhausted after delivery attempt: 1 caught: org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://payment/api/pay?bridgeEndpoint=true&type=flight with statusCode: 500
...
# after stacktrace
...
flight-7c8df48b88-6pzwt integration 2020-03-02 10:56:30.256 INFO [XNIO-2 task-6] route1 - Flight purchase #18 has been cancelled
flight-7c8df48b88-6pzwt integration 2020-03-02 10:56:35.150 INFO [default-workqueue-3] route2 - Buying flight #19
flight-7c8df48b88-6pzwt integration 2020-03-02 10:56:35.197 INFO [XNIO-1 I/O-1] route2 - Payment for flight #19 done
```
Then you can use ```kamel log saga``` to check the output of the transactions.
8 changes: 4 additions & 4 deletions examples/saga/Saga.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public void configure() throws Exception {
restConfiguration().port("8080");

LRASagaService service = new LRASagaService();
service.setCoordinatorUrl("http://lra-coordinator:8080");
service.setLocalParticipantUrl("http://saga:8080");
service.setCoordinatorUrl("http://lra-coordinator");
service.setLocalParticipantUrl("http://saga");
getContext().addService(service);

from("timer:clock?period=5s")
.saga()
.setHeader("id", header(Exchange.TIMER_COUNTER))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.log("Executing saga #${header.id}")
.to("http://train:8080/api/train/buy/seat")
.to("http://flight:8080/api/flight/buy");
.to("undertow:http://train/api/train/buy/seat")
.to("undertow:http://flight/api/flight/buy");

}
}
6 changes: 3 additions & 3 deletions examples/saga/Train.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void configure() throws Exception {
restConfiguration().port("8080");

LRASagaService service = new LRASagaService();
service.setCoordinatorUrl("http://lra-coordinator:8080");
service.setLocalParticipantUrl("http://train:8080");
service.setCoordinatorUrl("http://lra-coordinator");
service.setLocalParticipantUrl("http://train");
getContext().addService(service);

rest("/api/").post("/train/buy/seat")
Expand All @@ -39,7 +39,7 @@ public void configure() throws Exception {
.option("id", header("id"))
.compensation("direct:cancelPurchase")
.log("Buying train seat #${header.id}")
.to("http://payment:8080/api/pay?bridgeEndpoint=true&type=train")
.to("undertow:http://payment/api/pay?bridgeEndpoint=true&type=train")
.log("Payment for train #${header.id} done");

from("direct:cancelPurchase")
Expand Down
98 changes: 0 additions & 98 deletions examples/saga/lra-coordinator-kub.yaml

This file was deleted.

Loading

0 comments on commit 4037ad2

Please sign in to comment.