kafka spring-integration 配置上之后不起作用

下个路口 发表于: 2017-08-30   最后更新时间: 2017-08-30 13:59:18   3,344 游览

spring配置上之后不起作用

生产者xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:int="https://www.springframework.org/schema/integration"
    xmlns:int-kafka="https://www.springframework.org/schema/integration/kafka"
    xmlns:task="https://www.springframework.org/schema/task"
    xsi:schemaLocation="https://www.springframework.org/schema/integration/kafka https://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
        https://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
        https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        https://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd">

   <!-- commons config -->
    <bean id="byteArraySerializer" class="org.apache.kafka.common.serialization.ByteArraySerializer"/>
    <bean id="stringSerializer" class="org.apache.kafka.common.serialization.StringSerializer"/>
    <bean id="kafkaEncoder" class="org.springframework.integration.kafka.serializer.avro.AvroReflectDatumBackedKafkaEncoder">
        <constructor-arg value="java.lang.String" />
    </bean>
    <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="serializer.class">kafka.serializer.StringEncoder</prop>
                <prop key="partitioner.class">com.wanrong.payment.commons.MyPartitioner</prop>
                <prop key="request.required.acks">-1</prop>
                <prop key="send.buffer.bytes">5242880</prop>
                <prop key="request.timeout.ms">10000</prop>
                <prop key="retry.backoff.ms">200</prop>
                <prop key="queue.buffering.max.ms">500</prop>
                <prop key="queue.buffering.max.messages">10000</prop>
                <prop key="value.serializer">org.apache.kafka.common.serialization.ByteArraySerializer</prop>
                <prop key="key.serializer">org.apache.kafka.common.serialization.ByteArraySerializer</prop>
            </props>
        </property>
    </bean>

    <!-- topic test config  -->

    <int:channel id="kafkaTopicTest">
        <int:queue />
    </int:channel>

    <int-kafka:outbound-channel-adapter
        id="kafkaOutboundChannelAdapterTopicTest" kafka-producer-context-ref="producerContextTopicTest"
        auto-startup="true" channel="kafkaTopicTest" order="2" >
        <int:poller fixed-delay="1000" time-unit="MILLISECONDS"
            receive-timeout="1" task-executor="taskExecutor" />
    </int-kafka:outbound-channel-adapter>
    <task:executor id="taskExecutor" pool-size="5"
        keep-alive="120" queue-capacity="500" />
    <!-- <bean id="kafkaEncoder"
        class="org.springframework.integration.kafka.serializer.avro.AvroSpecificDatumBackedKafkaEncoder">
        <constructor-arg value="com.company.AvroGeneratedSpecificRecord" />
    </bean> -->
    <int-kafka:producer-context id="producerContextTopicTest"
        producer-properties="producerProperties">
        <int-kafka:producer-configurations>
            <!-- 多个topic配置 -->
            <int-kafka:producer-configuration
                broker-list="192.168.2.187:9092"
                key-serializer="stringSerializer"
                value-class-type="java.lang.String"
                value-serializer="stringSerializer"
                topic="test1" />
            <!-- <int-kafka:producer-configuration
                broker-list="192.168.2.132:9092,192.168.2.132:9093"
                key-serializer="stringSerializer"
                value-class-type="java.lang.String"
                value-serializer="stringSerializer"
                topic="emailtopic" /> -->
        </int-kafka:producer-configurations>
    </int-kafka:producer-context>
</beans>

消费者文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:int="https://www.springframework.org/schema/integration"
    xmlns:int-kafka="https://www.springframework.org/schema/integration/kafka"
    xmlns:task="https://www.springframework.org/schema/task"
    xsi:schemaLocation="https://www.springframework.org/schema/integration/kafka
                        https://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
                        https://www.springframework.org/schema/integration
                        https://www.springframework.org/schema/integration/spring-integration.xsd
                        https://www.springframework.org/schema/beans
                        https://www.springframework.org/schema/beans/spring-beans.xsd
                        https://www.springframework.org/schema/task
                        https://www.springframework.org/schema/task/spring-task.xsd">

    <!-- topic test conf -->
    <int:channel id="inputFromKafka" >
        <int:dispatcher task-executor="kafkaMessageExecutor" />
    </int:channel>
    <!-- zookeeper配置 可以配置多个 -->
    <int-kafka:zookeeper-connect id="zookeeperConnect"
        zk-connect="192.168.2.187:2181" zk-connection-timeout="6000"
        zk-session-timeout="6000" zk-sync-time="2000" />
    <!-- channel配置 auto-startup="true"  否则接收不发数据 -->
    <int-kafka:inbound-channel-adapter
        id="kafkaInboundChannelAdapter" kafka-consumer-context-ref="consumerContext"
        auto-startup="true" channel="inputFromKafka">
        <int:poller fixed-delay="1" time-unit="MILLISECONDS" />
    </int-kafka:inbound-channel-adapter>
    <task:executor id="kafkaMessageExecutor" pool-size="8" keep-alive="120" queue-capacity="500" />
    <bean id="kafkaDecoder"
        class="org.springframework.integration.kafka.serializer.common.StringDecoder" />

    <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>
    <!-- 消息接收的BEEN -->
    <bean id="kafkaConsumerService" class="cn.baiing.service.KafkaConsumerService" />
    <!-- 指定接收的方法 -->
    <int:outbound-channel-adapter channel="inputFromKafka"
        ref="kafkaConsumerService" 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">
                <!-- 两个TOPIC配置 -->
                <int-kafka:topic id="mytopic" streams="4" />
                <int-kafka:topic id="sunneytopic" streams="4" />
            </int-kafka:consumer-configuration>
        </int-kafka:consumer-configurations>
    </int-kafka:consumer-context>
</beans>
发表于 2017-08-30
添加评论

这个很抽象。。最好debug模式调试。分别测试生产者和消费者。

下个路口 -> 半兽人 7年前

是不是这个只能在spring-boot上面进行集成,普通的spring不能集成

半兽人 -> 下个路口 7年前

逻辑是一样的,前提是你要加载到才行。

我项目启动是没啥问题的,都能正常跑我也在zookeeper上看到了我这边联上了,但是发送消息之后,用命令实施监控就是得不到消息。
都给我整麻了。。。。。。。。。。。,求大神帮下忙/(ㄒoㄒ)/~~

半兽人 -> 下个路口 7年前

关键是我只能根据你的问题,帮你分析。/(ㄒoㄒ)/~~
你debug下试试看看kafka代码的日志。

你的答案

查看kafka相关的其他问题或提一个您自己的问题