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

pom.xml:

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka_2.10</artifactId>
  4. <version>0.8.2.1</version>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>org.springframework.integration</groupId>
  9. <artifactId>spring-integration-kafka</artifactId>
  10. <version>1.2.0.RELEASE</version>
  11. </dependency>

producer配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
  7. xmlns:int="http://www.springframework.org/schema/integration"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  15. http://www.springframework.org/schema/integration
  16. http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
  17. http://www.springframework.org/schema/integration/kafka
  18. http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
  19. ">
  20.  
  21. <!--kafka config-->
  22. <int:channel id="inputToKafka"/>
  23.  
  24. <int-kafka:outbound-channel-adapter kafka-producer-context-ref="kafkaProducerContext"
  25. auto-startup="true"
  26. channel="inputToKafka"
  27. order="1">
  28. </int-kafka:outbound-channel-adapter>
  29.  
  30. <bean id="producerProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  31. <property name="properties">
  32. <props>
  33. <prop key="topic.metadata.refresh.interval.ms">3600000</prop>
  34. <prop key="message.send.max.retries">5</prop>
  35. <prop key="send.buffer.bytes">5242880</prop>
  36. </props>
  37. </property>
  38. </bean>
  39.  
  40. <bean id="stringSerializer" class="org.apache.kafka.common.serialization.StringSerializer"/>
  41.  
  42. <int-kafka:producer-context id="kafkaProducerContext" producer-properties="producerProperties">
  43. <int-kafka:producer-configurations>
  44.  
  45. <int-kafka:producer-configuration broker-list="localhost:9092"
  46. key-serializer="stringSerializer"
  47. value-class-type="java.lang.String"
  48. value-serializer="stringSerializer"
  49. topic="helloworldTopic"/>
  50.  
  51. </int-kafka:producer-configurations>
  52. </int-kafka:producer-context>
  53. </beans>

consumer配置:

  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"
    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">
  2.  
  3. <int:channel id="inputFromKafka"/>
  4.  
  5. <int-kafka:zookeeper-connect id="zookeeperConnect" zk-connect="localhost:2181"
    zk-connection-timeout="6000"
    zk-session-timeout="6000"
    zk-sync-time="2000"/>
  6.  
  7. <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>
  8.  
  9. <bean id="kafkaDecoder" class="org.springframework.integration.kafka.serializer.common.StringDecoder">
    </bean>
  10.  
  11. <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>
  12.  
  13. <bean id="kafkaService" class="cn.innmall.union.function.service.work.KafkaService">
    </bean>
  14.  
  15. <int:outbound-channel-adapter channel="inputFromKafka"
    ref="kafkaService" method="processMessage" />
  16.  
  17. <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 测试代码:

  1. public class KafkaServiceImpl implements KafkaService {
  2. @Autowired
  3. @Qualifier("inputToKafka")
  4. MessageChannel channel;
  5.  
  6. public void sendUserInfo(String key, Object obj) {
  7. Message msg = MessageBuilder.withPayload(obj)
  8. .setHeader("kafkaUser", key)
  9. .setHeader(KafkaHeaders.TOPIC, "helloworldTopic").build();
  10. channel.send(msg);
  11. }
  12. }

consumer测试代码:

  1. package cn.innmall.union.function.service.work;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5.  
  6. import java.util.Map;
  7.  
  8. /**
  9. * Created by yujinghui on 4/23/16.
  10. *
  11. */
  12. public class KafkaService {
  13. static final Logger logger = LoggerFactory.getLogger(KafkaService.class);
  14.  
  15. public void processMessage(Map<String, Map<Integer, String>> msgs) {
  16. for (Map.Entry < String,Map<Integer, String>>entry:
  17. msgs.entrySet()){
  18. System.out.println("Consumer Message received: ");
  19. logger.debug("Suchit Topic:" + entry.getKey());
  20. for (String msg : entry.getValue().values()) {
  21. logger.info("Suchit Consumed Message: " + msg);
  22. }
  23. }
  24. }
  25.  
  26. }

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. 我的java学习笔记(一)

    第一个java程序,还是熟悉的hello world public class FirstSample { //类 public static void main(String[] args) //方 ...

  2. Android checkbox和radiobutton 以及Toast和AlertDialog的使用

    package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...

  3. Deep Learning Papers

    一.Image Classification(Recognition) lenet: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf alexn ...

  4. hibernate 单元測试框架

    hibernate在写数据库配置文件时很的不确定,必须进行必要的測试保证数据库结构的正确性.所以能够应用junit进行測试. 使用junit很easy,eclipse仅仅须要右键项目新建一个junit ...

  5. java多线程学习(一)

    一.操作系统线程和进程的概念 线程是指进程中的一个运行单元,这个过程中也可调度实体. 线程与进程的差别: (1)地址空间:线程为进程内的一个运行单元.进程至少有一个线程(进程的主线程):进程的全部线程 ...

  6. ASP.NET MVC:Razor 引入命名空间

    原文:ASP.NET MVC:Razor 引入命名空间 页面中引用 c# @using MvcApplication83.Models @using MvcApplication83.Common 行 ...

  7. OCP读书笔记(23) - 题库(ExamC)

    200.Which operation requires that you create an auxiliary instance manually before executing the ope ...

  8. 解决RecyclerView无法onItemClick问题

    供RecyclerView采用.会员可以查看将替代ListView的RecyclerView 的使用(一),单单从代码结构来说RecyclerView确实比ListView优化了非常多.也简化了我们编 ...

  9. Ubuntu 中查看内核版本和系统版本的三个命令

    一.查看内核版本:cat /proc/version 二.查看内核版本:uname -a 三.查看系统版本:lsb_release -a 四.查看发行版类型:cat /etc/issue

  10. DropDownListFor使用ViewData进行绑定的示例

    特别注意,经实践: 此方法的ViewBag的名称必须和new SelectList()中的最后一个参数,即下拉框的默认值的名称必须相同,如: ViewBag.Title = WebConst.UnSe ...