kafka安装以及入门demo
1.默认你已经安装了zookeeper3.4.8 and 关闭防火墙
kafka:2.9.2-0.8.2.1
kafka 官方下载地址 http://apache.fayea.com/kafka/0.8.2.1/kafka_2.9.2-0.8.2.1.tgz
tar -zxvf kafka_2.9.2-0.8.2.1.tgz -C /usr/local/ && mv kafka_2.9.2-0.8.2.1 kafka cd /usr/local/kafka vi config/zookeeper.properties dataDir=/usr/local/kafka/zookeeper vi config/server.properties broker.id=0 port=9092 hostname=192.168.194.110 log.dirs=/usr/local/kafka/kafka-logs zookeeper.connect=192.168.194.110:2181
bin/kafka-server-start.sh config/server.properties &
查看启动状态
jps
小DEMO
maven添加依赖
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.10</artifactId> <version>0.8.2.0</version> </dependency>
kafka消息生产者KafkaProducer
package kafka; import java.util.Date; import java.util.Properties; import java.util.Random; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; public class KafkaProducer extends Thread { public static void main(String[] args) { Properties props = new Properties(); props.put("metadata.broker.list", "176.20.32.51:9092"); props.put("serializer.class", "kafka.serializer.StringEncoder"); props.put("key.serializer.class", "kafka.serializer.StringEncoder"); props.put("request.required.acks", "1"); //配置key的序列化类 ProducerConfig config = new ProducerConfig(props); Producer<String, String> producer = new Producer<String, String>(config); Random rnd = new Random(); long runtime = new Date().getTime(); String ip = "176.20.32." + rnd.nextInt(255); String msg = runtime + ",www.example.com," + ip; KeyedMessage<String, String> data = new KeyedMessage<String, String>("test", "test-key",msg); producer.send(data); } }
kafka消息消费者 KafkaConsumer
package kafka; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; import kafka.serializer.StringDecoder; import kafka.utils.VerifiableProperties; public class KafkaConsumer { private static ConsumerConnector consumer = null; public static void main(String[] args) { Properties props = new Properties(); // zookeeper 配置 props.put("zookeeper.connect", "176.20.32.51:2181"); // group 代表一个消费组 props.put("group.id", "jd-group"); // zk连接超时 props.put("zookeeper.session.timeout.ms", "4000"); props.put("zookeeper.sync.time.ms", "200"); props.put("auto.commit.interval.ms", "1000"); props.put("auto.offset.reset", "smallest"); // 序列化类 props.put("serializer.class", "kafka.serializer.StringEncoder"); ConsumerConfig config = new ConsumerConfig(props); consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put("test", new Integer(1)); StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties()); StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties()); Map<String, List<KafkaStream<String, String>>> consumerMap = consumer.createMessageStreams(topicCountMap, keyDecoder, valueDecoder); KafkaStream<String, String> stream = consumerMap.get("test").get(0); ConsumerIterator<String, String> it = stream.iterator(); while (it.hasNext()) System.out.println(it.next().message()); } }
分别启动producer 和consumer 即可进行简单的消息发送和接收
log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 1441952197141,www.example.com,192.168.2.126
今日事今日毕