spring kafka生产、消费消息
参考网址:
https://blog.csdn.net/lansetiankong12/article/details/54946641
1.新建Maven项目-KafkaMaven
-》点击next按钮

-》点击next按钮

-》点击finish按钮,项目新建成功
2.生产者配置文件:kafka-producer.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义producer的参数 -->
<bean id="producerProperties" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="localhost:9092"/>
<entry key="group.id" value="0"/>
<entry key="retries" value="10"/>
<entry key="batch.size" value="16384"/>
<entry key="linger.ms" value="1"/>
<entry key="buffer.memory" value="33554432"/>
<entry key="key.serializer" value="org.apache.kafka.common.serialization.IntegerSerializer"/>
<entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer"/>
</map>
</constructor-arg>
</bean>
<!-- 创建kafkatemplate需要使用的producerfactory bean -->
<bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
<constructor-arg>
<ref bean="producerProperties"/>
</constructor-arg>
</bean>
<!-- 创建kafkatemplate bean,使用的时候,只需要注入这个bean,即可使用template的send消息方法 -->
<bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
<constructor-arg ref="producerFactory"/>
<constructor-arg name="autoFlush" value="true"/>
<property name="defaultTopic" value="myTopic"/>
</bean>
</beans>
3.消费者配置文件:kafka-consumer.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义consumer的参数 -->
<bean id="consumerProperties" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="localhost:9092"/>
<entry key="group.id" value="0"/>
<entry key="enable.auto.commit" value="true"/>
<entry key="auto.commit.interval.ms" value="1000"/>
<entry key="session.timeout.ms" value="15000"/>
<entry key="key.deserializer" value="org.apache.kafka.common.serialization.IntegerDeserializer"/>
<entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"/>
</map>
</constructor-arg>
</bean>
<!-- 创建consumerFactory bean -->
<bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
<constructor-arg>
<ref bean="consumerProperties"/>
</constructor-arg>
</bean>
<!-- 实际执行消息消费的类 -->
<bean id="messageListernerConsumerService" class="com.kafka.service.impl.MsgConsumerServiceImpl"/>
<!-- 消费者容器配置信息 -->
<bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties">
<constructor-arg value="test"/><!-- test为topic主题名 -->
<property name="messageListener" ref="messageListernerConsumerService"/>
</bean>
<!-- 创建kafkatemplate bean,使用的时候,只需要注入这个bean,即可使用template的send消息方法 -->
<bean id="messageListenerContainer" class="org.springframework.kafka.listener.KafkaMessageListenerContainer" init-method="doStart">
<constructor-arg ref="consumerFactory"/>
<constructor-arg ref="containerProperties"/>
</bean>
</beans>
4.生产者测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/config/kafka-producer.xml" })
public class ProduceMessage {
@Autowired
private KafkaTemplate<Integer, String> kafkaTemplate;
@Test
public void testTemplateSend() {
//topic主题test是上篇http://www.cnblogs.com/Bud-blog/p/9020018.html中新建的主题名称
kafkaTemplate.send("test", "www.686868.com");
}
}
5.消费者监听消息
package com.kafka.service.impl;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.MessageListener;
/**
* 消费者监听
*
*/
public class MsgConsumerServiceImpl implements MessageListener<Integer, String>
{
public void onMessage(ConsumerRecord<Integer, String> record) {
System.out.println(record);
}
}
6.消费者测试类
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerTest {
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/kafka-consumer.xml");
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
synchronized (ConsumerTest.class) {
while (true) {
try {
ConsumerTest.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
7.pom依赖如下:

8.项目视图

9.启动消费者测试类(注意:启动测试类前,需要启动zookeeper和kafka程序)
启动成功控制台如下显示

10.启动生产者测试类,启动成功控制台如下显示

9.consumer接收消息,控制台显示如下:

spring kafka生产、消费消息的更多相关文章
- kafka生产消费原理笔记
一.什么是kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性 ...
- Python往kafka生产消费数据
安装 kafka: pip install kafka-python 生产数据 from kafka import KafkaProducer import json ''' 生产者demo 向te ...
- kafka 生产消费原理详解
Kafka日志及Topic数据清理 https://blog.csdn.net/qiaqia609/article/details/78899298 Kafka--Consumer消费者 pastin ...
- Kafka生产消费API JAVA实现
Maven依赖: <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka- ...
- Kafka 生产消费 Avro 序列化数据
https://unmi.cc/kafka-produce-consume-avro-data/ https://unmi.cc/apache-avro-serializing-deserializi ...
- Spring Kafka和Spring Boot整合实现消息发送与消费简单案例
本文主要分享下Spring Boot和Spring Kafka如何配置整合,实现发送和接收来自Spring Kafka的消息. 先前我已经分享了Kafka的基本介绍与集群环境搭建方法.关于Kafka的 ...
- Spring Cloud Stream如何消费自己生产的消息?
在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...
- Spring Cloud Stream如何消费自己生产的消息
在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...
- kafka生产者与消费者的生产消息与消费消息所遇到的问题
当我们用API写kafka的时候 生产者生产消息,但是消费者接收不到消息?集群上启动消费者显示生产的消息.我们需要修改一下配置 (1)我们打开在虚拟机中修改kafka集群的配置文件 [root@spa ...
随机推荐
- Linux:Day17(下) openssl
Linux Services and Security OpenSSL OpenSSH dns:bind web:http,httpd(apache),php,mariadb(mysql) lamp ...
- 人生第一个过万 Star 的 github 项目诞生
写 Spring Boot 开源项目走入第三个年头,终于有一个开源项目要破万 Star 了,请各位读者大人批评指正. Spring Boot 文章 2016年,我开始学习 Spring Boot 的时 ...
- Python3中如何解决中文乱码与编码的问题
1.解决乱码问题: pyhton中内部所有编码是Unicode,中文是gbk:正常情况下,我们输出的是utf-8: 我们可以采用sys.getdefaultencoding()查看系统默认的编码: 解 ...
- arale-cookie 使用
https://www.npmjs.com/package/arale-cookie arale-cookie 使用 define(function() { var Cookie = require ...
- 电脑突然使用不了复制粘贴快捷键,Ctrl+C和Ctrl+V没用
今天不知道怎么回事,在复制代码的时候突然用不了Ctrl+C和Ctrl+V了 刚开始我还以为是eclipse出问题,然后我在idea中是可以复制 和 粘贴的,然后我又打开文本编辑器notepad++,发 ...
- Linux(Ubuntu)使用日记------trash-cli防止误删文件
1.安装过程 cd /tmp git clone https://github.com/andreafrancia/trash-cli cd trash-cli sudo python setup.p ...
- hdu4843(NOI2000) 古城之谜 (trie树+DP)
Description 著名的考古学家石教授在云梦高原上发现了一处古代城市遗址.让教授欣喜的是在这个他称为冰峰城(Ice-Peak City)的城市中有12块巨大石碑,上面刻着用某种文字书写的资料,他 ...
- Vue项目中jsonp抓取数据实现方式
因为最近在做vue的项目,在前端做数据的时候遇到了数据抓取的难题,查了一些资料,自己也研究了一下,总体来说是搞出来了(基于黄奕老师的项目找出来的经验),废话不多说,直接上代码 ------------ ...
- Windows平台安装TensorFlow Q&A
·本文讲的是Windows平台使用原生pip进行TensorFlow(CPU版本)安装的注意事项及常见问题解决方法 ·这是TensorFlow官网的安装介绍:在 Windows 上安装 TensorF ...
- ClientValidationEnabled
ClientValidationEnabled 去掉这个的时候就无效了,前端验证 <add key="ClientValidationEnabled" value=" ...