Kafka在Java客户端的配置参数


生产者配置(ProducerConfig)

bootstrap.servers(Kafka地址列表)

用于建立与Kafka集群的初始连接host/port列表,客户端会使用Kafka司所有的服务器,无论配置的列表有多少。

此列表是用于发现集群服务器的初始主机,客户端可以通过初始主机查找到整个集群服务器,在配置的时候会配置多个,因有可能配置的某个服务器不可用的情况。

配置格式:host1:port1,host2:port2,….

无默认值,且必须配置

key.serializer(Key序列化)

Key值序列化类,需要实现接口org.apache.kafka.common.serialization.Serializer

value.serializer(Value序列化)

Value值序列化类,需要实现接口
org.apache.kafka.common.serialization.Serializer

connections.max.idle.ms

partitioner.class(分区器)

用来计算消息分送到哪个主题分区,需要实现org.apache.kafka.clients.producer.Partitioner 接口。

public interface Partitioner extends Configurable, Closeable {
   //获取当前分区
    int partition(String var1, Object var2, byte[] var3, Object var4, byte[] var5, Cluster var6);

    void close();
     //生产下一个分区
    default void onNewBatch(String topic, Cluster cluster, int prevPartition) {
    }
}

默认值:DefaultPartitioner

interceptor.classes(拦截器)

拦截器:在消息发送之前和发送之后分别进行拦截,可以实现批量修改、统计、添加信息。需要实现org.apache.kafka.clients.producer.ProducerInterceptor 接口。

public interface ProducerInterceptor<K, V> extends Configurable {
    // 消息发送前
    ProducerRecord<K, V> onSend(ProducerRecord<K, V> var1);
    // 消息应答前
    void onAcknowledgement(RecordMetadata var1, Exception var2);

    void close();
}

acks(分区多少副本收到消息)

指定Leader需要有多少个副本持久化之后,才能反馈成功。

  • acks=0 如果设置为0,生产者不需要等待任何服务端响应,记录添加到缓存区被视为已发送,无法保证消息是否发送成功,并且retries 配置会失效(因客户端不知道任何异常). 每条记录的 offset 始终为-1.
  • acks=1 只要Leader写入成功立刻返回,不能确定有多少副本写入成功。如果Leader写入成功之后在followers复制之前失败,那么记录将丢失。
  • acks=all 要求Leader同步等待所有副本复制成功之后反馈,在至少有一个存活的副本,就能确保消息记录不会被丢失。 效果等同于 acks=-1 设置.
默认值:1, 取值范围{"all", "-1", "0", "1"}

client.id

生成者客户ID,目的用来跟踪请求资源,在服务器端请求日志包含应客户ID和请求IP与端口号。

默认值:“producer-”+num;

max.in.flight.requests.per.connection(最大连接数)

同步请求最大连接数,即请求未收到响应的请求是阻塞的。
如果设置值大于1,如果发送失败,且开启了重试,则会导致消息重排的风险。

默认值:5,最小值为1。

retries(重试次数)

当服务器发生短暂性异常(即可自行恢复的).可以通过配置大于0的值,自动重新发送失败的消息。

如果 max.in.flight.requests.per.connection 设置的不是1。 可能会改变同一个分区的消息记录顺序,如果第一批次消息写入失败则会产生重试,但是第二批次消息写入成功,第二批次的消息会排序在第一批次消息之前。

如果配置了delivery.timeout.ms ,那么重试的总时长不能超过delivery.timeout.ms的配置时间,超过的部分请求都将置为无效或失败。

不推荐使用此配置,而应该用delivery.timeout.ms配置来控制重试行为。

retry.backoff.ms(重试间隔时间)

重试发送请求之间的等待时间,避免紧密重复的发送请求。

默认值:100

delivery.timeout.ms(传输超时时间)

消息发送成功或失败的总时间上限(包括重试发送的时间)。

配置值应大于request.timeout.mslinger.ms.之和。

默认值:120000

linger.ms(发送延迟)

生产者会将未发送的消息分组,进行批次发送消息(消息合并发送),已减少请求数量。

生产者不会立即发送消息,而是将消息进行分组(分批),等到配置的延迟时间在发送或是消息超过匹配大小就会立即发送。

功能类似于TCP中的Nagle算法。

默认值:0

request.timeout.ms(请求超时时间)

客户端等待请求响应的最长时间,如果在响应收到之前超时,需要时客户端会重新发送请求或在重试次数耗尽之后请求失败。

这个值应该大于 replica.lag.time.max.ms (broker 配置) ,已减少由于不必要的重试而导致消息重复的。

默认值:30000

batch.size(批大小)

将多条发送同一分区的数据,生成者会尝试将多条记录合并发送。这样有助于提高客户端和服务器的性能。

批次大小不能设置太小,这样增加发送次数减少吞吐量,但是批次不能设置过大,否则会让费内存,且请求时间会过长而导致超时等情况,因此设置一个合理的缓存区大小。

默认值:16384(byte)

send.buffer.bytes

发送数据缓冲区(SO_RCVBUF)大小,如果设置为-1,采用操作系统默认值。
默认值:131072,最小值 -1。

receive.buffer.bytes

接收数据缓冲区(SO_RCVBUF)大小,如果设置为-1,采用操作系统默认值。
默认值:32768,最小值 -1。

max.request.size(请求最大字节数)

请求最大字节数,这个设置会限制在单个请求中发送批记录的数量,以避免发送数据超大的请求。
这个未压缩批次数据最大上限大小。
默认值:1048576(byte)

reconnect.backoff.ms(重连间隔时间)

重新连接间隔时间,在第上次失败之后等待多少时间之后在发起连接,以免重复连接。

默认值:50,最小值0.

reconnect.backoff.max.ms(重新连接最大间隔时间)

重新连接失败的最大等待时间,在每次连接失败之后,重连间隔时间将呈指数增长,直至重新连接最大时间这个值。

默认值:1000

max.block.ms

The configuration controls how long KafkaProducer.send() and KafkaProducer.partitionsFor() will block.These methods can be blocked either because the buffer is full or metadata unavailable.Blocking in the user-supplied serializers or partitioner will not be counted against this timeout.

buffer.memory

The total bytes of memory the producer can use to buffer records waiting to be sent to the server. If records are sent faster than they can be delivered to the server the producer will block for max.block.ms after which it will throw an exception.

This setting should correspond roughly to the total memory the producer will use, but is not a hard bound since not all memory the producer uses is used for buffering. Some additional memory will be used for compression (if compression is enabled) as well as for maintaining in-flight requests.

默认值:33554432

compression.type(压缩方式)

生产者发送数据的压缩方式,默认值为 none (即不压缩). 取值范围none, gzip, snappy, lz4, or zstd.

默认值:none

metadata.max.age.ms(元数据更新最长时间)

元数据在通过一段时间之后,会强制刷新元数据,即使没有看到任何分区Leader任何变换,主动发现任何新的brokers(代理)或partitions(分区)

默认值:300000

metadata.max.idle.ms(元数据过期时间)

控制空闲生成者主题缓存元数据时间,如果自上次生成主题以后经过的是会就爱你超过了元数据空闲持续时间,则该主题的元数据将被遗忘,下次访问将强制执行元数据获取请求。
默认值:300000

metrics.sample.window.ms

metrics.num.samples

metrics.recording.level

metric.reporters

enable.idempotence

When set to ‘true’, the producer will ensure that exactly one copy of each message is written in the stream. If ‘false’, producer retries due to broker failures, etc., may write duplicates of the retried message in the stream. Note that enabling idempotence requires max.in.flight.requests.per.connection to be less than or equal to 5, retries to be greater than 0 and acks must be ‘all’. If these values are not explicitly set by the user, suitable values will be chosen. If incompatible values are set, a ConfigException will be thrown.

默认值:false;

transaction.timeout.ms

The maximum amount of time in ms that the transaction coordinator will wait for a transaction status update from the producer before proactively aborting the ongoing transaction.If this value is larger than the transaction.max.timeout.ms setting in the broker, the request will fail with a InvalidTransactionTimeout error.

默认值:60000

transactional.id

The TransactionalId to use for transactional delivery. This enables reliability semantics which span multiple producer sessions since it allows the client to guarantee that transactions using the same TransactionalId have been completed prior to starting any new transactions. If no TransactionalId is provided, then the producer is limited to idempotent delivery. Note that enable.idempotence must be enabled if a TransactionalId is configured. The default is null, which means transactions cannot be used. Note that, by default, transactions require a cluster of at least three brokers which is the recommended setting for production; for development you can change this, by adjusting broker setting transaction.state.log.replication.factor.

security.providers

A list of configurable creator classes each returning a provider implementing security algorithms. These classes should implement the org.apache.kafka.common.security.auth.SecurityProviderCreator interface.

client.dns.lookup(客户端DNS查找)

控制客户端如何使用DNS查找。

如果值为use_all_dns_ips ,查找一个host对应多个IP地址,在连接失败之前,会尝试所有连接。适用于独立或公有服务器。

如果值为resolve_canonical_bootstrap_servers_only,每个条目将被解析并展开为规范名称列表。

取值范围:
DEFAULT(“default”),
USE_ALL_DNS_IPS(“use_all_dns_ips”),
RESOLVE_CANONICAL_BOOTSTRAP_SERVERS_ONLY(“resolve_canonical_bootstrap_servers_only”);

默认值:DEFAULT

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值