<?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"> <context:component-scan base-package="org.well.well"></context:component-scan> <!-- 基本的配置参数 可以写成配置文件或者这种${bootstrap.servers} 配置文件获取的 可以区分开发测试环境 --> <bean id="consumerProperties" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="${bootstrap.servers}"/> <entry key="group.id" value="${bootstrap.groupid}"/> <entry key="enable.auto.commit" value="false"/> <entry key="auto.commit.interval.ms" value="60000"/> <entry key="session.timeout.ms" value="60000"/> <!--<entry key="max.poll.interval.ms" value="60000"/>--> <entry key="auto.offset.reset=latest" value="latest"/> <entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"/> <entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"/> </map> </constructor-arg> </bean> <!-- 创建工厂 然后把配置信息注入--> <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory"> <constructor-arg> <ref bean="consumerProperties"/> </constructor-arg> </bean> <!-- 把实际消费的类关联进来 --> <bean id="messageListernerConsumerService" class="org.well.well.kafka.KafkaConsumer"/> <!-- 然后把这个类和消费的topic注入这个container topic也配置成灵活的 --> <bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties"> <constructor-arg name="topics" value="${kafka.topic}"/> <property name="messageListener" ref="messageListernerConsumerService"/> </bean> <!-- 把这个container和factory 注入 --> <bean id="messageListenerContainer" class="org.springframework.kafka.listener.KafkaMessageListenerContainer" init-method="doStart"> <constructor-arg ref="consumerFactory"/> <constructor-arg ref="containerProperties"/> </bean> </beans>