回顾: Kafka“推拉”模式
1.Kafka选择由producer向broker push消息并由consumer从broker pull消息。
push模式的目标是尽可能以最快速度传递消息;
pull模式则可以根据consumer的消费能力以适当的速率消费消息
Kafka API实现Producer
$>kafka-console-producer.sh --topic my-topic --broker-list localhost:9092
0.将Kafka的maven依赖复制到pom.xml中
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.1</version>
</dependency>
1.编写Producer类 (java类)
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class KafkaProducerDemo {
public static void main(String[] args) {
Properties props = new Properties();
//windows host修改
props.put("bootstrap.servers", "master:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer"