最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 集成 Kafka的詳細(xì)步驟

 更新時(shí)間:2024年07月26日 09:30:26   作者:傲雪凌霜,松柏長(zhǎng)青  
Spring Boot與Kafka的集成使得消息隊(duì)列的使用變得更加簡(jiǎn)單和高效,可以配置 Kafka、實(shí)現(xiàn)生產(chǎn)者和消費(fèi)者,并利用 Spring Boot 提供的功能處理消息流,以下是 Spring Boot 集成 Kafka 的詳細(xì)步驟,包括配置、生產(chǎn)者和消費(fèi)者的實(shí)現(xiàn)以及一些高級(jí)特性,感興趣的朋友一起看看吧

Spring Boot 與 Kafka 集成是實(shí)現(xiàn)高效消息傳遞和數(shù)據(jù)流處理的常見(jiàn)方式。Spring Boot 提供了簡(jiǎn)化 Kafka 配置和使用的功能,使得集成過(guò)程變得更加直觀和高效。以下是 Spring Boot 集成 Kafka 的詳細(xì)步驟,包括配置、生產(chǎn)者和消費(fèi)者的實(shí)現(xiàn)以及一些高級(jí)特性。

1. 添加依賴

首先,你需要在 Spring Boot 項(xiàng)目的 pom.xml 文件中添加 Kafka 相關(guān)的依賴。使用 Spring Boot 的起步依賴可以簡(jiǎn)化配置。

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

2. 配置 Kafka

2.1. 配置文件

application.propertiesapplication.yml 文件中配置 Kafka 相關(guān)屬性。

application.properties:

# Kafka 服務(wù)器地址
spring.kafka.bootstrap-servers=localhost:9092
# Kafka 消費(fèi)者配置
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 生產(chǎn)者配置
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 注解創(chuàng)建一個(gè)配置類,來(lái)定義 Kafka 的生產(chǎn)者和消費(fèi)者配置。

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. 實(shí)現(xiàn) Kafka 生產(chǎn)者

3.1. 生產(chǎn)者服務(wù)

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. 實(shí)現(xiàn) Kafka 消費(fèi)者

4.1. 消費(fèi)者服務(wù)

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. 高級(jí)特性

5.1. 消息事務(wù)

Kafka 支持消息事務(wù),確保消息的原子性。

生產(chǎn)者配置

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

使用事務(wù)

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. 異步發(fā)送與回調(diào)

異步發(fā)送

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())
    );
}

總結(jié)

Spring Boot 與 Kafka 的集成使得消息隊(duì)列的使用變得更加簡(jiǎn)單和高效。通過(guò)上述步驟,你可以輕松地配置 Kafka、實(shí)現(xiàn)生產(chǎn)者和消費(fèi)者,并利用 Spring Boot 提供的強(qiáng)大功能來(lái)處理消息流。了解 Kafka 的高級(jí)特性(如事務(wù)和異步處理)能夠幫助你更好地滿足業(yè)務(wù)需求,確保系統(tǒng)的高可用性和數(shù)據(jù)一致性。

到此這篇關(guān)于Spring Boot 集成 Kafka的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)Spring Boot 集成 Kafka內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot集成Redisson實(shí)現(xiàn)分布式鎖的方法示例

    SpringBoot集成Redisson實(shí)現(xiàn)分布式鎖的方法示例

    這篇文章主要介紹了SpringBoot集成Redisson實(shí)現(xiàn)分布式鎖的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java中mapstruct mapper轉(zhuǎn)換器部分字段轉(zhuǎn)換無(wú)效的解決方案

    Java中mapstruct mapper轉(zhuǎn)換器部分字段轉(zhuǎn)換無(wú)效的解決方案

    在SpringBoot2.1.5項(xiàng)目中,使用MapStruct1.3.0.Final進(jìn)行實(shí)體類字段映射時(shí)遇到問(wèn)題,下面就來(lái)具體介紹一下,感興趣的可以了解一下
    2025-10-10
  • sa-token?路由攔截式鑒權(quán)使用示例詳解

    sa-token?路由攔截式鑒權(quán)使用示例詳解

    這篇文章主要為大家介紹了sa-token?路由攔截式鑒權(quán)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 詳解MyBatis XML配置解析

    詳解MyBatis XML配置解析

    這篇文章主要介紹了詳解MyBatis XML配置解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring 整合多個(gè)配置文件的方法

    Spring 整合多個(gè)配置文件的方法

    在一些大型應(yīng)用中,可能存在多個(gè)配置文件,這篇文章給大家介紹了Spring 整合多個(gè)配置文件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2018-05-05
  • 使用SSE流式輸出實(shí)戰(zhàn)記錄(Javaweb前后端)

    使用SSE流式輸出實(shí)戰(zhàn)記錄(Javaweb前后端)

    這篇文章主要介紹了使用SSE流式輸出(Javaweb前后端)的相關(guān)資料,SSE是一種允許服務(wù)器向客戶端推送實(shí)時(shí)數(shù)據(jù)的技術(shù),適用于聊天應(yīng)用、實(shí)時(shí)數(shù)據(jù)監(jiān)控等場(chǎng)景,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-05-05
  • Eclipse項(xiàng)目有紅感嘆號(hào)的解決方法

    Eclipse項(xiàng)目有紅感嘆號(hào)的解決方法

    這篇文章主要為大家詳細(xì)介紹了Eclipse項(xiàng)目有紅感嘆號(hào)的解決方法,給出了Eclipse項(xiàng)目有紅感嘆號(hào)的原因,以及如何解決?,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 使用Java凍結(jié)Excel行和列的完整指南

    使用Java凍結(jié)Excel行和列的完整指南

    在日常數(shù)據(jù)處理和報(bào)表生成中,Excel 扮演著不可或缺的角色,然而,當(dāng)面對(duì)包含成百上千行、幾十乃至上百列的大型 Excel 文件時(shí),反復(fù)滾動(dòng)查看數(shù)據(jù)往往會(huì)帶來(lái)極大的不便,所以本文旨在提供一份清晰、實(shí)用且基于 Java 的解決方案,幫助讀者高效地實(shí)現(xiàn) Excel 行和列的凍結(jié)操作
    2026-02-02
  • spring使用validation參數(shù)及全局異常檢測(cè)方式

    spring使用validation參數(shù)及全局異常檢測(cè)方式

    本文主要介紹了Java的數(shù)據(jù)校驗(yàn)規(guī)范validation-api,包括其定義的注解和HibernateValidator的實(shí)現(xiàn),還介紹了spring-boot-starter-validation的使用,可以讓開(kāi)發(fā)者在SpringBoot應(yīng)用中簡(jiǎn)化數(shù)據(jù)校驗(yàn)的功能
    2025-02-02
  • java 如何實(shí)現(xiàn)日志追蹤MDC

    java 如何實(shí)現(xiàn)日志追蹤MDC

    這篇文章主要介紹了java 實(shí)現(xiàn)日志追蹤MDC方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論

沈丘县| 临桂县| 三河市| 准格尔旗| 瑞安市| 彝良县| 双牌县| 新余市| 漯河市| 阿尔山市| 龙州县| 肇州县| 云南省| 武平县| 纳雍县| 中江县| 曲沃县| 公安县| 仁化县| 黔西| 哈尔滨市| 麻栗坡县| 昔阳县| 龙井市| 咸阳市| 包头市| 南召县| 昌图县| 泾阳县| 太湖县| 定兴县| 长顺县| 湖口县| 龙胜| 封开县| 常宁市| 孟州市| 福清市| 静宁县| 济阳县| 大埔区|