记一次spring+springmvc整合kafka的过程,简单示例初学者(附源码)
环境变量的安装
1.首先是jdk的安装
下载地址:jdk下载
2.安装zookper和kafka,网上有很多案例 不在赘述
启动zookper和kafka
成功安装zookper和kafka之后
1.启动zookper:zkserver
启动成功
2.启动kafka
在kafka安装目录下–>shift+鼠标右键–>打开Powershell窗口
输入命令: .\bin\windows\kafka-server-start.bat .\config\server.properties
项目结构图
pom.xml 引入kafka所需的jar包
注意pom依赖的版本问题 我就是其中一个版本太低导致我一直错误
<!-- kafka客户端支持包 -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.1</version>
</dependency>
<!-- spring支持kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
config.properties配置文件
#============================= kafka ============================#
#主题
#页面浏览 topic
kafka.topic_page=pageTopic
#点击 topic
kafka.topic_click=clickTopic
#kafka 集群地址
kafka.servers=192.168.28.129:9092,192.168.28.130:9092,192.168.28.131:9092
#指定组名
kafka.group_id_report=report-data
kafka.report_data_container="reportDataContainer1"
application-kafka.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.1.xsd
">
<!-- 加载配置文件 -->
<context:property