kafka-example
Implementing producer/consumer for Kafka using Springboot
-
Download Kakfa 2.13-3.2.1 software
-
Unzip the file
tar -xvzf kafka_2.13-3.2.1.tgz
-
Go to the kafka folder
cd kafka_2.13-3.2.1
-
Run zookeeper
./bin/zookeeper-server-start.sh config/zookeeper.properties
-
Run Kafka server broker
./bin/kafka-server-start.sh config/server.properties
-
Create a Java project with Spring Boot, for instance, using maven
mvn archetype:generate \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DgroupId=mx.isccarrasco.kafka \ -DartifactId=kafka-sb-ms \ -DarchetypeVersion=0.0.1-SNAPSHOT \ -DinteractiveMode=false
or for the case of SpringBoot, it highly recommended to use the https://start.spring.io
-
Edit the
application.properties
file of the java project to assign the port of the kafka serverspring.kafka.bootstrap-servers=localhost:9092
-
Run consumer to read the information queued
./bin/kafka-console-consumer.sh --topic isccarrasco --from-beginning --bootstrap-server localhost:9092
-
There is also available a REST API to send or add data to the queue
http --json POST :8080/api/v1/messages message="api with kafka"