Skip to content

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.

Notifications You must be signed in to change notification settings

ivangfr/spring-cloud-stream-event-routing-cloudevents

Repository files navigation

spring-cloud-stream-event-routing-cloudevents

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.

Proof-of-Concepts & Articles

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.

Additional Readings

Project Diagram

project-diagram

Applications

  • producer-service

    Spring Boot application that exposes a REST API to submit news & 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":"..."}
    
  • consumer-service

    Spring Boot application that consumes the news & alert events published by producer-service.

Prerequisites

Start Environment

  • Open a terminal and inside the spring-cloud-stream-event-routing-cloudevents root folder run:

    docker compose up -d
    
  • Wait for Docker containers to be up and running. To check it, run:

    docker ps -a
    

Running Applications with Maven

  • producer-service

    • In a terminal, make sure you are in the spring-cloud-stream-event-routing-cloudevents root folder;
    • Run the command below to start the application:
      ./mvnw clean spring-boot:run --projects producer-service
      
  • consumer-service

    • Open a new terminal and navigate to the spring-cloud-stream-event-routing-cloudevents root folder
    • Run the following command to start the application:
      ./mvnw clean spring-boot:run --projects consumer-service
      

Running Applications as Docker containers

  • Build Docker Images

    • In a terminal, make sure you are inside the spring-cloud-stream-event-routing-cloudevents root folder;
    • Run the following script to build the Docker images:
      • JVM
        ./build-docker-images.sh
        
      • Native
        ./build-docker-images.sh native
        
  • Environment Variables

    • producer-service

      Environment Variable Description
      KAFKA_HOST Specify host of the Kafka message broker to use (default localhost)
      KAFKA_PORT Specify port of the Kafka message broker to use (default 29092)
    • consumer-service

      Environment Variable Description
      KAFKA_HOST Specify host of the Kafka message broker to use (default localhost)
      KAFKA_PORT Specify port of the Kafka message broker to use (default 29092)
  • Run Docker Containers

    • 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
      

Playing around

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"
    

Useful links

Shutdown

  • 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 the spring-cloud-stream-event-routing-cloudevents root folder, run the following command:
    docker compose down -v
    

Running Test Cases

In a terminal, make sure you are inside the spring-cloud-stream-event-routing-cloudevents root folder:

  • producer-service

    ./mvnw clean test --projects producer-service
    
  • consumer-service

    ./mvnw clean test --projects consumer-service
    

Cleanup

To remove the Docker images created by this project, go to a terminal and inside the spring-cloud-stream-event-routing-cloudevents root folder, run the following script:

./remove-docker-images.sh

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published