参考网址:

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生产、消费消息的更多相关文章

  1. kafka生产消费原理笔记

    一.什么是kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性 ...

  2. Python往kafka生产消费数据

    安装 kafka:  pip install kafka-python 生产数据 from kafka import KafkaProducer import json ''' 生产者demo 向te ...

  3. kafka 生产消费原理详解

    Kafka日志及Topic数据清理 https://blog.csdn.net/qiaqia609/article/details/78899298 Kafka--Consumer消费者 pastin ...

  4. Kafka生产消费API JAVA实现

    Maven依赖: <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka- ...

  5. Kafka 生产消费 Avro 序列化数据

    https://unmi.cc/kafka-produce-consume-avro-data/ https://unmi.cc/apache-avro-serializing-deserializi ...

  6. Spring Kafka和Spring Boot整合实现消息发送与消费简单案例

    本文主要分享下Spring Boot和Spring Kafka如何配置整合,实现发送和接收来自Spring Kafka的消息. 先前我已经分享了Kafka的基本介绍与集群环境搭建方法.关于Kafka的 ...

  7. Spring Cloud Stream如何消费自己生产的消息?

    在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...

  8. Spring Cloud Stream如何消费自己生产的消息

    在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...

  9. kafka生产者与消费者的生产消息与消费消息所遇到的问题

    当我们用API写kafka的时候 生产者生产消息,但是消费者接收不到消息?集群上启动消费者显示生产的消息.我们需要修改一下配置 (1)我们打开在虚拟机中修改kafka集群的配置文件 [root@spa ...

随机推荐

  1. 最新 robot framework安装

    相信大家对robot framework并不陌生,它是一个基于Python语言,用于验收测试和验收测试驱动开发(ATDD)的通用测试自动化框架=,提供了一套特定的语法,并且有非常丰富的测试库. Pyt ...

  2. React 系列教程2:编写兰顿蚂蚁演示程序

    简介 最早接触兰顿蚂蚁是在做参数化的时候,那时候只感觉好奇,以为是很复杂的东西.因无意中看到生命游戏的 React 实现,所以希望通过兰顿蚂蚁的例子再学习一下 React. 兰顿蚂蚁的规则非常简单: ...

  3. opendir函数和readdir函数内涵及用法

    工作中遇到奇怪的事,加载增量的时候加载不上.于是开始分析原因,log里边没有任何错误信息,只有加载完成的标志.增量的数据在目录里边是存在的,但是显示的目录大小却不是4096,而是17,不知道为什么.后 ...

  4. Python----逻辑回归

    逻辑回归 1.逻辑函数 sigmoid函数就出现了.这个函数的定义如下: sigmoid函数具有我们需要的一切优美特性,其定义域在全体实数,值域在[0, 1]之间,并且在0点值为0.5. 那么,如何将 ...

  5. WebApi(五)-Swagger接口文档①简单集成

    1,通过NuGet引用Swashbuckle 2,打开项目属性-->生成,勾选XML文档文件,保存 3,找到项目App_Start文件夹下WebApiConfig查找GetXmlComments ...

  6. 微信支付之01------获取订单微信支付二维码的接口------Java实现

    [ 前言:以前写过一个获取微信二维码支付的接口,发现最近公司新开的项目会经常用到,现在我又翻出代码看了一遍,觉得还是把整个代码流程记下来的好 ] 借鉴博客: 他这篇博客写得不错,挺全的:https:/ ...

  7. vue.js实战——props单向数据流

    Vue2.x通过props传递数据是单向的了,也就是父组件数据变化时会传递给子组件,但是反过来不行. 业务中会经常遇到两种需要改变prop的情况, 一种是父组件传递初始值进来,子组件将它作为初始值保存 ...

  8. P3373 【模板】线段树 2

    线段树的模板,但是还应注意维护乘标记,乘法的优先级大于加法,一定记得还要取模. #include<bits/stdc++.h> using namespace std; ; struct ...

  9. Linux学习之路1

    root用户 安装初始系统默认没有启动root用户,如下方式启动: l  sudo passwd l  连续输入两次密码,启动root用户 l  再登陆 登陆root用户 l  su root l  ...

  10. hdu-1711(hash)

    题意:给你T组数据,每组数据分别输入n,m和长度为n的数字数组,和长度为m的数字数组,问你长度为m的数组第一次出现在长度为n的数组的位置 解题思路:标准字符串匹配问题,一般用kmp解,拿来练hash ...