文档章节

高吞吐量的分布式发布订阅消息系统Kafka--spring-integration-kafka的应用

sunney888
 sunney888
发布于 2016/07/05 16:27
字数 904
阅读 84
收藏 3

一、概述

    Spring Integration Kafka 是基于 Apache Kafka 和Spring Integration来集成Kafka,对开发配置提供了方便。

二、配置

    1、spring-kafka-consumer.xml

<?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"
	xmlns:task="http://www.springframework.org/schema/task"
	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
                        http://www.springframework.org/schema/task 
                        http://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.1.237: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="com.sunney.service.impl.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>

  2、spring-kafka-producer.xml

<?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"
	xmlns:task="http://www.springframework.org/schema/task"
	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
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
	
   <!-- commons config -->
	<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="request.required.acks">1</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="3">
		<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.1.237:9090,192.168.1.237:9091,192.168.1.237:9092"
				key-serializer="stringSerializer"
                value-class-type="java.lang.String"
                value-serializer="stringSerializer"
				topic="mytopic" />
			<int-kafka:producer-configuration
				broker-list="192.168.1.237:9090,192.168.1.237:9091,192.168.1.237:9092"
				key-serializer="stringSerializer"
                value-class-type="java.lang.String"
                value-serializer="stringSerializer"
				topic="sunneytopic"/>
		</int-kafka:producer-configurations>
	</int-kafka:producer-context>
</beans>

  3、发消息接口 KafkaService

package com.sunney.service;

/**
 * 类KafkaService.java的实现描述:发消息接口类
 * @author Sunney 2016年4月30日 上午11:30:53
 */
public interface KafkaService {
    /**
     * 发消息
     * @param topic 主题
     * @param obj 发送内容
     */
    public void sendUserInfo(String topic, Object obj);
}

  4、发消息实现类 KafkaServiceImpl  

package com.sunney.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.kafka.support.KafkaHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Service;

import com.sunney.service.KafkaService;

/**
 * 类KafkaServiceImpl.java的实现描述:发消息实现类 
 * @author Sunney 2016年4月30日 上午11:31:13
 */
@Service("kafkaService")
public class KafkaServiceImpl  implements KafkaService{

    @Autowired
    @Qualifier("kafkaTopicTest")
    MessageChannel channel;

    public void sendUserInfo(String topic, Object obj) {
        channel.send(MessageBuilder.withPayload(obj)
                                    .setHeader(KafkaHeaders.TOPIC,topic)
                                    .build());
    }

}

  5、消费接收类KafkaConsumerService 

package com.sunney.service.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.sunney.service.UserDto;

/**
 * 类KafkaConsumerService.java的实现描述:消费接收类
 * 
 * @author Sunney 2016年4月30日 上午11:46:14
 */
public class KafkaConsumerService {

    static final Logger logger = LoggerFactory.getLogger(KafkaConsumerService.class);

    public void processMessage(Map<String, Map<Integer, String>> msgs) {
        logger.info("===============processMessage===============");
        for (Map.Entry<String, Map<Integer, String>> entry : msgs.entrySet()) {
            logger.info("============Topic:" + entry.getKey());
            LinkedHashMap<Integer, String> messages = (LinkedHashMap<Integer, String>) entry.getValue();
            Set<Integer> keys = messages.keySet();
            for (Integer i : keys)
                logger.info("======Partition:" + i);
            Collection<String> values = messages.values();
            for (Iterator<String> iterator = values.iterator(); iterator.hasNext();) {
                String message = "["+iterator.next()+"]";
                logger.info("=====message:" + message);
                List<UserDto> userList = JSON.parseArray(message, UserDto.class);  
                logger.info("=====userList.size:" + userList.size());

            }

        }
    }

}

  6、pom

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.integration</groupId>
			<artifactId>spring-integration-kafka</artifactId>
			<version>1.3.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId> org.apache.avro</groupId>
			<artifactId>avro</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>
	</dependencies>

  六、源代码地址:https://github.com/sunney2010/kafka-demo

     七、遇到的问题

         1、消费端口收不到消息

              spring-kafka-consumer.xml的auto-startup设置为true

本文转载自:http://www.cnblogs.com/yuanermen/p/5453339.html

sunney888

sunney888

粉丝 11
博文 15
码字总数 1499
作品 0
深圳
部门经理
私信 提问
kafka学习(三)----- Kafka整合SpringMVC实例

kafka一个高吞吐量的分布式发布订阅消息系统。有关知识请参看:kafka基本概念以及环境搭建,kafka整合springMVC需要用到一个开源框架:spring-integration-kafka,这个官方框架我就不介绍了,...

火龙战士
2016/08/22
2.9K
0
JEESZ-kafka消息服务平台实现

JEESZ的消息服务平台已经抛弃了之前的ActiveMQ,改用高吞吐量比较大的Kafka分布式消息中间件方案: JEESZ-kafka消息平台使用spring+kafka的集成方案,详情如下: 1. 使用最高版本2.1.0.RELEA...

ITtechnology
2017/04/20
443
1
Spring Boot 中使用 kafka

Kafka 是一种高吞吐的分布式发布订阅消息系统,能够替代传统的消息队列用于解耦合数据处理,缓存未处理消息等,同时具有更高的吞吐率,支持分区、多副本、冗余,因此被广泛用于大规模消息数据...

PengLei
2017/10/17
0
0
Kafka、RabbitMQ、RocketMQ等消息中间件的介绍和对比

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/t4i2b10X4c22nF6A/article/details/89507748

JAVA高级架构v
2019/04/24
0
0
Spring Boot2.0 整合 Kafka

Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就...

Jreey
2018/08/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

一分快3走势回血技巧一分快3走势规律技巧28438082

:QV:46~39~82~76 :包含与数字运算有关的类。 常用静态方法 abs():取绝对值,Math.abs(-100)=100 floor():向下取值,Math.floor(10.7)=10 round():四舍五入. Collections类:addAll(Colle...

旧梦忘倾
5分钟前
31
0
哈希表如何工作?

我正在寻找哈希表如何工作的解释 - 用简单的英语表示像我这样的傻瓜! 例如,我知道它需要密钥,计算哈希值(我正在寻找解释如何)然后执行某种模数来计算它存储在存储值的数组中的位置,但这...

javail
8分钟前
4
0
0121 spring-boot-redis的使用

redis是什么呢?redis,属于NoSQL的一种,在互联网时代,起到加速系统的作用。 redis是一种内存数据库,支持7种数据类型的存储,性能1S 10w次读写; redis提供的简单的事务保证了高并发场景下...

李福春carter
36分钟前
28
0
redis的有序集合研究

假设执行下面的操作 jedis.zadd("myzset", System.currentTimeMillis(), "An error is returned when key exists but does not hold a sorted set.\r\n" + "\r\n" ......

强子大叔的码田
54分钟前
33
0
如何防止favicon.ico请求?

我没有favicon.ico,但IE总是提出请求。 是否可以阻止浏览器从我的网站请求favicon? 也许HTML标题中有一些META-TAG? #1楼 你不能。 您所能做的就是使该图像尽可能小,并在将来设置一些缓存...

技术盛宴
54分钟前
34
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部