Expand description
Kafka producer and consumers for Autumn apps.
§Quick start
use autumn_plugin_kafka::{Consumer, HandlerError, KafkaPlugin, KafkaProducer, Message, Record};
use autumn_web::prelude::*;
#[derive(serde::Serialize, serde::Deserialize)]
struct Order {
id: u64,
}
#[post("/orders/{id}")]
async fn create(producer: KafkaProducer, Path(id): Path<u64>) -> AutumnResult<&'static str> {
producer
.send(Record::json("orders", &Order { id })?.with_key(id.to_string()))
.await?;
Ok("queued")
}
async fn on_order(msg: Message, _state: AppState) -> Result<(), HandlerError> {
let order: Order = msg.json()?;
tracing::info!(id = order.id, "order received");
Ok(())
}
#[autumn_web::main]
async fn main() {
autumn_web::app()
.plugin(
KafkaPlugin::new().consumer(
Consumer::new("orders", ["orders"])
.group_id("billing")
.handler(on_order)
.dead_letter_topic("orders.dlq"),
),
)
.routes(routes![create])
.run()
.await;
}§Parts
| Part | Function |
|---|---|
KafkaPlugin | Installs all parts. Reads [kafka] at startup. |
KafkaConfig | The [kafka] config section. |
KafkaProducer | Sends records. It is a handler argument. |
Consumer | A consumer with an async handler. |
MemoryBroker | An in-memory broker for tests. |
RdKafkaBackend | The default client, librdkafka. |
The plugin also adds the kafka health indicator and kafka_* metrics
to /actuator/prometheus.
§Delivery
Consumers are at-least-once. The plugin commits an offset only after the handler completes, or after the dead-letter send completes. A handler can get the same message again. Make sure that this is safe.
§Example
examples/orders.rs uses all parts. CI runs its self-check.
Structs§
- Consumer
- A consumer: topics, a group, and a handler.
- Consumer
Settings - Consumer settings. Maps to
[kafka.consumer]. - Consumer
Spec - The data that a backend needs to make one consumer.
- Delivery
- The position of a record after the broker accepted it.
- Health
Settings - Health indicator settings. Maps to
[kafka.health]. - Kafka
Config - Settings for the plugin. Maps to the
[kafka]section. - Kafka
Plugin - The Kafka plugin.
- Kafka
Producer - Sends records to Kafka. Clones share one client.
- Kafka
Runtime - A handle to the running plugin. Clones share one runtime.
- Memory
Broker - An in-memory broker. Use it in tests. It does not need Docker.
- Message
- A message received from Kafka.
- Producer
Settings - Producer settings. Maps to
[kafka.producer]. - RdKafka
Backend - The default backend. It uses
librdkafka. - Record
- A record to send to Kafka.
Enums§
- Client
Role - The kind of Kafka client that a property map is for.
- Kafka
Error - An error from the Kafka plugin.
Constants§
- DLQ_
HEADER_ CONSUMER - Dead-letter header: the consumer name.
- DLQ_
HEADER_ ERROR - Dead-letter header: the last handler error. Maximum 1024 bytes.
- DLQ_
HEADER_ OFFSET - Dead-letter header: the source offset, as decimal text.
- DLQ_
HEADER_ PARTITION - Dead-letter header: the source partition, as decimal text.
- DLQ_
HEADER_ TOPIC - Dead-letter header: the source topic.
- PLUGIN_
NAME - The plugin name.
Traits§
- Backend
- Makes producers and consumers.
- Consumer
Backend - Receives messages for one consumer group.
- Producer
Backend - Sends records.
Type Aliases§
- BoxFuture
- The future type of the backend traits.
An owned dynamically typed
Futurefor use in cases where you can’t statically type your result or need to add some indirection. - Handler
Error - The error type that a handler can return.
- Header
- A header name and value.