I'm getting this error when running my producer class in Eclipse: org.apache.kafka.common.config.ConfigException: Missing required configuration "bootstrap.servers" which has no default value
Here is my producer class:
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();
}
}
}
Any pointers on how to fix it?
Use the following and avoid using hardcoded values
For
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");
Use
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 will be found in org.apache.kafka.clients.producer package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With