kafka使用scala编写,采用zookeeper作为服务注册发现中心,下载地址以及安装启动过程如下,我的安装目录是/Users/r/services/kafka:
核心:
- 下载 https://www.apache.org/dyn/closer.cgi?path=/kafka/2.2.0/kafka_2.12-2.2.0.tgz
- 解压至/opt目录,重命名为kafka
- cd /opt
- tar zxvf kafka_2.12-2.2.0.tgz
- mv kafka_2.12-2.2.0 kafka
- 进入程序目录:
- cd /opt/kafka
- 启动zookeeper:如果没有配置文件,记得先复制一份配置文件
- ./bin/zookeeper-server-start.sh config/zookeeper.properties
- 启动kafka:如果没有配置文件,记得先复制一份配置文件
- ./bin/kafka-server-start.sh config/server.properties
- 创建topic test
- ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
- 显示topic列表
- ./bin/kafka-topics.sh --list --zookeeper localhost:2181
- 创建消费者
- ./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
- 创建生产者
- ./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
- kafka-manage
- web管理工具 https://www.cnblogs.com/dadonggg/p/8205302.html
- github https://github.com/yahoo/kafka-manager
- cd /Users/r/services/kafka-manager/target/universal/kafka-manager-2.0.0.0; ./bin/kafka-manager -Dconfig.file=conf/application.conf -Dhttp.port=9080
- kafka架构简介
- spring-integration-kafka & spring-boot-starter
创建topic test
[root@uds-gww2 kafka]# ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic test.
[root@uds-gww2 kafka]#
显示topic列表
[root@uds-gww2 kafka]# ./bin/kafka-topics.sh --list --zookeeper localhost:2181
test
[root@uds-gww2 kafka]#
预先创建topic的情景:
生产:
[root@uds-gww2 kafka]# ./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>asfd
>asdf
>^C[root@uds-gww2 kafka]#
消费:
[root@uds-gww2 kafka]# ./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
asfd
asdf
^CProcessed a total of 2 messages
[root@uds-gww2 kafka]#
topic没有被预先创建的情景:
生产:
[root@uds-gww2 kafka]# ./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test2
>asfwr
[2019-04-09 02:04:07,594] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 3 : {test2=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
[2019-04-09 02:04:07,697] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 4 : {test2=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
>^C[root@uds-gww2 kafka]#
消费:
[root@uds-gww2 kafka]# ./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning
asfwr
^CProcessed a total of 1 messages
[root@uds-gww2 kafka]#
再次显示topic列表:
[root@uds-gww2 kafka]# ./bin/kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
test
test2
[root@uds-gww2 kafka]#
kafka-manager的dashboard如下:http://localhost:9080/
下面创建springboot项目kafka-hello,引入依赖:spring-kafka
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
配置属性文件application.properties: 其中的地址是zookeeper的地址
spring.kafka.bootstrap-servers=127.0.0.1:9092
spring.kafka.consumer.group-id=test1
定义生产者:
package com.sc.kafkahello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
@Component
public class Sender {
@Autowired
private KafkaTemplate kafkaTemplate;
public void send(String msg) {
ListenableFuture listenableFuture = this.kafkaTemplate.send("test1", msg);
}
}
定义消费者:
package com.sc.kafkahello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
@Component
public class Sender {
@Autowired
private KafkaTemplate kafkaTemplate;
public void send(String msg) {
ListenableFuture listenableFuture = this.kafkaTemplate.send("test1", msg);
}
}
测试例子:
package com.sc.kafkahello;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class KafkaHelloApplicationTests {
@Autowired
private Sender sender;
@Test
public void contextLoads() {
this.sender.send((new Date()).toString());
}
}
先运行程序,再运行测试例子,观察到消息消费:
命令行也能同步消费消息: