Spring Boot 集成 Kafka的详细步骤

 更新时间:2024年07月26日 09:30:26   作者:傲雪凌霜,松柏长青  
Spring Boot与Kafka的集成使得消息队列的使用变得更加简单和高效,可以配置 Kafka、实现生产者和消费者,并利用 Spring Boot 提供的功能处理消息流,以下是 Spring Boot 集成 Kafka 的详细步骤,包括配置、生产者和消费者的实现以及一些高级特性,感兴趣的朋友一起看看吧

Spring Boot 与 Kafka 集成是实现高效消息传递和数据流处理的常见方式。Spring Boot 提供了简化 Kafka 配置和使用的功能,使得集成过程变得更加直观和高效。以下是 Spring Boot 集成 Kafka 的详细步骤,包括配置、生产者和消费者的实现以及一些高级特性。

1. 添加依赖

首先,你需要在 Spring Boot 项目的 pom.xml 文件中添加 Kafka 相关的依赖。使用 Spring Boot 的起步依赖可以简化配置。

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

2. 配置 Kafka

2.1. 配置文件

application.propertiesapplication.yml 文件中配置 Kafka 相关属性。

application.properties:

# Kafka 服务器地址
spring.kafka.bootstrap-servers=localhost:9092
# Kafka 消费者配置
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
# Kafka 生产者配置
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

application.yml:

spring:
  kafka:
    bootstrap-servers: localhost:9092
    consumer:
      group-id: my-group
      auto-offset-reset: earliest
      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

2.2. Kafka 配置类

在 Spring Boot 中,你可以使用 @Configuration 注解创建一个配置类,来定义 Kafka 的生产者和消费者配置。

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.config.ContainerProperties;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableKafka
public class KafkaConfig {
    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return new DefaultKafkaProducerFactory<>(configProps);
    }
    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        configProps.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
        configProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return new DefaultKafkaConsumerFactory<>(configProps);
    }
    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
}

3. 实现 Kafka 生产者

3.1. 生产者服务

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Service
public class KafkaProducerService {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    private static final String TOPIC = "my_topic";
    public void sendMessage(String message) {
        kafkaTemplate.send(TOPIC, message);
    }
}

3.2. 控制器示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class KafkaController {
    @Autowired
    private KafkaProducerService kafkaProducerService;
    @PostMapping("/send")
    public void sendMessage(@RequestBody String message) {
        kafkaProducerService.sendMessage(message);
    }
}

4. 实现 Kafka 消费者

4.1. 消费者服务

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class KafkaConsumerService {
    @KafkaListener(topics = "my_topic", groupId = "my-group")
    public void listen(String message) {
        System.out.println("Received message: " + message);
    }
}

5. 高级特性

5.1. 消息事务

Kafka 支持消息事务,确保消息的原子性。

生产者配置

spring.kafka.producer.enable-idempotence=true
spring.kafka.producer.transaction-id-prefix=my-transactional-id

使用事务

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.core.TransactionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class KafkaTransactionalService {
    private final KafkaTemplate<String, String> kafkaTemplate;
    private final TransactionTemplate transactionTemplate;
    public KafkaTransactionalService(KafkaTemplate<String, String> kafkaTemplate, TransactionTemplate transactionTemplate) {
        this.kafkaTemplate = kafkaTemplate;
        this.transactionTemplate = transactionTemplate;
    }
    @Transactional
    public void sendMessageInTransaction(String message) {
        kafkaTemplate.executeInTransaction(t -> {
            kafkaTemplate.send("my_topic", message);
            return true;
        });
    }
}

5.2. 异步发送与回调

异步发送

public void sendMessageAsync(String message) {
    kafkaTemplate.send("my_topic", message).addCallback(
        result -> System.out.println("Sent message: " + message),
        ex -> System.err.println("Failed to send message: " + ex.getMessage())
    );
}

总结

Spring Boot 与 Kafka 的集成使得消息队列的使用变得更加简单和高效。通过上述步骤,你可以轻松地配置 Kafka、实现生产者和消费者,并利用 Spring Boot 提供的强大功能来处理消息流。了解 Kafka 的高级特性(如事务和异步处理)能够帮助你更好地满足业务需求,确保系统的高可用性和数据一致性。

到此这篇关于Spring Boot 集成 Kafka的详细步骤的文章就介绍到这了,更多相关Spring Boot 集成 Kafka内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java应用启动停止重启Shell脚本模板server.sh

    Java应用启动停止重启Shell脚本模板server.sh

    这篇文章主要为大家介绍了Java应用启动、停止、重启Shell脚本模板server.sh,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Java文件大小转换的两种方式小结

    Java文件大小转换的两种方式小结

    在程序开发的过程中,文件的大小在视图呈现和数据库存储的过程不一致怎么转换呢,本文主要介绍了Java文件大小转换的两种方式小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • 详解Java MD5二次加密的应用

    详解Java MD5二次加密的应用

    MD5的全称是message-digest algorithm 5 信息-摘要算法。这篇文章主要为大家详细介绍了Java中MD5二次加密的应用,感兴趣的小伙伴可以了解一下
    2023-02-02
  • 利用maven引入第三方jar包以及打包

    利用maven引入第三方jar包以及打包

    Maven是通过仓库对依赖进行管理的,当Maven项目需要某个依赖时,只要其POM中声明了依赖的坐标信息,Maven就会自动从仓库中去下载该构件使用,如何将jar引用到项目,并且能够让项目正常调用该jar包的方法,本篇文章重点针对于这两点进行讲解
    2023-05-05
  • 详解Spring Boot中如何自定义SpringMVC配置

    详解Spring Boot中如何自定义SpringMVC配置

    这篇文章主要给大家介绍了关于Spring Boot中如何自定义SpringMVC配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • java利用StringTokenizer分割字符串的实现

    java利用StringTokenizer分割字符串的实现

    利用java.util.StringTokenizer的方法,可以将一个字符串拆分为一系列的标记,本文就来介绍一下java利用StringTokenizer分割字符串的实现,感兴趣的可以了解一下
    2023-10-10
  • 详解Java中的Vector

    详解Java中的Vector

    Vector 可实现自动增长的对象数组。本文通过实例代码给大家详细介绍java中的vector,感兴趣的朋友一起看看吧
    2017-10-10
  • Mybatis创建逆向工程的步骤

    Mybatis创建逆向工程的步骤

    Mybatis逆向工程是一个自动生成Mybatis Mapper接口、XML文件和Java实体类的工具,可以提高开发效率,避免手动编写大量的重复代码,本文主要介绍了Mybatis创建逆向工程的步骤,感兴趣的可以了解一下
    2023-10-10
  • Java多线程下解决资源竞争的7种方法详解

    Java多线程下解决资源竞争的7种方法详解

    这篇文章主要介绍了Java多线程下解决资源竞争的7种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Spring MVC实现GET请求接收Date类型参数

    Spring MVC实现GET请求接收Date类型参数

    这篇文章主要介绍了Spring MVC实现GET请求接收Date类型参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论