org.apache.kafka.common.config.configexception:缺少没有默认值的必需配置“bootstrap.servers”

mjqavswn  于 2021-06-08  发布在  Kafka
关注(0)|答案(3)|浏览(835)

我在eclipse中运行producer类时遇到这个错误:org.apache.kafka.common.config.configexception:缺少没有默认值的必需配置“bootstrap.servers”
这是我的制作人课程:

public class SimpleProducer {

  public static void main(String[] args) throws Exception {

    try {
        String topicName = "mytopic";
        String key = "key1";
        String value = "Value-1";

        Properties prop = new Properties();
        prop.put("bootstrap.server","localhost:9092");
        prop.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        prop.put("value.serializer","org.apache.kafka.cpmmon.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(prop);

        ProducerRecord<String, String> record = new ProducerRecord<>(topicName,key,value);
        producer.send(record);
        producer.close();
        System.out.println("SimpleProducer Completed.");
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}

有没有关于如何修复的建议?

watbbzwu

watbbzwu1#

使用以下命令,避免使用硬编码值
对于

prop.put("bootstrap.server","localhost:9092");
prop.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
prop.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

使用

prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

producerconfig将在org.apache.kafka.clients.producer包中找到

s4chpxco

s4chpxco2#

换个衣服就行了 prop.put("bootstrap.server","localhost:9092");prop.put("bootstrap.servers","localhost:9092"); 在你的代码里 prop.put("value.serializer","prop.put("value.serializer","org.apache.kafka.cpmmon.serialization.StringSerializer"); 这个 common 不正确,正确的包是 org.apache.kafka.common.serialization.StringSerializer

eit6fx6z

eit6fx6z3#

这里发现了两个观察结果

1.

代替

prop.put("bootstrap.server","localhost:9092");

通过

prop.put("bootstrap.servers","localhost:9092");

bootstrap.server--不正确
bootstrap.servers—正确

2.

代替

org.apache.kafka.cpmmon.serialization.StringSerializer

通过

org.apache.kafka.common.serialization.StringSerializer

相关问题