Kafka spring 集成

下载配置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);
            }
        }
    }

}
原文地址:https://www.cnblogs.com/yujinghui/p/5424706.html