消息队列学习02:kafka Windows环境搭建并集成Springboot
一、Zookeeper环境配置
(1)下载zookeeper安装包http://apache.fayea.com/zookeeper/current/;
(2)加压压缩文件到指定位置,本文加压到E:Kafkazookeeper-3.4.12,并新建一个文件夹data;
![](https://upload-images.jianshu.io/upload_images/8820432-4d76537b149f0063.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/513/format/webp)
(3)进入到conf目录,复制zoo_sample.cfg为zoo.cfg,修改其中dataDir为新建文件夹data对的路径;
![](https://upload-images.jianshu.io/upload_images/8820432-2cd6f9f6e63375fd.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/443/format/webp)
(4)进入bin目录,双击zkServer.cmd即可启动zookeeper。
![](https://upload-images.jianshu.io/upload_images/8820432-b40b16f313087586.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/643/format/webp)
二、Kafka环境配置
(1)下载Kafka二进制包,http://kafka.apache.org/downloads.html;
![](https://upload-images.jianshu.io/upload_images/8820432-b495f0ddb5187ae5.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/496/format/webp)
(2)解压安装包到指定目录下,这里我们解压到E:Kafkakafka_2.11-2.1.0,并新建一个命名为logs的空文件夹;
![](https://upload-images.jianshu.io/upload_images/8820432-11dab2bf38356256.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/326/format/webp)
(3)进入config目录,编辑server.properties 文件,修改 log.dirs 属性部分地址为新建logs文件夹的路径;
![](https://upload-images.jianshu.io/upload_images/8820432-e300bc0e080189da.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/597/format/webp)
(4)在E:Kafkakafka_2.11-2.1.0目录下打开命令窗口,输入如下命令设置CLASSPATH;
SET CLASSPATH=.;
(5)然后输入下面的命令启动kafka-server;
.inwindowskafka-server-start.bat .configserver.properties
![](https://upload-images.jianshu.io/upload_images/8820432-1cc13d3e1db05e45.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/669/format/webp)
(6)测试
- 创建topic:在E:Kafkakafka_2.11-2.1.0inwindows下打开命令窗口,输入下面的命令创建test主题
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test.topic
![](https://upload-images.jianshu.io/upload_images/8820432-b4270549089161a3.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/661/format/webp)
- 打开消息生产者:在上一步的命令窗口接着输入
kafka-console-producer.bat --broker-list localhost:9092 --topic test.topic
- 创建一个消息消费者:在E:Kafkakafka_2.11-2.1.0inwindows下打开命令窗口,输入下面的命令:
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test.topic --from-beginning
![](https://upload-images.jianshu.io/upload_images/8820432-95b3a5907e2f4edf.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/664/format/webp)
-
在producer输入内容就会在consumer看到对应显示
三、Kafka环境配置
从IDE初始化一个Springboot项目,具体配置等如下
目录
application.properties
#============== kafka =================== # 指定kafka server的地址,集群配多个,中间,逗号隔开 spring.kafka.bootstrap-servers=127.0.0.1:9092 #=============== provider ======================= # 写入失败时,重试次数。当leader节点失效,一个repli节点会替代成为leader节点,此时可能出现写入失败, # 当retris为0时,produce不会重复。retirs重发,此时repli节点完全成为leader节点,不会产生消息丢失。 spring.kafka.producer.retries=0 # 每次批量发送消息的数量,produce积累到一定数据,一次发送 spring.kafka.producer.batch-size=16384 # produce积累数据一次发送,缓存大小达到buffer.memory就发送数据 spring.kafka.producer.buffer-memory=33554432 #procedure要求leader在考虑完成请求之前收到的确认数,用于控制发送记录在服务端的持久化,其值可以为如下: #acks = 0 如果设置为零,则生产者将不会等待来自服务器的任何确认,该记录将立即添加到套接字缓冲区并视为已发送。在这种情况下,无法保证服务器已收到记录,并且重试配置将不会生效(因为客户端通常不会知道任何故障),为每条记录返回的偏移量始终设置为-1。 #acks = 1 这意味着leader会将记录写入其本地日志,但无需等待所有副本服务器的完全确认即可做出回应,在这种情况下,如果leader在确认记录后立即失败,但在将数据复制到所有的副本服务器之前,则记录将会丢失。 #acks = all 这意味着leader将等待完整的同步副本集以确认记录,这保证了只要至少一个同步副本服务器仍然存活,记录就不会丢失,这是最强有力的保证,这相当于acks = -1的设置。 #可以设置的值为:all, -1, 0, 1 spring.kafka.producer.acks=1 # 指定消息key和消息体的编解码方式 spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer #=============== consumer ======================= # 指定默认消费者group id --> 由于在kafka中,同一组中的consumer不会读取到同一个消息,依靠groud.id设置组名 spring.kafka.consumer.group-id=testGroup # smallest和largest才有效,如果smallest重新0开始读取,如果是largest从logfile的offset读取。一般情况下我们都是设置smallest spring.kafka.consumer.auto-offset-reset=earliest # enable.auto.commit:true --> 设置自动提交offset spring.kafka.consumer.enable-auto-commit=true #如果'enable.auto.commit'为true,则消费者偏移自动提交给Kafka的频率(以毫秒为单位),默认值为5000。 spring.kafka.consumer.auto-commit-interval=100 # 指定消息key和消息体的编解码方式 spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tyj</groupId> <artifactId>kafka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>kafka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>1.1.1.RELEASE</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
生产者
package com.tyj.kafka.controller; /* * *@Description: *@Author:TYJ *@Date: create in 2019/10/14 17:47 */ import com.tyj.kafka.producer.KafkaSender; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class KafkaController { @Autowired private KafkaSender kafkaSender; @GetMapping("/message/send") public String send(){ kafkaSender.sendMsg("testTopic","hello tyj"); return "success"; } }
消费者
package com.tyj.kafka.consumer; /* * *@Description: *@Author:TYJ *@Date: create in 2019/10/14 17:57 */ import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class ConsumerListener { @KafkaListener(topics = "testTopic") public void onMessage(String message){ System.out.println("ConsumerListener: " + message); } }
controller
package com.tyj.kafka.controller; /* * *@Description: *@Author:TYJ *@Date: create in 2019/10/14 17:47 */ import com.tyj.kafka.producer.KafkaSender; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class KafkaController { @Autowired private KafkaSender kafkaSender; @GetMapping("/message/send") public String send(){ String msg = "hello tyj"; System.out.println("producer send message: " + msg ); kafkaSender.sendMsg("testTopic",msg); return "success"; } }
启动类
package com.tyj.kafka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class KafkaApplication { public static void main(String[] args) { SpringApplication.run(KafkaApplication.class, args); } }
测试
依次启动Zookeeper、kafka、Springboot;在浏览器输入http://localhost:8080/message/send
日志输出
producer send message: hello tyj 2019-10-14 20:14:00.278 INFO 17768 --- [nio-8080-exec-1] 2019-10-14 20:14:00.285 INFO 17768 --- [nio-8080-exec-1] o.a.kafka.common.utils.AppInfoParser : Kafka version : 0.10.0.1 2019-10-14 20:14:00.285 INFO 17768 --- [nio-8080-exec-1] o.a.kafka.common.utils.AppInfoParser : Kafka commitId : a7a17cdec9eaa6c5 ConsumerListener: hello tyj
四、参考资料: