对zookeeper集群的搭建请自行get。
kafka_2.11-0.10.1.0,springBoot 1.5.4
1.安装
wget http://www-us.apache.org/dist/kafka/0.10.1.0/kafka_2.11-0.10.1.0.tgz
tar -zxf kafka_2.11-0.10.1.0.tgz
mv kafka_2.11-0.10.1.0 kafka
cd kafka
2.配置
vim config/server.properties //配置文件
broker.id=0 #整个集群内唯一id号,整数,一般从0开始
listeners=PLAINTEXT://172.16.10.20:9092 #协议、当前broker机器ip、端口,此值可以配置多个,应该跟SSL等有关系,更多用法尚未弄懂,这里修改为ip和端口。
port=9092 #broker端口
host.name=172.16.10.20 #broker 机器ip
log.dirs=/zyxx_data/kafka-logs/kafka00 #kafka存储数据的目录
zookeeper.connect=172.16.10.21:2181,172.16.10.21:2182,172.16.10.21:2183/kafka #zookeeper 集群列表
config/server-1.properties
broker.id=1
listeners=PLAINTEXT://172.16.10.20:9093
port=9093
host.name=172.16.10.20
log.dirs=/zyxx_data/kafka-logs/kafka01
zookeeper.connect=172.16.10.21:2181,172.16.10.21:2182,172.16.10.21:2183/kafka
config/server-2.properties
broker.id=2
listeners=PLAINTEXT://172.16.10.20:9094
port=9094
host.name=172.16.10.20
log.dirs=/zyxx_data/kafka-logs/kafka02
zookeeper.connect=172.16.10.21:2181,172.16.10.21:2182,172.16.10.21:2183/kafka
3.启动
bin/kafka-server-start.sh -daemon config/server.properties
bin/kafka-server-start.sh -daemon config/server-1.properties
bin/kafka-server-start.sh -daemon config/server-2.properties
4.测试
创建Topic
bin/kafka-topics.sh --create --zookeeper ip:2181/kafka --replication-factor 2 --partitions 1 --topic shuaige
#解释
--replication-factor 2 #复制两份
--partitions 1 #创建1个分区
--topic #主题为shuaige
'''在一台服务器上创建一个发布者'''
#创建一个broker,发布者
bin/kafka-console-producer.sh --broker-list 172.16.10.20:9092 --topic base-topic
'''在一台服务器上创建一个订阅者'''
./kafka-console-consumer.sh --zookeeper 172.16.10.21:2181,172.16.10.21:2182,172.16.10.21:2183/kafka --topic base-topic --from-beginning
在发布者控制台输入字符串,然后再订阅者控制台检查是否能够看到结果即可。
4.1、查看topic
bin/kafka-topics.sh --list --zookeeper 172.16.10.21:2181,172.16.10.21:2182,172.16.10.21:2183/kafka
#就会显示我们创建的所有topic
4.2、查看topic状态
bin/kafka-topics.sh --describe --zookeeper 172.16.10.21:2181,172.16.10.21:2182,172.16.10.21:2183/kafka --topic shuaige
#下面是显示信息
Topic:ssports PartitionCount:1 ReplicationFactor:2 Configs:
Topic: shuaige Partition: 0 Leader: 1 Replicas: 0,1 Isr: 1
#分区为为1 复制因子为2 他的 shuaige的分区为0
#Replicas: 0,1 复制的为0,1
#
4.监控
1.先下载安装scala构建工具sbt
#本安装环境为centos6.5
curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo
sudo mv bintray-sbt-rpm.repo /etc/yum.repos.d/
sudo yum install sbt
2.下载编译 kafka-manager
git clone https://github.com/yahoo/kafka-manager
cd kafka-manager
sbt clean dist
cd target/
在target目录下找到kafka-manager-1.3.0.8.zip
解压
unzip kafka-manager-1.3.0.8.zip
cd kafka-manager-1.3.0.8
修改配置 conf/application.properties
# 如果zk是集群,这里填写多个zk地址
kafka-manager.zkhosts="localhost:2181"
启动
bin/kafka-manager
访问 ip:9000
或者
bin/kafka-manager -Dconfig.file=/usr/install/kafka-manager-1.2.7/conf/application.conf (个人而异)
-Dhttp.port=8089 &
访问 ip:8089
在防火墙里面打开kafka使用的所有端口给监控机器。这样可以监控到更全面集群的信息。
5.spring boot(1.5.4)
1.pom.xml
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
2.消费者
@Component
public class Receiving {
@KafkaListener(topics = "base-topic")
public void processMessage(String content) {
System.out.println(Instant.now().toEpochMilli());
System.out.println("content : " + content);
}
}
3.生产者
@RestController
public class KafkaProduct {
@Autowired
private KafkaTemplate kafkaTemplate;
@RequestMapping(value = "index")
public Object index() {
System.out.println("kafkaTemplate : " + kafkaTemplate);
return "index";
}
@RequestMapping(value = "sendMsg")
public Object sendMsg(String msg) {
System.out.println(Instant.now().toEpochMilli());
kafkaTemplate.send("base-topic", msg );
return "ok";
}
}