spring.kafka.producer.bootstrapServers=192.168.211.54:4988
spring.kafka.producer.properties.sasl.mechanism=PLAIN
spring.kafka.producer.properties.security.protocol=SASL_PLAINTEXT
spring.kafka.producer.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username=\"caadmin\" password=\"R5s^C8p@B5dA\";
或者硬編碼寫法
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();
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結