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.
- Rust (
https://rustup.rs/) - Docker (
https://docs.docker.com/get-docker/) - Docker Compose (
https://docs.docker.com/compose/install/)
git clone https://github.com/ASoldo/kafka_rust.git
cd kafka_rustdocker-compose up -dThis command starts Kafka and Zookeeper services in Docker containers.
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"To run the consumer, use the following command:
cargo run --bin consumer || cargo run --bin consumer humanTo run the producer, use the following command:
cargo run --bin producerTo run the Actix API, use the following command:
cargo run --bin apiHere are the curl commands to test the CRUD operations:
- 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"}'- 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.
- 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.
- 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.