kafka消息驱动的适配器无法正常工作

时间:2015-11-05 19:11:15

标签: spring spring-integration apache-kafka

想使用kafka:message-driven-channel-adapter消费消息。

在以下渠道中制作讯息: headers ['topic'] = inMsge_topic,emailMesge_topic

在互联网上也没有找到同样的好例子。请建议。

使用时工作正常 int-kafka:inbound-channel-adapter但它需要轮询。(想要不进行轮询)

以下配置使用:

 <int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="kafkaProducerContext"
                                                                 auto-startup="true" channel="inputToKafka" message-key="kafka_messageKey">
          <int:poller fixed-delay="1000" time-unit="MILLISECONDS" receive-timeout="0" task-executor="taskExecutor" /> 
   </int-kafka:outbound-channel-adapter>

   <int-kafka:producer-context id="kafkaProducerContext"
          producer-properties="producerProperties">
          <int-kafka:producer-configurations>
                <int-kafka:producer-configuration
                       broker-list="${kafka.producer.brokerList}" topic="headers['topic']" key-class-type="java.lang.String"
                       value-class-type="com.vo.MessageVO"
                       value-encoder="kafkaEncoder" key-encoder="kafkaKeyEncoder"
                       compression-type="none" />
          </int-kafka:producer-configurations>
   </int-kafka:producer-context>    
   <int:channel id="inputToKafka">
     <int:queue />
   </int:channel>

  <int:channel id="inputFromKafka">
   </int:channel>
 <bean id="kafkaConfiguration" class="org.springframework.integration.kafka.core.ZookeeperConfiguration">
   <constructor-arg ref="zookeeperConnect"/>

 <bean id="connectionFactory" class="org.springframework.integration.kafka.core.DefaultConnectionFactory">
    <constructor-arg ref="kafkaConfiguration"/>

      <int-kafka:message-driven-channel-adapter
        id="adapter"
        channel="inputFromKafka"
        connection-factory="connectionFactory"
        key-decoder="kafkaKeyDecoder"
        payload-decoder="kafkaDecoder"          
        max-fetch="100"
        topics="inMsge_topic"/>

        <int-kafka:message-driven-channel-adapter
        id="adapter1"
        channel="inputFromKafka"
        connection-factory="connectionFactory"
        key-decoder="kafkaKeyDecoder"
        payload-decoder="kafkaDecoder"          
        max-fetch="100"
        topics="emailMesge_topic"/>


    <int-kafka:zookeeper-connect id="zookeeperConnect"
    zk-connect="localhost:2181" zk-connection-timeout="6000"
    zk-session-timeout="400" zk-sync-time="200" />

日志输出:

20:01:13.130 [pool-5-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 2] @ 0 20:01:13.131 [pool-5-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 4] @ 0 20:01:13.131 [pool-5-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区读取[topic ='inMsge_topic',id = 1] @ 0 20:01:13.131 [pool-5-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 0] @ 0 20:01:13.131 [pool-5-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 3] @ 1913 20:01:13.134 [pool-11-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 1] @ 0 20:01:13.134 [pool-11-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 3] @ 1913 20:01:13.134 [pool-11-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区读取[topic ='inMsge_topic',id = 4] @ 0 20:01:13.134 [pool-11-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区读取[topic ='inMsge_topic',id = 2] @ 0 20:01:13.134 [pool-11-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='inMsge_topic',id = 0] @ 0 20:01:13.158 [pool-7-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区读取[topic ='emailMesge_topic',id = 1] @ 0 20:01:13.158 [pool-7-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 3] @ 334 20:01:13.158 [pool-7-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区读取[topic ='emailMesge_topic',id = 0] @ 0 20:01:13.158 [pool-7-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 4] @ 0 20:01:13.158 [pool-7-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区读取[topic ='emailMesge_topic',id = 2] @ 0 20:01:13.164 [pool-13-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 1] @ 0 20:01:13.164 [pool-13-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 4] @ 0 20:01:13.164 [pool-13-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 0] @ 0 20:01:13.164 [pool-13-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 3] @ 334 20:01:13.164 [pool-13-thread-1] DEBUG o.s.i.kafka.core.DefaultConnection - 从分区中读取[topic ='emailMesge_topic',id = 2] @ 0

1 个答案:

答案 0 :(得分:1)

看一下Spring Integration kafka sample - 它使用Java配置而不是XML,但它显示了出站和消息驱动的适配器。