spring+springmvc整合kafka 简单示例

本文介绍了如何在Spring+SpringMVC环境下整合Kafka,包括环境变量安装、启动Zookeeper和Kafka、项目结构、配置文件设置以及生产者和消费者的实现。通过详细步骤和源码分享,帮助读者理解并实现Spring集成Kafka的基本过程。
摘要由CSDN通过智能技术生成

环境变量的安装

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
        ">
        <!-- 加载配置文件 --
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值