Kafka spring 集成

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);
            }
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值