Apache Kafka examples
You should create .env file with IP address to run kafka
example .env
IP_ADDR=192.168.0.180You can get your ip address using command:
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2To setup cluster run:
docker-compose up -dThis command will create 3 Zookeeper instances and 3 Kafka instances.
To create example topic run:
docker exec -t <KafkaContainerID> \
kafka-topics --create \
--topic example \
--partitions 4 \
--replication-factor 2 \
--if-not-exists \
--zookeeper 192.168.0.180:32181To list topics run:
docker exec -t <KafkaContainerID> kafka-topics --list --zookeeper 192.168.0.180:32181You can use kafkacat tool to use kafka CLI:
brew install kafkacat #macos
apt install kafkacat #aptTo listen to partiotions run:
kafkacat -C -b localhost:19092,localhost:29092,localhost:39092 -t example -p 0To list consumer groups:
kafka-consumer-groups --list --bootstrap-server 192.168.0.180:19092-
Application uses golang gin for HTTP server creation and routing
-
default port:
8080 -
default topic:
example -
default partition:
0 -
endpoints
-
GET /messages- get all messages for default consumer groupexample-consumer-group -
GET /messages/:partition/:offset- get message on desired partition and offset -
POST /messages- create message- body:
{ "Key": "key", "Value": "value" }
-