第一步、下载 zookeeper和kafka
版本:zookeeper:zookeeper-3.4.10
kafka :kafka_2.12-2.1.1
第二步、启动zookeeper服务
zookeeper目录下执行 ./zkServer.sh start 启动服务
zookeeper目录下执行 ./zkServer.sh status 查看服务状态
Mode: standalone 代表单机启动,集群模式随后加上
zookeeper目录下执行 ./zkServer.sh stop 关闭服务
第三步、启动kafka服务
配置kafka(可借鉴)
Kafka在config目录下提供了一个基本的配置文件。为了保证可以远程访问Kafka,我们需要修改两处配置。
打开config/server.properties文件,在很靠前的位置有listeners和 advertised.listeners两处配置的注释,去掉这两个注释,并且根据当前服务器的IP修改如下:
############################# Server Basics #############################
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0
delete.topic.enable=true
host.name=阿里云内网ip
advertised.host.name=阿里云外网ip
############################# Socket Server Settings #############################
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://阿里云内网ip:9092
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
advertised.listeners=PLAINTEXT://阿里云外网ip:9092
---------------------
kafka目录下执行 bin
/zookeeper-server-start
.sh config
/zookeeper
.properties
启动服务
(./bin/kafka-server-start.sh -daemon config/server.properties 加上 -daemon,加上意味着程序在后台运行)
第四步、创建主题
kafka目录下执行bin
/kafka-topics
.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
它只包含一个分区,只有一个副本
replication-factor:1
副本因子(自行查阅具体信息)
partitions :1 代表一个分区
如果我们运行list topic命令,我们现在可以看到该主题:
1 2 |
|
或者,您可以将代理配置为在发布不存在的主题时自动创建主题,而不是手动创建主题。
第五步、生产者
Kafka附带一个命令行客户端,它将从文件或标准输入中获取输入,并将其作为消息发送到Kafka集群。默认情况下,每行将作为单独的消息发送。
运行生产者,然后在控制台中键入一些消息以发送到服务器。
1 2 3 |
|
bin
/kafka-console-producer
.sh --broker-list
阿里云内网Ip:9092 --topic
test
port=9092 端口号
host.name=阿里云内网Ip
advertised.host.name=阿里云外网Ip
第六步、消费者
Kafka还有一个命令行使用者,它会将消息转储到标准输出。
1 2 3 |
|
bin
/kafka-console-consumer
.sh --bootstrap-server
阿里云内网Ip:9092 --topic
test
--from-beginning
如果您在不同的终端中运行上述每个命令,那么您现在应该能够在生产者终端中键入消息并看到它们出现在消费者终端中。
所有命令行工具都有其他选项; 运行不带参数的命令将显示更详细地记录它们的使用信息。
第七步、Springboot yml配置
spring: application: name: kafka-tutorial kafka: # 指定kafka 代理地址,可以多个 bootstrap-servers: 阿里云外网ip:9092 producer: retries: 0 # 每次批量发送消息的数量 batch-size: 16384 # 缓存容量 buffer-memory: 33554432 # 指定消息key和消息体的编解码方式 key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer consumer: # 指定默认消费者group id group-id: consumer-wd auto-commit-interval: 100 auto-offset-reset: earliest enable-auto-commit: true # 指定消息key和消息体的编解码方式 key-deserializer: org.apache.kafka.common.serialization.StringDeserializer value-deserializer: org.apache.kafka.common.serialization.StringDeserializer # 指定listener 容器中的线程数,用于提高并发量 listener: concurrency: 3
Maven配置
<!-- springBoot集成kafka--> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.1.1.RELEASE</version> </dependency>
生产者代码:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/kafka") public class ProducerController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private KafkaTemplate<String, String> kafkaTemplate; @RequestMapping(value = "/send", method = RequestMethod.GET) public String sendKafka(@RequestParam("message") String message) { try { logger.info("kafka的消息={}", message); kafkaTemplate.send("TEST", "key", message); logger.info("发送kafka成功."); return "successs"; } catch (Exception e) { logger.error("发送kafka失败", e); return "failure"; } } }
消费者代码:
import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; /** * @Auth wd * @Date 2019/2/21 * @Desc */ @Component public class ConsumerController { @KafkaListener(topics = "TEST") public void listen(ConsumerRecord<?, ?> record) throws Exception { System.out.printf("++++++++++++++++++++++++"); System.out.printf("topic = %s, offset = %s, value = %s \n", record.topic(), record.key(), record.value()); System.out.printf("++++++++++++++++++++++++"); } }