Skip to content

Latest commit

 

History

History
 
 

scs-099

SCS-099

Producer Consumer

A simple example for an Event Driven Flow by the help of SPRING CLOUD STREAM KAFKA

General Flow Diagram

properties
  • java.version: 11
  • spring-cloud.version: Hoxton.SR12 (To get Advantage of Binders @Input,@Output)
  • spring-boot.version: 2.4.13

Documentation

Please visit Spring Cloud Stream Kafka (Part 1) for Project documentation

The Docker-compose file contains: single kafka and zookeeper. just simply run the following command

docker-compose up -d

or

docker-compose -f ./kafka-cluster.yml -f ./control-center-ui.yml up

To stop the brokers and the Control Center UI run the following command:

docker-compose -f ./kafka-cluster.yml -f ./control-center-ui.yml down

Note: docker-compose file is at the root of this project "/scs-kafka-intro"

Build the project

Run the First Test:

mvn clean package

Run the Application

Then run the generated jar file in target folder, (so make sure you are in the same directory when you run the jar file or give the full path)

java -jar target/scs-099-0.0.1-SNAPSHOT.jar

The application starts to listen on port 8080. make sure that port is not occupied by any other app already, if is try to pass the following parameter before -jar by adding --server.port=8081

Basically in this test, Producer adding 10 messages into kafka topic every 5 sec.

Single Producer and Single Consumer

mvn spring-boot:run -Dspring.profiles.active=test2 

or

java --spring.profiles.active=test2 -jar target/scs-099-0.0.1-SNAPSHOT.jar

General Flow Diagram

Single Producer and Single Consumer with 3 Thread

mvn spring-boot:run  -Dspring.profiles.active=test3 

or

java --spring.profiles.active=test3 -jar target/scs-099-0.0.1-SNAPSHOT.jar

General Flow Diagram

Single Producer and 3 Consumer App (3 separate JVM process)

Run the following codes in 3 different terminal

on Terminal-1: (this app has one Producer and one consumer)

mvn spring-boot:run  -Dspring.profiles.active=test2

or

java --spring.profiles.active=test2 -jar target/scs-099-0.0.1-SNAPSHOT.jar

on Terminal-2: (this app has only one consumer)

mvn spring-boot:run   -Dspring.profiles.active=test2 -Dserver.port=8081

or

java --spring.profiles.active=test2 --server.port=8081  -jar target/scs-099-0.0.1-SNAPSHOT.jar

on Terminal-3: (this app has only one consumer)

mvn spring-boot:run -Dspring.profiles.active=test2 -Dserver.port=8082

or

java --spring.profiles.active=test2 --server.port=8082  -jar target/scs-099-0.0.1-SNAPSHOT.jar

General Flow Diagram

Please visit Spring Cloud Stream Kafka (Part 1) for Project documentation

Testing Spring Cloud Stream Applications

Spring Cloud Stream Polled Consumer with Embedded Kafka Broker Sample