Springboot简单集成kafka

Springboot简单集成kafka

本篇主要介绍如何再Springboot项目中如何使用kafka进行消息的生产与消费
1、引入jar包

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

2、配置文件中加入kafka相关配置

spring.kafka.bootstrap-servers = 10.11.255.57:19092,10.11.255.105:19092,10.11.255.117:19092
spring.kafka.producer.acks = -1
spring.kafka.producer.batch-size = 16384
spring.kafka.producer.buffer-memory = 33554432
spring.kafka.producer.retries = 3
spring.kafka.producer.compression-type = gzip
spring.kafka.consumer.group-id = eccs
spring.kafka.consumer.enable-auto-commit = false
spring.kafka.consumer.auto-offset-reset = latest

3、新建kafka生产者类

package com.example.demo.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFutureCallback;

@Component
public class KafkaProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * Kafka主题
     */
    @Value("${kafkaTopic.testTopic:test_kafka}")
    private String topic;

    public void pushKafkaTest(String test) {
        try {
            kafkaTemplate.send(topic, test).addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {

                @Override
                public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
                    System.out.println("kafka发送成功");
                }

                @Override
                public void onFailure(Throwable throwable) {
                    System.out.println("kafka发送失败");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

4、创建Kafka消费者

package com.example.demo.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
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 {

    @KafkaListener(topics = "test_kafka")
    public void kafkaConsumer(ConsumerRecord<?, ?> record, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            String message = kafkaMessage.get().toString();
            System.out.println("Kafka消费成功 "+message);
        }
    }
}


5、Service调用Kafka生产者

package com.example.demo.service.impl;

import com.example.demo.kafka.KafkaProducer;
import com.example.demo.service.KafkaTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KafkaTestServiceImpl implements KafkaTestService {

    @Autowired
    private KafkaProducer kafkaProducer;

    @Override
    public void testKafka(String test) {
        kafkaProducer.pushKafkaTest(test);
    }
}

最终使用postman进行生产消费测试
Springboot简单集成kafka_第1张图片这样便完成了kafka的生产和消费调用

你可能感兴趣的:(Springboot)