2017-06-05 50 views
1

運行我的製片人上課的時候Eclipse的我得到這個錯誤沒有默認值: org.apache .kafka.common.config.ConfigException:缺少必需的配置「bootstrap.servers」,它沒有默認值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(); 
    } 
    } 
} 

上任何指針如何修理它?

+1

您拼寫屬性鍵「bootstrap.servers」'。爲了避免將來出現這種錯誤,請改用常量'ProducerConfig.BOOTSTRAP_SERVERS_CONFIG'。 –

回答

4

只要改變

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

0

使用以下,避免使用硬編碼值

對於

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"); 

使用

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

相關問題