kafka_client
A pure Rust Kafka client library built on Tokio async runtime. Supports SASL authentication (PLAIN, SCRAM-SHA-256, SCRAM-SHA-512) and TLS encryption.
Features
- Pure Rust - No C bindings or external dependencies required
- Async/Await - Built on Tokio for modern async Rust
- SASL Authentication - Supports PLAIN, SCRAM-SHA-256, SCRAM-SHA-512
- TLS Encryption - Secure connections with configurable TLS
- Layered Architecture - Clean separation between transport, protocol, and API layers
- High-Level API - Easy-to-use Producer and Consumer interfaces
- Low-Level Access - Direct protocol access for advanced use cases
- Connection Pooling - Efficient broker connection management
- Metadata Caching - Automatic cluster metadata refresh
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
Basic Connection
use KafkaClient;
let client = builder
.with_client_id
.build
.await?;
// Query cluster metadata
let brokers = client.metadata.get_all_brokers.await;
println!;
Producer
use ;
use Bytes;
let client = builder
.build
.await?;
let producer = client.producer.await?;
// Send messages
let record = new;
let metadata = producer.send.await?;
println!;
// Ensure all messages are delivered
producer.flush.await?;
Consumer
use ;
let client = builder
.build
.await?;
let mut consumer = client.consumer;
consumer.subscribe.await?;
// Poll for messages
let records = consumer.poll.await?;
for record in records
SASL Authentication
use ;
// PLAIN authentication
let client = builder
.with_sasl
.build
.await?;
// SCRAM-SHA-256 authentication
let client = builder
.with_sasl
.build
.await?;
TLS Encryption
use ;
let tls = TlsConfig ;
// TLS only
let client = builder
.with_tls_config
.build
.await?;
// TLS + SASL
let client = builder
.with_sasl_tls
.build
.await?;
Architecture
The library uses a layered architecture for clean separation of concerns:
┌─────────────────────────────────────────┐
│ High-Level API Layer │
│ (Producer, Consumer, KafkaClient) │
├─────────────────────────────────────────┤
│ Cluster Layer │
│ (ClusterClient, BrokerManager, │
│ MetadataCache) │
├─────────────────────────────────────────┤
│ Connection Layer │
│ (Connection, ConnectionPool) │
├─────────────────────────────────────────┤
│ Protocol Layer │
│ (Wire Codec, Request/Response) │
├─────────────────────────────────────────┤
│ Transport Layer │
│ (TCP, TLS, SASL) │
└─────────────────────────────────────────┘
Examples
See the examples directory for complete working examples:
basic_connect.rs- Simple connection and metadata queryproduce_consume.rs- Complete produce/consume workflowsasl_auth.rs- SASL authentication with different mechanismstls_connect.rs- TLS encryption and TLS+SASLadmin_operations.rs- Topic management operationsraw_connection.rs- Low-level Connection API
Run an example:
# Basic connection
# SASL authentication
SASL_MECHANISM=SCRAM-SHA-256 \
SASL_USERNAME=user \
SASL_PASSWORD=pass \
# TLS connection
KAFKA_DOMAIN=kafka.example.com \
Documentation
Requirements
- Rust 1.85 or later
- Kafka 0.10.0 or later (supports Kafka 3.x+)
Testing
Integration tests require a running Kafka broker:
# Using Docker
# Run tests
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Author
- zzzdong - GitHub - kuwater@163.com