Kafka在SpringBoot中的简单应用

1.pom引入相应maven坐标



    org.springframework.kafka
    spring-kafka

由于kafka受版本限制,所以引入jar包时不必引入version版本,默认安装最新版本即可

2.配置文件application.yml的相关参数配置如下:

kafka:
  bootstrap-servers: 127.0.0.1:9092
  #设置默认组
  consumer:
    group-id: 0
    key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
  producer:
    key-serializer: org.apache.kafka.common.serialization.StringSerializer
    value-serializer: org.apache.kafka.common.serialization.StringSerializer
    batch-size: 65536
    buffer-memory: 524288

3.创建kafka消息发送方

package com.ghg.vue.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("kafka")
public class KafkaProducer {

    private static final Logger logger = LoggerFactory.getLogger(KafkaProducer.class);
    //自定义topic
    public static final String TOPIC_ONE = "topic.one";

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @RequestMapping("/send")
    public String send(@RequestParam("msg") String msg) {
        logger.info("要发送的消息:{}" ,msg);
        //发送消息
        ListenableFuture> future = kafkaTemplate.send(TOPIC_ONE, msg);
        future.addCallback(new ListenableFutureCallback>() {
            @Override
            public void onFailure(Throwable throwable) {
                //发送失败的处理
                logger.info(TOPIC_ONE + " - 生产者 发送消息失败:" + throwable.getMessage());
            }

            @Override
            public void onSuccess(SendResult stringStringSendResult) {
                //处理成功
                logger.info(TOPIC_ONE + "-生产者 发送消息成功:" + stringStringSendResult.toString());
            }
        });
        return "0000";
    }
}

4.创建kafka消息消费方

package com.ghg.vue.controller;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaHandler;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.KafkaListeners;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class KafkaConsumer {

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

    @KafkaListener(topics = KafkaProducer.TOPIC_ONE,groupId = KafkaProducer.TOPIC_ONE)
    public void topic_one(ConsumerRecord record, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            logger.info("被"+KafkaProducer.TOPIC_ONE+"消费了: ++++++++ Topic"+topic+",Record:"+record+",Message:"+msg);
        }
    }

    @KafkaListener(topics = KafkaProducer.TOPIC_ONE,groupId = KafkaProducer.TOPIC_ONE)
    public void topic_two(ConsumerRecord record, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            logger.info("被"+KafkaProducer.TOPIC_ONE+"1111消费了: ++++++++ Topic"+topic+",Record:"+record+",Message:"+msg);
        }
    }

}

上述消费之消费方式是广播消费模式,每一个绑定发送者topic的消费者都可以接收到相应的消息

 

5.kafka在window上的安装

请参考Windows平台kafka环境的搭建

你可能感兴趣的:(消息队列,kafka)