首页 > 代码库 > kafka安装以及入门

kafka安装以及入门

一、安装

下载最新版kafka,Apache Kafka,然后上传到Linux,我这里有三台机器,192.168.127.129,130,131 。

 进入上传目录,解压到/usr/local目录下

tar -zxvf kafka_2.11-0.11.0.0.tgz -C /usr/local/

 

进入/usr/local目录

cd /usr/local

 

然后改一下名字(这一步可有可无) 

mv kafka_2.11-0.11.0.0/ kafka

 

 进入kafka目录,编辑配置文件

cd kafka/

vi config/server.properties

 

配置文件:有的地方三台机器不一样哦,不一样的你都能看出来

 技术分享

编辑好之后,进入kafka目录并且建立日志目录

mkdir kafka-logs

 开启kafka的通信端口9092

firewall-cmd --zone=public --add-port=9092/tcp --permanent

systemctl restart firewalld

 

 启动Zookeeper,如果你不知道Zookeeper安装的话,就找我的文章,有一篇是关于Zookeeper安装的。

 然后启动kafka

cd /usr/local/kafka/bin/
./kafka-server-start.sh /usr/local/kafka/config/server.properties &

 

最后出现

技术分享

就是成功了,进入Zookeeper客户端

技术分享

除了activemq,zookeeper,dubbo,root,都是kafka相关的

二、入门

依赖jar包,请找到kafka的libs目录

技术分享

生产者:
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class SimpleProducer {

    public static void main(String[] args) throws InterruptedException {
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.127.129:9092,192.168.127.130:9092,192.168.127.131: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", "org.apache.kafka.common.serialization.StringSerializer");
        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 10; i++){
            //ProducerRecord<String, String>(String topic, String key, String value)
            producer.send(new ProducerRecord<String, String>("my-topic", "fruit" + i, "banana" + i));
            Thread.sleep(1000);
        }
        producer.close();

    }
}
消费者:
import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

public class SimpleConsumer {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.127.129:9092,192.168.127.130:9092,192.168.127.131:9092");
        props.put("group.id", "testGroup");
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        //订阅的topic,多个用逗号隔开
        consumer.subscribe(Arrays.asList("my-topic"));
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records)
                System.out.printf("offset = %d, key = %s, value = http://www.mamicode.com/%s%n", record.offset(), record.key(), record.value());
        }
        //consumer.close();
    }
}

 

先启动消费者,再启动生产者

你会发现消费者每隔1秒输出一行数据。

 

kafka安装以及入门