参考网址:

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. openstack 2019/4/28

    官网参考地址:https://docs.openstack.org/keystone/queens/install/index-rdo.html (但愿能看懂) 环境:这个部分解释如何按示例架构配置控 ...

  2. handsontable的基础应用

    handsontable是一款页面端的表格式交互插件,可以通过她加载显示表格内容,能够支持合并项.统计.行列拖动等. 同时,支持对加载后的表格页面的处理:添加/删除行/列,合并单元格等操作. 我在项目 ...

  3. 关于Net core 的https 设置小知识

    今天我是遇到了一个蛋疼的问题, 就是https 协议, 在创建项目的时候, 我勾选了for  https (如下图), 然后我就在startup.cs 和Kestrel 各种设置还是全部走https ...

  4. 自然人税收管理系统扣缴客户端Sqlite数据库有密码的,如何破解读取呢

    https://www.cnblogs.com/Charltsing/p/EPPortal.html 有人问我能不能直接读自然人税收管理系统扣缴客户端,因为需要导出数据做处理. 看了一下,这个客户端是 ...

  5. vue 父子之间的通讯

    //父组件<template>     <Button @click='openChild'><Button>      <child-modal :moda ...

  6. JVM进程占用CPU过高问题排查

    上午收到报警,某台机器上的CPU负载过高,通过逐步的排查,解决了问题,下面记录一下整个排查的过程. 首先,登录上对应的机器,通过top命令找到占用CPU过高的进程ID,也就是PID,为29126, 然 ...

  7. kettle变量(param命名参数2)

    接arg参数: 通过命令行进行变量赋值和引用 定义跟界面定义相同: 赋值(转换): 运行命令到kettle目录下 pan /file:path "/param:aa="bb&quo ...

  8. LeetCode136.只出现一次的数字

    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...

  9. A/B test

    A/B test https://en.wikipedia.org/wiki/A/B_testing A/B testing (bucket tests or split-run testing) i ...

  10. Ubuntu系统桌面任务栏和启动器全部消失解决方案

    ubuntu桌面上没有启动器,没有任务栏,只有一个背景,但是运行正常.这种情况很可能是文件管理程序出现异常了. 解决办法: Ctrl+Alt+F1 进入命令行,输入: sudo service lig ...