Linux系統(tǒng)中KafKa安裝和使用方法 java客戶(hù)端連接kafka過(guò)程
kafka linux單機(jī)安裝
1 下載并安裝kafka
# tar zxvf kafka_2.12-1.1.0tgz # mv kafka_2.12-1.1.0 /usr/local/kafka # cd /usr/local/kafka
2 啟動(dòng)服務(wù)
運(yùn)行kafka需要使用Zookeeper,所以需要先啟動(dòng)一個(gè)Zookeeper服務(wù)器,如果沒(méi)有Zookeeper,可以使用kafka自帶打包和配置好的Zookeeper,&后臺(tái)進(jìn)程
# bin/zookeeper-server-start.sh config/zookeeper.properties &
然后啟動(dòng)kafka服務(wù)
# bin/kafka-server-start.sh config/server.properties &
3 新建一個(gè)topic
創(chuàng)建一個(gè)名為“test”的Topic,只有一個(gè)分區(qū)和一個(gè)備份:
# bin/kafka-topics.sh --create --zookeeper localhost:2182 --replication-factor 1 --partitions 1 --topic test
創(chuàng)建好之后,可以通過(guò)以下命令查看已創(chuàng)建的topic信息:
# bin/kafka-topics.sh --list --zookeeper localhost:2182 test
除手工創(chuàng)建topic外,也可以配置broker,當(dāng)發(fā)布一個(gè)不存在的topic時(shí)自動(dòng)創(chuàng)建topic。
4 發(fā)送消息
Kafka提供了一個(gè)命令行工具,可以從輸入文件或者命令行中讀取消息并發(fā)送給Kafka集群,每一行是一條消息。
運(yùn)行producer,然后在控制臺(tái)輸入幾條消息到服務(wù)器
# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test This is a message This is another message
5 消費(fèi)消息
Kafka也提供了一個(gè)消費(fèi)消息的命令行工具
# bin/kafka-console-consumer.sh --zookeeper localhost:2182 --topic test --from-beginning This is a message This is another message
append:

listeners=PLAINTEXT://172.16.49.173:9092

java 客服端連接代碼
生產(chǎn)者代碼
import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class KafkaProducer {
private final Producer<String, String> producer;
public final static String TOPIC = "test";
private KafkaProducer(){
Properties props = new Properties();
//此處配置的是kafka的端口
props.put("metadata.broker.list", "10.175.118.105:9092");
//配置value的序列化類(lèi)
props.put("serializer.class", "kafka.serializer.StringEncoder");
//配置key的序列化類(lèi)
props.put("key.serializer.class", "kafka.serializer.StringEncoder");
props.put("request.required.acks","-1");
producer = new Producer<String, String>(new ProducerConfig(props));
}
void produce() {
int messageNo = 1000;
final int COUNT = 10000;
while (messageNo < COUNT) {
String key = String.valueOf(messageNo);
String data = "hello kafka message " + key;
producer.send(new KeyedMessage<String, String>(TOPIC, key ,data));
System.out.println(data);
messageNo ++;
}
}
public static void main( String[] args )
{
new KafkaProducer().produce();
}
}消費(fèi)者代碼
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.huawei.hwclouds.dbs.ops.base.huatuo.diagnosis.service.impl.KafkaProducer;
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 final ConsumerConnector consumer;
private KafkaConsumer() {
Properties props = new Properties();
//zookeeper 配置
props.put("zookeeper.connect", "10.175.118.105:2182");
//group 代表一個(gè)消費(fèi)組
props.put("group.id", "test-consumer-group");
//zk連接超時(shí)
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");//必須要加,如果要讀舊數(shù)據(jù)
//序列化類(lèi)
props.put("serializer.class", "kafka.serializer.StringEncoder");
ConsumerConfig config = new ConsumerConfig(props);
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config);
}
void consume() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(KafkaProducer.TOPIC, 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(KafkaProducer.TOPIC).get(0);
ConsumerIterator<String, String> it = stream.iterator();
while (it.hasNext())
System.out.println(it.next().message());
}
public static void main(String[] args) {
new KafkaConsumer().consume();
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用VSCode和SSH進(jìn)行遠(yuǎn)程開(kāi)發(fā)
這篇文章主要介紹了使用VSCode和SSH進(jìn)行遠(yuǎn)程開(kāi)發(fā),文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
CentOS 7.2安裝Nginx 1.10.2的詳細(xì)教程
最近公司要切換VPS,所以打算使用最新的系統(tǒng)進(jìn)行搭建LNMP,這篇文章是為CentOS 7.2安裝Nginx 1.10.2的安裝記錄,記錄下以便下次或者有需要的朋友們參考使用。下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
Linux /etc/network/interfaces配置接口方法
在本篇文章里小編給各位分享的是一篇關(guān)于Linux /etc/network/interfaces配置接口方法知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2020-02-02
linux控制臺(tái)下實(shí)現(xiàn)2048小游戲
2048小游戲已經(jīng)火了很久了,各種程序版本的都有,今天我們就來(lái)給大家分享一個(gè)在Linux控制臺(tái)中實(shí)現(xiàn)2048小游戲的代碼,希望大家能夠喜歡。2015-03-03
Ubuntu下安裝rsh實(shí)現(xiàn)無(wú)密碼訪問(wèn)詳解
這篇文章主要為大家詳細(xì)介紹了Ubuntu下安裝rsh實(shí)現(xiàn)無(wú)密碼訪問(wèn)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
linux中使用boost.python調(diào)用c++動(dòng)態(tài)庫(kù)的方法
這篇文章主要給大家介紹了關(guān)于linux中使用boost.python調(diào)用c++動(dòng)態(tài)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-11-11

