The goal of this project is to play with Spring Cloud Stream Event Routing
and CloudEvents
. For it, we will implement a producer and consumer of news
& alert
events.
On ivangfr.github.io, I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.
- [Medium] How to Route CloudEvents messages with Spring Cloud Stream
- [Medium] Implementing a Kafka Producer and Consumer using Spring Cloud Stream
- [Medium] Implementing Unit Tests for a Kafka Producer and Consumer that uses Spring Cloud Stream
- [Medium] Implementing End-to-End testing for a Kafka Producer and Consumer that uses Spring Cloud Stream
- [Medium] Configuring Distributed Tracing with Zipkin in a Kafka Producer and Consumer that uses Spring Cloud Stream
- [Medium] Using Cloudevents in a Kafka Producer and Consumer that uses Spring Cloud Stream
-
Spring Boot
application that exposes a REST API to submitnews
&alert
events.Endpoints
POST /api/news/cnn {"title":"..."} POST /api/news/dw {"titel":"..."} POST /api/news/rai {"titolo":"..."} POST /api/alerts/earthquake {"richterScale":"...", "epicenterLat":"...", "epicenterLon":"..."} POST /api/alerts/weather {"message":"..."}
-
Spring Boot
application that consumes thenews
&alert
events published byproducer-service
.
-
Open a terminal and inside
spring-cloud-stream-event-routing-cloudevents
root folder rundocker compose up -d
-
Wait for Docker containers to be up and running. To check it, run
docker compose ps
-
producer-service
- In a terminal, make sure you are in
spring-cloud-stream-event-routing-cloudevents
root folder - Run the command below to start the application
./mvnw clean spring-boot:run --projects producer-service
- In a terminal, make sure you are in
-
consumer-service
- Open a new terminal and navigate to
spring-cloud-stream-event-routing-cloudevents
root folder - Run the command below to start the application
./mvnw clean spring-boot:run --projects consumer-service
- Open a new terminal and navigate to
-
- In a terminal, make sure you are inside
spring-cloud-stream-event-routing-cloudevents
root folder - Run the following script to build the Docker images
- JVM
./docker-build.sh
- Native
./docker-build.sh native
- JVM
- In a terminal, make sure you are inside
-
-
producer-service
Environment Variable Description KAFKA_HOST
Specify host of the Kafka
message broker to use (defaultlocalhost
)KAFKA_PORT
Specify port of the Kafka
message broker to use (default29092
) -
consumer-service
Environment Variable Description KAFKA_HOST
Specify host of the Kafka
message broker to use (defaultlocalhost
)KAFKA_PORT
Specify port of the Kafka
message broker to use (default29092
)
-
-
-
producer-service
Run the following command in a terminal
docker run --rm --name producer-service -p 9080:9080 \ -e KAFKA_HOST=kafka -e KAFKA_PORT=9092 \ --network=spring-cloud-stream-event-routing-cloudevents_default \ ivanfranchin/producer-service:1.0.0
-
consumer-service
Open a new terminal and run the following command
docker run --rm --name consumer-service -p 9081:9081 \ -e KAFKA_HOST=kafka -e KAFKA_PORT=9092 \ --network=spring-cloud-stream-event-routing-cloudevents_default \ ivanfranchin/consumer-service:1.0.0
-
In a terminal, submit the following POST requests to producer-service
and check its logs and consumer-service
logs
Note: HTTPie is being used in the calls bellow
-
news
http :9080/api/news/cnn title="NYC subway strike" http :9080/api/news/dw titel="Berliner Untergrundstreik" http :9080/api/news/rai titolo="Sciopero della metropolitana di Roma"
-
alerts
http :9080/api/alerts/earthquake richterScale=5.5 epicenterLat=37.7840781 epicenterLon=-25.7977037 http :9080/api/alerts/weather message="Thunderstorm in Berlin"
-
Kafdrop
Kafdrop
can be accessed at http://localhost:9000
- To stop applications, go to the terminals where they are running and press
Ctrl+C
- To stop and remove docker compose containers, network and volumes, go to a terminal and, inside
spring-cloud-stream-event-routing-cloudevents
root folder, run the following commanddocker compose down -v
In a terminal, make sure you are inside spring-cloud-stream-event-routing-cloudevents
root folder
-
producer-service
./mvnw clean test --projects producer-service
-
consumer-service
./mvnw clean test --projects consumer-service
To remove the Docker images created by this project, go to a terminal and inside spring-cloud-stream-event-routing-cloudevents
root folder, run the following script
./remove-docker-images.sh