SPRING 集成 KAFKA 发送消息
准备工作
1.安装kafka+zookeeper环境
2.利用命令创建好topic,创建一个topic my-topic
集成步骤
1.配置生产者
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 定义producer的参数 -->
<bean id="producerProperties" class="java.util.HashMap">
<constructor-arg>
<map>
<!-- 配置kafka的broke -->
<entry key="bootstrap.servers" value="192.168.1.88:9001,192.168.1.88:9002"/>
<!-- 配置组-->
<entry key="group.id" value="group1"/>
<entry key="acks" value="all"/>
<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.StringSerializer"/>
<entry key="value.serializer" value="com.redxun.jms.ObjectSerializer"/>
</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="my-topic"/>
</bean> </beans>
2.配置消费者
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 定义consumer的参数 -->
<bean id="consumerProperties" class="java.util.HashMap">
<constructor-arg>
<map>
<!-- 配置kafka的broke -->
<entry key="bootstrap.servers" value="192.168.1.88:9001,192.168.1.88:9002"/>
<!-- 配置组-->
<entry key="group.id" value="group1"/>
<entry key="enable.auto.commit" value="true"/>
<entry key="auto.commit.interval.ms" value="1000"/>
<entry key="session.timeout.ms" value="30000"/>
<entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"/>
<entry key="value.deserializer" value="com.redxun.jms.ObjectDeSerializer"/>
</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.redxun.jms.KafkaConsumerListener"/> <!-- 消费者容器配置信息 -->
<bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties">
<!-- 重要!配置topic -->
<constructor-arg value="my-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>
3.消息序列化和反序列化
在发送消息时,我们可以发送对象,而不只是字符串,所以我们需要将发送的数据进行序列化和反序列化,上面的配置文件有配置序列化和反序列化。
序列化代码
package com.redxun.jms; import java.util.Map; import org.apache.kafka.common.serialization.Serializer; import com.redxun.core.util.FileUtil; public class ObjectSerializer implements Serializer<Object> { @Override
public void configure(Map<String, ?> configs, boolean isKey) { } @Override
public byte[] serialize(String topic, Object data) {
try {
return FileUtil.objToBytes(data);
} catch (Exception e) {
return null;
}
} @Override
public void close() { } }
反序列化
package com.redxun.jms; import java.util.Map; import org.apache.kafka.common.serialization.Deserializer; import com.redxun.core.util.FileUtil; public class ObjectDeSerializer implements Deserializer<Object> { @Override
public void configure(Map<String, ?> configs, boolean isKey) { } @Override
public Object deserialize(String topic, byte[] data) {
try {
return FileUtil.bytesToObject(data);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} @Override
public void close() {
// TODO Auto-generated method stub } }
4.发送消息代码
OsUser user=new OsUser();
user.setUserId("00001");
user.setFullname("zyg");
kafkaTemplate.sendDefault("demo", user);
5.接收消息代码
package com.redxun.jms; import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.MessageListener; import com.redxun.sys.org.entity.OsUser; public class KafkaConsumerListener implements MessageListener<String, Object> { @Override
public void onMessage(ConsumerRecord<String, Object> record) {
if(record.value() instanceof OsUser ){
OsUser user=(OsUser) record.value();
System.out.println(user.getFullname());
} } }
6.注意事项
在配置 kafka 配置文件
需要配置
host.name=ip地址
port=端口
SPRING 集成 KAFKA 发送消息的更多相关文章
- Kafka发送消息失败原因
Kafka发送消息方法如下: Properties properties = new Properties(); properties.put("zookeeper.connect" ...
- Spring集成kafka,消费者运行时内存占用会一直增长
Spring集成kafka,消费者运行时内存占用会一直增长? 20C 本人用Spring集成kafka消费者,发布运行时内存占用会一直升高,最后程序挂掉.请各位大神看看,提供解决方法 以下是我的配置文 ...
- Spring 集成Kafka(完整版)
前面的文章我们已经完成了Kafka基于Zookeeper的集群的搭建了.Kafka集群搭建请点我.记过几天的研究已经实现Spring的集成了.本文重点 jar包准备 集成是基于spring-integ ...
- [置顶]
spring集成mina 实现消息推送以及转发
spring集成mina: 在学习mina这块时,在网上找了很多资料,只有一些demo,只能实现客户端向服务端发送消息.建立长连接之类.但是实际上在项目中,并不简单实现这些,还有业务逻辑之类的处理以及 ...
- 物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
1. 前言 按照我自己设计的物联网框架,对于MQTT集群中的所有消息,是要持久化到磁盘的,这里采用一个消息队列中间件Kafka作为数据缓冲,缓冲结果存到数据仓库中,以供后续作为数据分析.由于MQTT集 ...
- spring集成kafka
一.添加依赖项 compile 'org.springframework.kafka:spring-kafka:1.2.2.RELEASE' 二.发消息(生产者) 2.1 xml配置 <?xml ...
- Kafka生产者发送消息的三种方式
Kafka是一种分布式的基于发布/订阅的消息系统,它的高吞吐量.灵活的offset是其它消息系统所没有的. Kafka发送消息主要有三种方式: 1.发送并忘记 2.同步发送 3.异步发送+回调函数 下 ...
- Kafka学习笔记(6)----Kafka使用Producer发送消息
1. Kafka的Producer 不论将kafka作为什么样的用途,都少不了的向Broker发送数据或接受数据,Producer就是用于向Kafka发送数据.如下: 2. 添加依赖 pom.xml文 ...
- SpringBoot开发案例之整合Kafka实现消息队列
前言 最近在做一款秒杀的案例,涉及到了同步锁.数据库锁.分布式锁.进程内队列以及分布式消息队列,这里对SpringBoot集成Kafka实现消息队列做一个简单的记录. Kafka简介 Kafka是由A ...
随机推荐
- Bugku——Flag在index里(http://120.24.86.145:8005/post/)
Bugku——Flag在index里(http://120.24.86.145:8005/post/) 进入题目发现有一个file参数,查看源码,发现该参数可以包含php文件,并且题目提示,flag在 ...
- 关于后台执行JS代码总结
方法一.FineUI的 pagecontext对象 string js="functionName();"; PageContext.RegisterStartUpScript(j ...
- vue 给v-html中的元素设置样式
解决方案:写样式的时候添加>>>
- sizeof 4字节对齐
#include <iostream> #include<assert.h> using namespace std; typedef struct sys{ char a; ...
- form表单验证字段学习总结
字段的属性梳理 最重要的字段 required inital widget error_messages ----------------------------------------------- ...
- mac navicat premium 使用技巧
快捷键 CMD-I:对象信息 CMD-L:查询日志 CMD-Y:新建查询 SHIFT-CMD-T:数据传输 SHIFT-CMD-C:命令列界面
- mac上为nodejs设置环境变量
Mac下面的环境变量设置和Linux差不多,一般为这几个文件(左边的先加载): /etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.pro ...
- mysql 5.6 datetime 保存精确到秒
mysql中的CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 设置默认值 now(3) datetime 长度 3 保存精确到秒
- 31-mysql 代码建立数据库
给个例子,模仿即可: drop database if exists tt; create database tt default character set utf8; use tt; create ...
- vue-form表单验证插件
参考地址:https://segmentfault.com/q/1010000003988864 github地址:https://github.com/fergaldoyle/vue-form 安装 ...