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(配置文件方式 生产者)的更多相关文章

  1. spring整合kafka(配置文件方式 消费者)

    Kafka官方文档有   https://docs.spring.io/spring-kafka/reference/htmlsingle/ 这里是配置文件实现的方式 先引入依赖 <depend ...

  2. spring整合hibernate配置文件

    Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置 spring对hibernate配置文件hibernate.cfg.x ...

  3. spring 整合kafka监听消费

    前言 最近项目里有个需求,要消费kafka里的数据.之前也手动写过代码去消费kafka数据.但是转念一想.既然spring提供了消费kafka的方法.就没必要再去重复造轮子.于是尝试使用spring的 ...

  4. spring整合kafka项目生产和消费测试结果记录(一)

    使用spring+springMVC+mybatis+kafka做了两个web项目,一个是生产者,一个是消费者. 通过JMeter测试工具模拟100个用户并发访问生产者项目,发送json数据给生产者的 ...

  5. hibernate3和spring整合的一些方式

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. Ibatis,Spring整合(注解方式注入)

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  7. 关闭spring整合kafka时,消费者一直打印kafka日志

    在log4j.properties中添加如下代码 log4j.logger.org.apache.kafka.common.metrics.Metrics=OFF log4j.logger.org.a ...

  8. spring 整合quartz的方式——简单介绍

    一.继承QuartzJobBean,重写executeInternal方法 <bean name="statQuartzJob" class="org.spring ...

  9. Spring整合redis配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

随机推荐

  1. c#利用ApplicationContext类 同时启动双窗体的实现

    Application类(位于System.Windows.Forms命名空间)公开了Run方法,可以调用该方法来调度应用程序进入消息循环.Run方法有三个重载 1.第一个重载版本不带任何参数,比较少 ...

  2. Python tkinter模块和参数

    转自:https://www.cnblogs.com/aland-1415/p/6849193.html 1.使用tkinter.Tk() 生成主窗口(root=tkinter.Tk()):root. ...

  3. JVMj机制

  4. 深入理解CSS系列(一):理解CSS的盒子模型

    接触前端也有好几个年头了,但是,讲实话,对于CSS的理解真的是不敢恭维,相信很多同行也有类似的感受吧!这是为什么呢?因为我们都认为CSS太简单了,没有必要深入学习,果真如此?其实,只不过是自己图样图森 ...

  5. pac4j探索(一)之buji-pac4j shiro整合Cas实现单点登录

    https://blog.csdn.net/hxm_code/article/details/79181218 https://blog.csdn.net/hxm_code/article/detai ...

  6. 【WebLogic使用】1.WebLogic的下载与安装

    一.WebLogic的介绍    WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogic ...

  7. QThread的一些使用心得

    qthread这个类主要用于解决pyqt中多线程的问题,常见用法是将后台代码封装在该类的run()方法中,再对qthread对象执行start()函数来启动线程. 但往往一个业务会有粒度划分,多个粒度 ...

  8. 黄聪:xampp启动后mysql报Error

    2013-08-04 13:48:22 760 [ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous t ...

  9. 并发之lock的condition接口

    13.死磕Java并发-----J.U.C之Condition 12.Condition使用总结 11.Java并发编程系列之十七:Condition接口 === 13.死磕Java并发-----J. ...

  10. docker 在window10下的安装

    在win10下安装docker 打开下载页面 https://store.docker.com/editions/community/docker-ce-desktop-windows 打开控制面板 ...