使用KafkaConsumer的API不能消费消息

HL 发表于: 2019-05-30   最后更新时间: 2019-05-30 23:36:53   2,626 游览

提问说明

使用命令行发布消息,使用api消费消息失败。使用命令行能正常消费。

消费者代码如下:

Properties props = new Properties();
props.setProperty("bootstrap.servers", "localhost:9092");
props.setProperty("group.id", "test");
props.setProperty("enable.auto.commit", "true");
props.setProperty("auto.commit.interval.ms", "1000");
props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("test"));
while (true) {
    ConsumerRecords<String, String> records = consumer.poll(100);
    for (ConsumerRecord<String, String> record : records)
        System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}

贴上报错信息

[main] INFO  2019-05-30 17:02:35.286 o.a.k.c.c.ConsumerConfig[238] - ConsumerConfig values: 
    auto.commit.interval.ms = 1000
    auto.offset.reset = latest
    bootstrap.servers = [localhost:9092]
    check.crcs = true
    client.id = 
    connections.max.idle.ms = 540000
    enable.auto.commit = true
    exclude.internal.topics = true
    fetch.max.bytes = 52428800
    fetch.max.wait.ms = 500
    fetch.min.bytes = 1
    group.id = test
    heartbeat.interval.ms = 3000
    interceptor.classes = null
    internal.leave.group.on.close = true
    isolation.level = read_uncommitted
    key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
    max.partition.fetch.bytes = 1048576
    max.poll.interval.ms = 300000
    max.poll.records = 500
    metadata.max.age.ms = 300000
    metric.reporters = []
    metrics.num.samples = 2
    metrics.recording.level = INFO
    metrics.sample.window.ms = 30000
    partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
    receive.buffer.bytes = 65536
    reconnect.backoff.max.ms = 1000
    reconnect.backoff.ms = 50
    request.timeout.ms = 305000
    retry.backoff.ms = 100
    sasl.jaas.config = null
    sasl.kerberos.kinit.cmd = /usr/bin/kinit
    sasl.kerberos.min.time.before.relogin = 60000
    sasl.kerberos.service.name = null
    sasl.kerberos.ticket.renew.jitter = 0.05
    sasl.kerberos.ticket.renew.window.factor = 0.8
    sasl.mechanism = GSSAPI
    security.protocol = PLAINTEXT
    send.buffer.bytes = 131072
    session.timeout.ms = 10000
    ssl.cipher.suites = null
    ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
    ssl.endpoint.identification.algorithm = null
    ssl.key.password = null
    ssl.keymanager.algorithm = SunX509
    ssl.keystore.location = null
    ssl.keystore.password = null
    ssl.keystore.type = JKS
    ssl.protocol = TLS
    ssl.provider = null
    ssl.secure.random.implementation = null
    ssl.trustmanager.algorithm = PKIX
    ssl.truststore.location = null
    ssl.truststore.password = null
    ssl.truststore.type = JKS
    value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer

 [main] INFO  2019-05-30 17:02:36.434 o.a.k.c.u.AppInfoParser[109] - Kafka version : 1.0.1
 [main] INFO  2019-05-30 17:02:36.434 o.a.k.c.u.AppInfoParser[110] - Kafka commitId : c0518aa65f25317e
 [main] INFO  2019-05-30 17:02:36.789 o.a.k.c.c.i.AbstractCoordinator[341] - [Consumer clientId=consumer-1, groupId=test] Discovered group coordinator DESKTOP-2LPADH2.localdomain:9092 (id: 2147483647 rack: null)
 [main] INFO  2019-05-30 17:02:36.832 o.a.k.c.c.i.AbstractCoordinator[341] - [Consumer clientId=consumer-1, groupId=test] Marking the coordinator DESKTOP-2LPADH2.localdomain:9092 (id: 2147483647 rack: null) dead
发表于 2019-05-30
HL
添加评论

问题已解决,配置文件中此配置默认没有打开。打开就好了

listeners=PLAINTEXT://localhost:9092

你的答案

查看kafka相关的其他问题或提一个您自己的问题