- 1.zookeeper集羣 搭建在110, 111,112
-
- 2.kafka使用3個節點110, 111,112
- 修改配置文件config/server.properties
- broker.id=110
- host.name=192.168.1.110
- log.dirs=/usr/local/kafka_2.10-0.8.2.0/logs
- 複製到其餘兩個節點,而後修改對應節點上的config/server.pro
-
- 3.啓動,在三個節點分別執行
- bin/kafka-server-start.sh config/server.properties >/dev/null 2>&1 &
-
- 4 建立主題
- bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic test
-
- 5 查看主題詳細
- bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
- --topic test
- Topic:test PartitionCount:3 ReplicationFactor:3 Configs:
- Topic: test Partition: 0 Leader: 110 Replicas: 110,111,112 Isr: 110,111,112
- Topic: test Partition: 1 Leader: 111 Replicas: 111,112,110 Isr: 111,112,110
- Topic: test Partition: 2 Leader: 112 Replicas: 112,110,111 Isr: 112,110,111
-
-
- 6 去zk上看kafka集羣
- [zk: localhost:2181(CONNECTED) 5] ls /
- [admin, zookeeper, consumers, config, controller, zk-fifo, storm, brokers, controller_epoch]
- [zk: localhost:2181(CONNECTED) 6] ls /brokers ----> 查看註冊在zk內的kafka
- [topics, ids]
- [zk: localhost:2181(CONNECTED) 7] ls /brokers/ids
- [112, 110, 111]
- [zk: localhost:2181(CONNECTED) 8] ls /brokers/ids/112
- []
- [zk: localhost:2181(CONNECTED) 9] ls /brokers/topics
- [test]
- [zk: localhost:2181(CONNECTED) 10] ls /brokers/topics/test
- [partitions]
- [zk: localhost:2181(CONNECTED) 11] ls /brokers/topics/test/partitions
- [2, 1, 0]
- [zk: localhost:2181(CONNECTED) 12]
2 kafka java調用:java
2.1 java端生產數據, kafka集羣消費數據:apache
- 1 建立maven工程,pom.xml中增長以下:
- <dependency>
- <groupId>org.apache.kafka</groupId>
- <artifactId>kafka_2.10</artifactId>
- <version>0.8.2.0</version>
- </dependency>
-
-
- 2 java代碼: 向主題test內寫入數據
-
- import java.util.Properties;
- import java.util.concurrent.TimeUnit;
-
- import kafka.javaapi.producer.Producer;
- import kafka.producer.KeyedMessage;
- import kafka.producer.ProducerConfig;
- import kafka.serializer.StringEncoder;
-
-
-
-
- public class kafkaProducer extends Thread{
-
- private String topic;
-
- public kafkaProducer(String topic){
- super();
- this.topic = topic;
- }
-
-
- @Override
- public void run() {
- Producer producer = createProducer();
- int i=0;
- while(true){
- producer.send(new KeyedMessage<Integer, String>(topic, "message: " + i++));
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- private Producer createProducer() {
- Properties properties = new Properties();
- properties.put("zookeeper.connect", "192.168.1.110:2181,192.168.1.111:2181,192.168.1.112:2181");
- properties.put("serializer.class", StringEncoder.class.getName());
- properties.put("metadata.broker.list", "192.168.1.110:9092,192.168.1.111:9093,192.168.1.112:9094");
- return new Producer<Integer, String>(new ProducerConfig(properties));
- }
-
-
- public static void main(String[] args) {
- new kafkaProducer("test").start();
-
- }
-
- }
-
-
-
-
- 3 kafka集羣中消費主題test的數據:
- [root@h2master kafka]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginnin
-
- 4 啓動java代碼,而後在看集羣消費的數據以下:
-
- message: 0
- message: 1
- message: 2
- message: 3
- message: 4
- message: 5
- message: 6
- message: 7
- message: 8
- message: 9
- message: 10
- message: 11
- message: 12
- message: 13
- message: 14
- message: 15
- message: 16
- message: 17
- message: 18
- message: 19
- message: 20
- message: 21
3 kafka 使用Java寫消費者,這樣 先運行kafkaProducer ,在運行kafkaConsumer,便可獲得生產者的數據:api
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
-
- import kafka.consumer.Consumer;
- import kafka.consumer.ConsumerConfig;
- import kafka.consumer.ConsumerIterator;
- import kafka.consumer.KafkaStream;
- import kafka.javaapi.consumer.ConsumerConnector;
-
-
-
-
- public class kafkaConsumer extends Thread{
-
- private String topic;
-
- public kafkaConsumer(String topic){
- super();
- this.topic = topic;
- }
-
-
- @Override
- public void run() {
- ConsumerConnector consumer = createConsumer();
- Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
- topicCountMap.put(topic, 1);
- Map<String, List<KafkaStream<byte[], byte[]>>> messageStreams = consumer.createMessageStreams(topicCountMap);
- KafkaStream<byte[], byte[]> stream = messageStreams.get(topic).get(0);
- ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
- while(iterator.hasNext()){
- String message = new String(iterator.next().message());
- System.out.println("接收到: " + message);
- }
- }
-
- private ConsumerConnector createConsumer() {
- Properties properties = new Properties();
- properties.put("zookeeper.connect", "192.168.1.110:2181,192.168.1.111:2181,192.168.1.112:2181");
- properties.put("group.id", "group1");
- return Consumer.createJavaConsumerConnector(new ConsumerConfig(properties));
- }
-
-
- public static void main(String[] args) {
- new kafkaConsumer("test").start();
-
- }
-
- }
代碼見附件: