spring+springmvc整合kafka 简单示例

记一次spring+springmvc整合kafka的过程,简单示例初学者(附源码)

  • 环境变量的安装
    • 启动zookper和kafka
    • 项目结构图
    • pom.xml 引入kafka所需的jar包
    • config.properties配置文件
    • application-kafka.xml
    • spring-mvc.xml
    • web.xml
    • 生产者SpringProducer
    • 消费者KafkaConsumerListener
    • 总结

环境变量的安装

1.首先是jdk的安装
下载地址:jdk下载
2.安装zookper和kafka,网上有很多案例 不在赘述

启动zookper和kafka

成功安装zookper和kafka之后
1.启动zookper:zkserver
启动成功
spring+springmvc整合kafka 简单示例_第1张图片
2.启动kafka
在kafka安装目录下–>shift+鼠标右键–>打开Powershell窗口
输入命令: .\bin\windows\kafka-server-start.bat .\config\server.properties
spring+springmvc整合kafka 简单示例_第2张图片

项目结构图

spring+springmvc整合kafka 简单示例_第3张图片

pom.xml 引入kafka所需的jar包

注意pom依赖的版本问题 我就是其中一个版本太低导致我一直错误

  
	   
	        org.apache.kafka
	        kafka-clients
	        0.11.0.1
	    
		
	    
	        org.springframework.kafka
	        spring-kafka
	        2.0.4.RELEASE
	    

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-placeholder ignore-unresolvable="false" location="classpath:properties/config.properties"/>
		     
		<!-- 生产者 -->
		<bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
			<constructor-arg>
				<map>
					<entry key="bootstrap.servers" value="${kafka.servers}"></entry>
					<entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
					<entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
					<entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
					<entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
				</map>
			</constructor-arg>
		</bean>
		<!-- 用于发送消息的模板 kafkaTemplate-->
        <bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
        	<constructor-arg ref="producerFactory"></constructor-arg>
        	<constructor-arg name="autoFlush" value="true"></constructor-arg>
        </bean>
        
        <!-- 创建消费者 -->
       <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
       		<constructor-arg>
       			<map>
       				<entry key="bootstrap.servers" value="${kafka.servers}"></entry>
       				<entry key="group.id" value="${kafka.report_data_container}"></entry>
       				<entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
       				<entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
       			</map>
       		</constructor-arg>
       </bean>
       <!-- 消息消费者监听器 用于在接收到消息时执行相关的业务代码  (自行定义的类 用户于处理相应的业务操作)-->
       <bean id="consumerListener" class="com.cyou.web.newsSSM.listener.KafkaConsumerListener"/>
       
      
       
       <!-- 用于将监听器和个某个主题绑定在一起 -->
       <bean id="containerProperties_example" class="org.springframework.kafka.listener.config.ContainerProperties">
       		<constructor-arg value="test-topic"></constructor-arg>
       		<property name="messageListener" ref="consumerListener"></property>
       		<!-- <property name="messageListener" ref="reportDataListener"/> -->
       		<!-- <constructor-arg>
       			<list>
       			 	<value>${kafka.topic_page}</value>
       			 	<value>${kafka.topic_click}</value>
       			</list>
       		</constructor-arg> -->
       </bean>
       <!-- 用于将消息监听器与消息消费者工厂绑定在一起 -->
       <bean id="messageListtenerContainer_example" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
       		<constructor-arg ref="containerProperties_example"></constructor-arg>
       		<constructor-arg ref="consumerFactory"></constructor-arg>
       </bean>
       
       	<!-- springMvc4 用于将对象转换为 JSON  -->  
	    <bean id="stringConverter"  
	        class="org.springframework.http.converter.StringHttpMessageConverter">  
	        <property name="supportedMediaTypes">  
	            <list>  
	                <value>text/plain;charset=UTF-8</value>  
	            </list>  
	        </property>  
	    </bean>  
	    <bean id="jsonConverter"  
	        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>  
	    <bean  
	        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
	        <property name="messageConverters">  
	            <list>  
	                <ref bean="stringConverter" />  
	                <ref bean="jsonConverter" />  
	            </list>  
	        </property>  
	    </bean>          	
 
</beans>        

spring-mvc.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"  
    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">
        
         <!-- 开启注解扫描功能 springMvc--> 
        <context:component-scan base-package="com.java.kafka.controller" />
       
        
        <!-- 视图解析器 (对应DispatcherServlet跳转(逻辑视图名))-->
		<bean id="viewResolver"
			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF" />
			<property name="suffix" value=".jsp"></property>
		</bean>
        
            
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>NewsSSM</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
    <servlet-mapping>
   		<servlet-name>default</servlet-name>
   		<url-pattern>*.js</url-pattern>
   </servlet-mapping>
   
   <servlet-mapping>
   		<servlet-name>default</servlet-name>
   		<url-pattern>*.css</url-pattern>
   </servlet-mapping>
  
  	<!-- Spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止spring内存溢出监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
    <!-- 监听servletContext,启动contextConfigLocation中的spring配置信息 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>
	
    
	<!-- spring 配置   加载配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application-kafka.xml</param-value>
	</context-param>
 
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*
	
    
    
	
		springMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
		true
	
	
		springMVC
		*.do
	
	
    
        spring.profiles.active
        dev
    
    
        spring.profiles.default
        dev
    
    
        spring.liveBeansView.mbeanDomain
        dev
    
    
    
    
      DruidStatView
      com.alibaba.druid.support.http.StatViewServlet
  	
  	
      DruidStatView
      /druid/*
  	


生产者SpringProducer

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.kafka.core.KafkaTemplate;

public class SpringProducer {
	 public static void main(String[] args) {
	        // TODO Auto-generated method stub
	        ApplicationContext context = new ClassPathXmlApplicationContext("application-kafka.xml");
	        KafkaTemplate<String, String> kafkaTemplate = (KafkaTemplate<String, String>)context.getBean("kafkaTemplate");
	        kafkaTemplate.send("test-topic","消息中间件实践");
	        kafkaTemplate.send("test-topic","docker技术");
	        kafkaTemplate.send("test-topic","springcloud实践");
	    }
	
}

消费者KafkaConsumerListener

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.kafka.support.Acknowledgment;

public class KafkaConsumerListener implements MessageListener<String, String>{

	public void onMessage(ConsumerRecord<String, String> record) {
        // TODO Auto-generated method stub
        System.out.println("监听开始.......");
        System.out.println("partition = " + record.partition() 
                  + " ,offset = " + record.offset() 
                  + " ,key = " + record.key() + " ,value = " + record.value());
    }
 
    public void onMessage(ConsumerRecord<String, String> data,
            Acknowledgment acknowledgment) {
        // TODO Auto-generated method stub
        
    }
 
    public void onMessage(ConsumerRecord<String, String> data,
            Consumer<?, ?> consumer) {
        // TODO Auto-generated method stub
        
    }
 
    public void onMessage(ConsumerRecord<String, String> data,
            Acknowledgment acknowledgment, Consumer<?, ?> consumer) {
        // TODO Auto-generated method stub
        
    }

}

总结

这个时候只需要启动生产者类 在控制台看到生产者生产的消息
spring+springmvc整合kafka 简单示例_第4张图片
spring+springmvc整合kafka 简单示例_第5张图片
源码链接在这里: //download.csdn.net/download/weixin_44041342/12038655

参考博文链接
https://blog.csdn.net/qq_31987649/article/details/86500936

你可能感兴趣的:(Java学习)