下载配置kafka参考该链接:http://www.cnblogs.com/super-d2/p/4534323.html

pom.xml:

        <dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.2.1</version>
</dependency> <dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-kafka</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

producer配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
http://www.springframework.org/schema/integration/kafka
http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
"> <!--kafka config-->
<int:channel id="inputToKafka"/> <int-kafka:outbound-channel-adapter kafka-producer-context-ref="kafkaProducerContext"
auto-startup="true"
channel="inputToKafka"
order="1">
</int-kafka:outbound-channel-adapter> <bean id="producerProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="topic.metadata.refresh.interval.ms">3600000</prop>
<prop key="message.send.max.retries">5</prop>
<prop key="send.buffer.bytes">5242880</prop>
</props>
</property>
</bean> <bean id="stringSerializer" class="org.apache.kafka.common.serialization.StringSerializer"/> <int-kafka:producer-context id="kafkaProducerContext" producer-properties="producerProperties">
<int-kafka:producer-configurations> <int-kafka:producer-configuration broker-list="localhost:9092"
key-serializer="stringSerializer"
value-class-type="java.lang.String"
value-serializer="stringSerializer"
topic="helloworldTopic"/> </int-kafka:producer-configurations>
</int-kafka:producer-context>
</beans>

consumer配置:

<?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"
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"> <int:channel id="inputFromKafka"/> <int-kafka:zookeeper-connect id="zookeeperConnect" zk-connect="localhost:2181"
zk-connection-timeout="6000"
zk-session-timeout="6000"
zk-sync-time="2000"/> <int-kafka:inbound-channel-adapter
id="kafkaInboundChannelAdapter"
kafka-consumer-context-ref="consumerContext"
auto-startup="false"
channel="inputFromKafka">
<int:poller fixed-delay="1" time-unit="MILLISECONDS"/>
</int-kafka:inbound-channel-adapter> <bean id="kafkaDecoder" class="org.springframework.integration.kafka.serializer.common.StringDecoder">
</bean> <bean id="consumerProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="auto.offset.reset">smallest</prop>
<prop key="socket.receive.buffer.bytes">10485760</prop> <!-- 10M -->
<prop key="fetch.message.max.bytes">5242880</prop>
<prop key="auto.commit.interval.ms">1000</prop>
</props>
</property>
</bean> <bean id="kafkaService" class="cn.innmall.union.function.service.work.KafkaService">
</bean> <int:outbound-channel-adapter channel="inputFromKafka"
ref="kafkaService" method="processMessage" /> <int-kafka:consumer-context id="consumerContext"
consumer-timeout="1000"
zookeeper-connect="zookeeperConnect" consumer-properties="consumerProperties">
<int-kafka:consumer-configurations>
<int-kafka:consumer-configuration group-id="default1"
value-decoder="kafkaDecoder"
key-decoder="kafkaDecoder"
max-messages="5000">
<int-kafka:topic id="helloworldTopic" streams="4"/>
</int-kafka:consumer-configuration>
</int-kafka:consumer-configurations>
</int-kafka:consumer-context>
</beans>

producer 测试代码:

public class KafkaServiceImpl implements KafkaService {
@Autowired
@Qualifier("inputToKafka")
MessageChannel channel; public void sendUserInfo(String key, Object obj) {
Message msg = MessageBuilder.withPayload(obj)
.setHeader("kafkaUser", key)
.setHeader(KafkaHeaders.TOPIC, "helloworldTopic").build();
channel.send(msg);
}
}

consumer测试代码:

package cn.innmall.union.function.service.work;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.Map; /**
* Created by yujinghui on 4/23/16.
*
*/
public class KafkaService {
static final Logger logger = LoggerFactory.getLogger(KafkaService.class); public void processMessage(Map<String, Map<Integer, String>> msgs) {
for (Map.Entry < String,Map<Integer, String>>entry:
msgs.entrySet()){
System.out.println("Consumer Message received: ");
logger.debug("Suchit Topic:" + entry.getKey());
for (String msg : entry.getValue().values()) {
logger.info("Suchit Consumed Message: " + msg);
}
}
} }

Kafka spring 集成的更多相关文章

  1. Spring集成kafka,消费者运行时内存占用会一直增长

    Spring集成kafka,消费者运行时内存占用会一直增长? 20C 本人用Spring集成kafka消费者,发布运行时内存占用会一直升高,最后程序挂掉.请各位大神看看,提供解决方法 以下是我的配置文 ...

  2. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  3. 阿里RocketMq试用记录+简单的Spring集成

    CSDN学院招募微信小程序讲师啦 程序猿全指南,让[移动开发]更简单! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! 阿里RocketMq试用记录+简单的S ...

  4. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  5. 【转】Dubbo使用例子并且和Spring集成使用

    一.编写客户端和服务器端共用接口类1.登录接口类public interface LoginService {    public User login(String name, String psw ...

  6. 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)

    硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...

  7. axis2+spring集成

    转载自:http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html 1.新建一个web project项目,最终工程目录如下: 注意 ...

  8. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  9. 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)

    从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...

随机推荐

  1. php学习之路:WSDL详细解释(两)

    3.定义服务使用的逻辑消息 当服务的操作被调用时.服务被定义为消息交换.在wsdl文档中,这些消息被定义message元素. 这些消息由称之为part元素的部分组成. 一个服务的操作,通过指定逻辑消息 ...

  2. 正则、grep、sed、awk

    每次用到正则都要蛋疼一下,索性总结一下在这里. 正则 正則表達式主要分为基础正则和扩展正则.注意,正则和一般命令行输入的命令的通配符不同.正则仅仅使用于支持这样的表示法的工具,如:vi,grep,se ...

  3. 依据Uri获得sd卡图片

    <pre name="code" class="java">String mBigImage = URLEncoder.encode(mImageU ...

  4. 第五十九届冠军(使用C++设计一个类不能被继承)

    称号:使用C++设计一个类不能被继承. 分析:这是Adobe 公司2007 的笔试题最新校园招聘. 应聘者的C++基本功底外,还能考察反应能力,是一道非常好的题目. 分析:C++中父类的构造函数会调用 ...

  5. JUnit实战(2) - JUnit核心(使用Suite来组合测试)

    创建Java Project项目:ch02-internals MasterTestSuite.java package com.manning.junitbook.ch02.internals; i ...

  6. java中System类简介(转)

    上次面试中遇到的一个问题,问到System.out.println()中的out是不是内部类[不是内部类],当时就给问蒙了,直观感觉out应该是System类的一个属性,跟内部类有什么关系?而且之前整 ...

  7. 恢复SQLSERVER被误删除的数据

    原文:恢复SQLSERVER被误删除的数据 恢复SQLSERVER被误删除的数据 曾经想实现Log Explorer for SQL Server的功能,利用ldf里面的日志来还原误删除的数据 这里有 ...

  8. 如何获得android手机通讯录的字母显示(两)

    随后的写如何使各第一字母显示相同的分类触点: 于adapter implement SectionIndexer 这项adapter必须在下面可以实现3接口: @Override public Obj ...

  9. .NET缓存框架CacheManager---1、CacheManager的介绍

    在我们开发的很多分布式项目里面(如基于WCF服务.Web API服务方式),由于数据提供涉及到数据库的相关操作,如果客户端的并发数量超过一定的数量,那么数据库的请求处理则以爆发式增长,如果数据库服务器 ...

  10. 使用JAVA打开本地应用程序相关的文件

    在该项目中需要运行本地文件或应用程序,JDK6添加后Desktop类别.可以直接使用.这使得有可能在程序中无论什么应用程序可以打开的.例:打开pdf文件,当地福昕是默认打开.执行程序将使用福昕开放pd ...