spring整合kafka(配置文件方式 生产者)
Kafka官方文档有 https://docs.spring.io/spring-kafka/reference/htmlsingle/
这里是配置文件实现的方式
先引入依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.1.0.RELEASE</version>
</dependency> 创建 spring-context-kafka-provider.xml 当然要配置spring扫描该配置文件 配置文件如下
<?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="kafka集群地址"/>
<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="test"/>
</bean> </beans> 在需要发消息的类里自动注入
@Autowired
private KafkaTemplate<Integer, String> kafkaTemplate;
用kafkaTemplate就可以调用发消息的方法
spring整合kafka(配置文件方式 生产者)的更多相关文章
- spring整合kafka(配置文件方式 消费者)
Kafka官方文档有 https://docs.spring.io/spring-kafka/reference/htmlsingle/ 这里是配置文件实现的方式 先引入依赖 <depend ...
- spring整合hibernate配置文件
Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置 spring对hibernate配置文件hibernate.cfg.x ...
- spring 整合kafka监听消费
前言 最近项目里有个需求,要消费kafka里的数据.之前也手动写过代码去消费kafka数据.但是转念一想.既然spring提供了消费kafka的方法.就没必要再去重复造轮子.于是尝试使用spring的 ...
- spring整合kafka项目生产和消费测试结果记录(一)
使用spring+springMVC+mybatis+kafka做了两个web项目,一个是生产者,一个是消费者. 通过JMeter测试工具模拟100个用户并发访问生产者项目,发送json数据给生产者的 ...
- hibernate3和spring整合的一些方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Ibatis,Spring整合(注解方式注入)
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- 关闭spring整合kafka时,消费者一直打印kafka日志
在log4j.properties中添加如下代码 log4j.logger.org.apache.kafka.common.metrics.Metrics=OFF log4j.logger.org.a ...
- spring 整合quartz的方式——简单介绍
一.继承QuartzJobBean,重写executeInternal方法 <bean name="statQuartzJob" class="org.spring ...
- Spring整合redis配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
随机推荐
- Python调用ffpmeg和ffprobe处理视频文件
需求: 运营有若干批次的视频.有上千个,视频文件,有mp4格式的,有ts格式的 现在有需要去掉视频文件片头和片尾的批量操作需求. 比如 文件夹A下面的视频去掉片尾10秒 文件夹B下面的视频去掉片头6秒 ...
- Caused by: io.protostuff.ProtobufException: Protocol message contained an invalid tag (zero).
[ERROR] com.xxxx.redis.RedisClientTemplate.getOject(RedisClientTemplate.java:60):http-bio-8080-exec- ...
- LDAP认证模式简介
今天发现公共服务中有ldap数据库服务,先大概了解一下ldap,转载下面的文章.原文链接:https://www.jianshu.com/p/d3f8c8f5d661 另外记录一篇文章地址:https ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- AI与RPA
RPA(机器人流程自动化)是一类自动化软件工具,它可以通过用户界面使用和理解企业已有的应用,将基于规则的常规操作自动化,例如读取邮件和系统,计算,生成文件和报告,检查文件等.因此,RPA的应用范围非常 ...
- 用GDB调试程序(二)
GDB的命令概貌——————— 启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如下所示: /home/hchen> g ...
- Spring线程池的5个要素
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...
- [leetcode]335. Self Crossing
You are given an array x of n positive numbers. You start at point (,) and moves x[] metres to the n ...
- Python __slots__限制动态添加变量
Python是一种非常灵活的动态语言,有时感觉太灵活以至于不知道遵循什么样的规则去驾驭.不过Python已经是非常完备的语言,想实现什么样的功能都是有方法的,而且也很容易,比如限制一个类动态添加成员变 ...
- 详解vue组件的is特性:限制元素&动态组件
在vue.js组件教程的一开始提及到了is特性 意思就是有些元素,比如 ul 里面只能直接包含 li元素,像这样: <ul> <li></li> </ul&g ...