|
| 1 | +# Dapr Spring Boot and Testcontainers integration Example |
| 2 | + |
| 3 | +This example consists of two applications: |
| 4 | +- Producer App: |
| 5 | + - Publish messages using a Spring Messaging approach |
| 6 | + - Store and retrieve information using Spring Data CrudRepository |
| 7 | + - Implements a Workflow with Dapr Workflows |
| 8 | +- Consumer App: |
| 9 | + - Subscribe to messages |
| 10 | + |
| 11 | +## Running these examples from source code |
| 12 | + |
| 13 | +To run these examples you will need: |
| 14 | +- Java SDK |
| 15 | +- Maven |
| 16 | +- Docker or a container runtime such as Podman |
| 17 | + |
| 18 | +From the `spring-boot-examples/` directory you can start each service using the test configuration that uses |
| 19 | +[Testcontainers](https://testcontainers.com) to boostrap [Dapr](https://dapr.io) by running the following command: |
| 20 | + |
| 21 | +<!-- STEP |
| 22 | +name: Run Demo Producer Service |
| 23 | +match_order: none |
| 24 | +output_match_mode: substring |
| 25 | +expected_stdout_lines: |
| 26 | +- 'Started ProducerApplication' |
| 27 | +background: true |
| 28 | +expected_return_code: 143 |
| 29 | +sleep: 30 |
| 30 | +timeout_seconds: 45 |
| 31 | +--> |
| 32 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 33 | + |
| 34 | +```sh |
| 35 | +cd producer-app/ |
| 36 | +../../mvnw -Dspring-boot.run.arguments="--reuse=true" spring-boot:test-run |
| 37 | +``` |
| 38 | + |
| 39 | +<!-- END_STEP --> |
| 40 | + |
| 41 | +This will start the `producer-app` with Dapr services and the infrastructure needed by the application to run, |
| 42 | +in this case RabbitMQ and PostgreSQL. The `producer-app` starts on port `8080` by default. |
| 43 | + |
| 44 | +The `-Dspring-boot.run.arguments="--reuse=true"` flag helps the application to connect to an existing shared |
| 45 | +infrastructure if it already exists. For development purposes, and to connect both applications we will set the flag |
| 46 | +in both. For more details check the `DaprTestContainersConfig.java` classes in both, the `producer-app` and the `consumer-app`. |
| 47 | + |
| 48 | +Then run in a different terminal: |
| 49 | + |
| 50 | +<!-- STEP |
| 51 | +name: Run Demo Consumer Service |
| 52 | +match_order: none |
| 53 | +output_match_mode: substring |
| 54 | +expected_stdout_lines: |
| 55 | +- 'Started ConsumerApplication' |
| 56 | +background: true |
| 57 | +expected_return_code: 143 |
| 58 | +sleep: 30 |
| 59 | +timeout_seconds: 45 |
| 60 | +--> |
| 61 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 62 | + |
| 63 | +```sh |
| 64 | +cd consumer-app/ |
| 65 | +../../mvnw -Dspring-boot.run.arguments="--reuse=true" spring-boot:test-run |
| 66 | +``` |
| 67 | + |
| 68 | +<!-- END_STEP --> |
| 69 | +The `consumer-app` starts in port `8081` by default. |
| 70 | + |
| 71 | +## Interacting with the applications |
| 72 | + |
| 73 | +Now that both applications are up you can place an order by sending a POST request to `:8080/orders/` |
| 74 | +You can use `curl` to send a POST request to the `producer-app`: |
| 75 | + |
| 76 | + |
| 77 | +<!-- STEP |
| 78 | +name: Send POST request to Producer App |
| 79 | +match_order: none |
| 80 | +output_match_mode: substring |
| 81 | +expected_stdout_lines: |
| 82 | +- 'Order Stored and Event Published' |
| 83 | +background: true |
| 84 | +sleep: 1 |
| 85 | +timeout_seconds: 2 |
| 86 | +--> |
| 87 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 88 | + |
| 89 | +```sh |
| 90 | +curl -X POST localhost:8080/orders -H 'Content-Type: application/json' -d '{ "item": "the mars volta EP", "amount": 1 }' |
| 91 | +``` |
| 92 | + |
| 93 | +<!-- END_STEP --> |
| 94 | + |
| 95 | + |
| 96 | +If you check the `producer-app` logs you should see the following lines: |
| 97 | + |
| 98 | +```bash |
| 99 | +... |
| 100 | +Storing Order: Order{id='null', item='the mars volta EP', amount=1} |
| 101 | +Publishing Order Event: Order{id='d4f8ea15-b774-441e-bcd2-7a4208a80bec', item='the mars volta EP', amount=1} |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +If you check the `consumer-app` logs you should see the following lines, showing that the message |
| 106 | +published by the `producer-app` was correctly consumed by the `consumer-app`: |
| 107 | + |
| 108 | +```bash |
| 109 | +Order Event Received: Order{id='d4f8ea15-b774-441e-bcd2-7a4208a80bec', item='the mars volta EP', amount=1} |
| 110 | +``` |
| 111 | + |
| 112 | +Next, you can create a new customer to trigger the customer's tracking workflow: |
| 113 | + |
| 114 | +<!-- STEP |
| 115 | +name: Start Customer Workflow |
| 116 | +match_order: none |
| 117 | +output_match_mode: substring |
| 118 | +expected_stdout_lines: |
| 119 | +- 'New Workflow Instance created for Customer' |
| 120 | +background: true |
| 121 | +sleep: 1 |
| 122 | +timeout_seconds: 2 |
| 123 | +--> |
| 124 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 125 | + |
| 126 | +```sh |
| 127 | +curl -X POST localhost:8080/customers -H 'Content-Type: application/json' -d '{ "customerName": "salaboy" }' |
| 128 | +``` |
| 129 | + |
| 130 | +<!-- END_STEP --> |
| 131 | + |
| 132 | + |
| 133 | +A new Workflow Instance was created to track the customers interactions. Now, the workflow instance |
| 134 | +is waiting for the customer to request a follow-up. |
| 135 | + |
| 136 | +You should see in the `producer-app` logs: |
| 137 | + |
| 138 | +```bash |
| 139 | +Workflow instance <Workflow Instance Id> started |
| 140 | +Let's register the customer: salaboy |
| 141 | +Customer: salaboy registered. |
| 142 | +Let's wait for the customer: salaboy to request a follow up. |
| 143 | +``` |
| 144 | + |
| 145 | +Send an event simulating the customer request for a follow-up: |
| 146 | + |
| 147 | +<!-- STEP |
| 148 | +name: Emit Customer Follow-up event |
| 149 | +match_order: none |
| 150 | +output_match_mode: substring |
| 151 | +expected_stdout_lines: |
| 152 | +- 'Customer Follow-up requested' |
| 153 | +background: true |
| 154 | +sleep: 1 |
| 155 | +timeout_seconds: 5 |
| 156 | +--> |
| 157 | +<!-- Timeout for above service must be more than sleep + timeout for the client--> |
| 158 | + |
| 159 | +```sh |
| 160 | +curl -X POST localhost:8080/customers/followup -H 'Content-Type: application/json' -d '{ "customerName": "salaboy" }' |
| 161 | +``` |
| 162 | + |
| 163 | +<!-- END_STEP --> |
| 164 | + |
| 165 | +In the `producer-app` logs you should see that the workflow instance id moved forward to the Customer Follow Up activity: |
| 166 | + |
| 167 | +```bash |
| 168 | +Customer follow-up requested: salaboy |
| 169 | +Let's book a follow up for the customer: salaboy |
| 170 | +Customer: salaboy follow-up done. |
| 171 | +Congratulations the customer: salaboy is happy! |
| 172 | +``` |
| 173 | +
|
| 174 | +## Running on Kubernetes |
| 175 | +
|
| 176 | +You can run the same example on a Kubernetes cluster. [Check the Kubernetes tutorial here](kubernetes/README.md). |
0 commit comments