Configuration for Rust Client for Kafka
Configure each client with a HashMap<String, String> of configuration
properties, which you pass to ProducerConfig::new, ConsumerConfig::new,
or AdminClientConfig::new.
The client supports only SASL/PLAIN authentication. The client doesn’t support SASL/SCRAM, OAuth, or AWS IAM.
Example configuration
The following example shows a minimal configuration for a producer, consumer, and admin client connecting to Confluent Cloud with SASL/PLAIN authentication.
use std::collections::HashMap;
use confluent_kafka::admin::{AdminClient, AdminClientConfig};
use confluent_kafka::common::serialization::{StringDeserializer, StringSerializer};
use confluent_kafka::consumer::{ConsumerConfig, KafkaConsumer};
use confluent_kafka::producer::{KafkaProducer, ProducerConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut props: HashMap<String, String> = [
("bootstrap.servers", "pkc-xxxxx.us-east-1.aws.confluent.cloud:9092"),
("security.protocol", "SASL_SSL"),
("sasl.mechanism", "PLAIN"),
("sasl.jaas.config",
"org.apache.kafka.common.security.plain.PlainLoginModule required \
username='<api_key>' password='<api_secret>';"),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
// Producer
let producer = KafkaProducer::<String, String>::new(
ProducerConfig::new(&props)?,
StringSerializer::new(),
StringSerializer::new(),
)?;
// Admin client
let admin = AdminClient::create(AdminClientConfig::new(&props)?)?;
// Consumer: add the consumer group properties
props.insert("group.id".to_string(), "my-consumer-group".to_string());
props.insert("group.protocol".to_string(), "consumer".to_string());
let consumer = KafkaConsumer::<String, String>::new(
ConsumerConfig::new(&props)?,
StringDeserializer::new(),
StringDeserializer::new(),
)?;
// Use the clients here.
Ok(())
}
Common properties
The following properties apply to the producer, consumer, and admin client.
Property |
Default |
Description |
|---|---|---|
|
Required. Must be defined. |
A comma-separated list of host and port pairs that are the addresses of
the Kafka brokers. For example, |
|
|
How the client uses the DNS lookups of the bootstrap servers. With
|
|
|
What the client does when none of the brokers it knows is available.
With |
|
Empty string |
An optional identifier string to include in server-side request logging. Use this to identify the application producing or consuming messages in broker logs and metrics. |
|
|
The protocol used to communicate with brokers. Valid values are
|
|
Required. Set to |
The SASL mechanism used for authentication. The client supports only
|
|
Empty string |
Java Authentication and Authorization Service (JAAS) login context parameters for SASL connections, in the format used by the Java Client. Required when using SASL/PLAIN authentication. |
|
|
The maximum amount of time the client waits for a response to a request, in milliseconds. |
|
|
The recording level for metrics sensors. Set to |
|
|
The number of samples maintained in the rolling window for windowed
statistics such as averages and rates. Must be at least |
|
|
The window size for windowed statistics, in milliseconds. Must be at
least |
|
|
The initial time to wait before retrying a failed request, in
milliseconds. Backoff increases exponentially up to
|
|
|
The maximum time to wait between retries, in milliseconds. |
|
|
The initial time to wait before reconnecting to a broker after the
connection is closed, in milliseconds. Backoff increases exponentially
up to |
|
|
The maximum time to wait before reconnecting to a broker, in milliseconds. |
|
|
The time after which idle connections are closed, in milliseconds.
The consumer default is |
|
|
The maximum time before the client force-refreshes metadata, even if no partition leadership changes have occurred, in milliseconds. |
|
|
The time the client waits for a socket connection to be established, in milliseconds. If the connection isn’t established within this time, the client closes the connection and retries. |
|
|
The maximum connection setup timeout, in milliseconds. The timeout increases exponentially for each consecutive failure up to this ceiling. |
|
|
The size of the TCP send buffer, in bytes. Set to |
|
|
The size of the TCP receive buffer, in bytes. Set to |
TLS properties
The following properties configure TLS when security.protocol is SSL
or SASL_SSL, and apply to the producer, consumer, and admin client.
The client reads keys and certificates only in PEM format. It doesn’t support
JKS or PKCS12 stores, so convert them to PEM. It doesn’t support encrypted
private keys, so the ssl.key.password, ssl.keystore.password, and
ssl.truststore.password properties aren’t available.
If you set neither ssl.truststore.location nor
ssl.truststore.certificates, the client verifies the broker certificate
against Mozilla’s root certificates, which the webpki-roots Cargo
feature includes in the build.
Property |
Default |
Description |
|---|---|---|
|
None |
The path to a PEM file with the certificates of the certificate authorities that the client trusts. |
|
None |
The certificates of the certificate authorities that the client trusts,
as a PEM string. Use it instead of |
|
|
The format of the trust store. The only supported value is |
|
None |
The path to a PEM file with the client’s private key and certificate chain, for mutual TLS authentication. |
|
None |
The client’s private key, as a PEM string. Use it with
|
|
None |
The client’s certificate chain, as a PEM string. |
|
|
The format of the key store. The only supported value is |
|
|
The algorithm the client uses to verify that the broker host name matches the broker certificate. Set to an empty string to turn off host name verification. |
|
|
The TLS protocol versions the client can use. |
|
|
The TLS protocol whose default protocol versions and cipher suites
apply when |
|
None |
A comma-separated list of the cipher suites the client can use, in order of preference. Leave unset to use the default cipher suites. |
Producer properties
The following properties apply only to the producer (ProducerConfig).
Property |
Default |
Description |
|---|---|---|
|
|
The number of acknowledgments the producer requires before considering a
request complete. Valid values are |
|
|
The time the producer waits before sending a batch, in milliseconds. Increasing this value allows more records to accumulate in the same batch, which can improve throughput at the cost of latency. |
|
|
The maximum time the producer waits for a record to be acknowledged, including retries, in milliseconds. |
|
|
The maximum number of bytes to include in a single batch per partition. Larger values can improve throughput at the cost of memory. |
|
|
The total memory the producer uses to buffer records waiting to be sent,
in bytes. |
|
|
The maximum time |
|
|
The maximum size of a single produce request, in bytes. |
|
|
The maximum number of unacknowledged requests the producer sends to a
single broker before blocking. Set to |
|
|
The compression type for all data the producer generates. Valid values
are |
|
|
The number of times the producer retries sending a record after a
transient error. In practice, |
|
|
When |
|
|
The partitioner used for records that don’t specify a partition. Valid
values are |
|
None |
The transactional ID to use for atomic writes across partitions and
topics. Required to use the transactional producer API. When set,
|
|
|
The maximum lifetime of a transaction before the broker aborts it, in milliseconds. |
|
|
The time after which the producer drops cached metadata for topics that haven’t produced any records, in milliseconds. |
Consumer properties
The following properties apply only to the consumer (ConsumerConfig).
Property |
Default |
Description |
|---|---|---|
|
Must be defined. |
The consumer group the consumer belongs to. Consumers with the same
|
|
Required. Set to |
The rebalance protocol to use. The |
|
|
The offset-reset policy when there is no initial offset in Kafka or when
the current offset no longer exists. Valid values are |
|
|
When |
|
|
The maximum time between |
|
|
The maximum number of records returned by a single call to |
|
|
Has no effect, because the broker controls the session timeout with
|
|
|
The minimum amount of data the broker should return for a fetch request, in bytes. Increasing this value reduces the number of fetch requests at the cost of added latency. |
|
|
The maximum time the broker waits for |
|
|
The maximum number of bytes the server returns across all partitions for a single fetch request. |
|
|
The maximum number of bytes the server returns per partition in a fetch response. |
|
|
Has no effect, because the broker controls the heartbeat interval with
|
|
|
The interval at which the consumer automatically commits offsets, in
milliseconds. Only applies when |
|
|
Controls which transactional records the consumer reads. |
|
Empty string |
The rack of the consumer. The consumer sends it to the brokers so that it can fetch from the closest replica, when the brokers have a replica selector configured, and to the group coordinator for rack-aware partition assignment. |
|
|
When |
|
|
When |
|
|
When |
|
None |
A unique static identifier for this consumer instance. When set, the consumer uses static group membership and doesn’t trigger a rebalance on restart. |
|
None |
The name of the server-side assignor to use with the KIP-848 consumer group protocol. Leave unset to use the broker default. |
Admin client properties
The following properties apply only to the admin client (AdminClientConfig).
The admin client also accepts all common properties.
Property |
Default |
Description |
|---|---|---|
|
None |
A comma-separated list of host and port pairs that are the addresses of
the Kafka controllers. Use it instead of |
|
|
The default timeout for admin client operations that don’t specify a
per-call timeout, in milliseconds. Must be greater than or equal to
|
|
|
The maximum number of times the admin client retries a transient request failure before returning an error. |