Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kafka Rust Project

This project demonstrates how to produce and consume messages using Apache Kafka in a Rust application. The setup uses Docker Compose to run Kafka and Zookeeper.

Prerequisites

  • Rust (https://rustup.rs/)
  • Docker (https://docs.docker.com/get-docker/)
  • Docker Compose (https://docs.docker.com/compose/install/)

Setting Up the Project

1. Clone the Repository

git clone https://github.com/ASoldo/kafka_rust.git
cd kafka_rust

2. Build the Docker Containers

docker-compose up -d

This command starts Kafka and Zookeeper services in Docker containers.

3. Add Dependencies

Ensure your Cargo.toml file includes the following dependencies:

name = "kafka_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4.8.0"
futures = "0.3.30"
rdkafka = "0.36.2"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
tokio = { version = "1.38.1", features = ["full"] }
uuid = { version = "1.10.0", features = ["v4"]}
async-stream = "0.3.5"

Running the Consumer

To run the consumer, use the following command:

cargo run --bin consumer || cargo run --bin consumer human

Running the Producer

To run the producer, use the following command:

cargo run --bin producer

Running the Actix API

To run the Actix API, use the following command:

cargo run --bin api

Curl API Example:

Here are the curl commands to test the CRUD operations:

  1. Produce a Message To send a message to Kafka, use the following command:
curl -X POST http://127.0.0.1:8080/produce -H "Content-Type: application/json" -d '{"message": "Hello, Kafka!", "key": "some_key"}'
  1. Get a Message To retrieve a message by its ID:
curl -X GET http://127.0.0.1:8080/messages/<message_id>

Replace <message_id> with the actual message ID returned when you produced the message.

  1. Update a Message To update a message by its ID:
curl -X PUT http://127.0.0.1:8080/messages/<message_id> -H "Content-Type: application/json" -d '{"message": "Updated message", "key": "new_key"}'

Replace <message_id> with the actual message ID.

  1. Delete a Message To delete a message by its ID:
curl -X DELETE http://127.0.0.1:8080/messages/<message_id>

Replace <message_id> with the actual message ID.

About

Rust Kafka Demo

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages