美文网首页Kafka
spring-integration-kafka

spring-integration-kafka

作者: 会点代码的大叔 | 来源:发表于2016-04-21 11:08 被阅读5455次

    单位,新突击做了一个项目,用的新技术比较多一些,说是新技术,只是我之前没有接触过,kafka是其中之一。
    因为项目用到了spring,所以在使用很多开源框架的时候,都选择使用spring进行集成。而kafka的使用,我们是使用到了spring-integration-kafka。

    1、pom.xml

    项目使用maven进行构建,使用spring-integration-kafka的话,需要引入相关jar包:

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-kafka</artifactId>
        <version>2.0.0.M1</version>
    </dependency>
    

    spring选择的是4.1.6.RELEASE:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    

    2、发送消息

    2.1配置文件

    <?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"
        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-2.0.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
            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-4.1.xsd">
    
    <context:property-placeholder location="classpath:*.properties" />
    <!-- 生产消息 -->
    <int:channel id="inputToKafka"></int:channel>
    <int-kafka:outbound-channel-adapter kafka-template="kafkaTemplate"  channel="inputToKafka" topic="test"/>
    <bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
        <constructor-arg index="0" ref="producerFactory"></constructor-arg>
    </bean>
    <bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
        <constructor-arg>
            <!-- key 详见org.apache.kafka.clients.producer.ProducerConfig中的常量用于调优kafka参数 -->
            <map>
                <entry key="bootstrap.servers" value="${broker.address}"/>
                <entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
                <entry key="value.serializer" value="org.apache.kafka.common.serializatbroker.addression.StringSerializer"></entry>
            </map>
        </constructor-arg>
    </bean>
    </beans>
    

    topic="test" 需要修改成对应的topoc的名称

    kafka.properties:

    broker.address=localhost:9092
    broker.groupid=mygroup
    

    2.2代码

    package com.taikang.kafka.demo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.messaging.Message;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.support.GenericMessage;
    
    public class Server {
        
        public static void main(String[] args) {
            //ApplicationContext context =new ClassPathXmlApplicationContext("spring-produce.xml");
            final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-produce.xml", Server.class);
            context.start();
            
            MessageChannel messageChannel=(MessageChannel) context.getBean("inputToKafka");
            for(int i=0;i<15;i++){
                Message<String> message=new GenericMessage<String>("test-------------");
                boolean flag=messageChannel.send(message);
                
                //boolean flag = messageChannel.send(message, 2 * 1000);
                System.out.println(flag);
            }
            
            try {
                System.out.println("Thread.sleep===========================");
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            context.close();
        }
    
    }
    

    3、消息接收

    3.1配置文件

    <?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"
        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-2.0.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
            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-4.1.xsd">
    
        <int-kafka:message-driven-channel-adapter
    id="kafkaListener"
    listener-container="container1"
    auto-startup="false"
    phase="100"
    send-timeout="5000"
    channel="nullChannel"
    error-channel="errorChannel" />
    
        <bean id="container1" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
        <constructor-arg>
        <bean class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
        <constructor-arg>
        <map>
            <entry key="bootstrap.servers" value="localhost:9092" />
            <entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
                            <entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
                        </map>
        </constructor-arg>
        </bean>
        </constructor-arg>
        <constructor-arg name="topics" value="test" />
        </bean>
    </beans>
    

    3.2代码

    package com.taikang.kafka.demo;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.integration.channel.QueueChannel;
    import org.springframework.messaging.Message;
    
    public class Consumer {
        public static void main(String[] args) {
            ApplicationContext context =new ClassPathXmlApplicationContext("spring-consumer.xml");
            QueueChannel queueChannel=(QueueChannel) context.getBean("inputFromKafka");
            Message msg=null;
            while((msg=queueChannel.receive(-1))!=null){
                String map=(String) msg.getPayload();
                System.out.println(map);
            }
        }
    
    }
    

    上传github失败....回头再想办法传吧...不过想起来之前scrapy的安装文件说之后上传也没传。

    相关文章

      网友评论

      • Blackie_Z:嗨,楼主 请教下spring-integration-kafka 和 sping-kafka 有什么区别?我看到官网上说 "Starting from version 2.0 version this project is a complete rewrite based on the new spring-kafka project which uses the pure java Producer and Consumer clients provided by Kafka 0.9.x.x and 0.10.x.x.." 说spring-integration-kafka2.0之后是基于spring-kafka 的,只是是这样吗?那两者有什么区别呢,你们公司为什么选择前者? 请楼主帮忙解惑,不胜感激!
      • 过去过去过:spring集成kafka的那个spring-integration...这个jar包在哪可以下载到?
        会点代码的大叔:@过去过去过 单独下载jar包的话,找一个maven中央仓库,org.springframework.integration目录找,如:http://mvnrepository.com/artifact/org.springframework.integration/spring-integration-kafka
        会点代码的大叔:@过去过去过 一般都用maven构建
        <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-kafka</artifactId>
        <version>2.0.0.M1</version>
        </dependency>
      • ec5f4c017aec:消费者是单线程的吧
      • bca7e4d5a4be:你好有代码?配置后queueChannel 拿不到东西。一个topic 10个分区。有代码能否发我一份sss130@163.com 谢谢
      • 33bb9e6feb42:inputFromKafka 从哪来的
        ec5f4c017aec:@默认 配到消费者下
        会点代码的大叔:引入需要么jar包之后,这个直接设置就行了
      • 33bb9e6feb42:多个topic怎么配置

      本文标题:spring-integration-kafka

      本文链接:https://www.haomeiwen.com/subject/wzjtrttx.html