kafka学习之producer端部署及API

1producer.properties:文件位于/resources目录下

#partitioner.class=
metadata.broker.list=183.57.57.76:9092
producer.type=sync
compression.codec=0
serializer.class=kafka.serializer.StringEncoder
##在producer.type=async时有效
#batch.num.messages=100
2、Java API实现

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
 
import java.util.Properties;
 
public class KafkaProductor {
 
public static void main(String[] args) throws InterruptedException {
 
Properties properties = new Properties();
 properties.put("zk.connect", "183.57.57.76:2181/YRFS/test-datacenter/test-server");
 properties.put("metadata.broker.list", "183.57.57.76:9092");
 
properties.put("serializer.class", "kafka.serializer.StringEncoder");
 
ProducerConfig producerConfig = new ProducerConfig(properties);
 Producer producer = new Producer(producerConfig);
 
// 构建消息体
 KeyedMessage keyedMessage = new KeyedMessage("test-topic", "test-message");
 producer.send(keyedMessage);
 
Thread.sleep(1000);
 
producer.close();
 }
 
}




你可能感兴趣的:(kafka学习)