在 Spring Boot 2.7.x 中引入 Kafka-0.9 的實踐

文章目錄

  • 在 Spring Boot 2.7.x 中引入 Kafka-0.9 的實踐
    • 一、下載 Kafka-0.9
    • 二、啟動 Zookeeper 和 Kafka
    • 三、創建 Spring Boot 項目
    • 四、引入 kafka 依賴
    • 五、移除 Kafka 自動配置
    • 六、編寫 Kafka 生產者
      • 6.1 Kafka配置類
      • 6.2 生產者監聽類
    • 七、編寫Controller發送Kafka
    • 八、驗證消費者
    • Other. Spring Boot 引入 Kafka-0.11

在 Spring Boot 2.7.x 中引入 Kafka-0.9 的實踐

一、下載 Kafka-0.9

# 1. 下載
wget https://archive.apache.org/dist/kafka/0.11.0.0/kafka_2.11-0.11.0.0.tgz# 2. 解壓
tar -zxvf kafka_2.11-0.11.0.0.tgz# 3. 進入kafka目錄
cd kafka_2.11-0.11.0.0

二、啟動 Zookeeper 和 Kafka

# 1. 啟動 zk 
bin/zookeeper-server-start.sh config/zookeeper.properties# 2. 啟動 kafka
bin/kafka-server-start.sh config/server.properties# 3. 創建topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic user_topic# 4. 消費
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic user_topic --from-beginning

三、創建 Spring Boot 項目

https://blog.csdn.net/Agan__/article/details/136109762
https://start.spring.io/

四、引入 kafka 依賴

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>1.0.5.RELEASE</version>
</dependency><dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>0.9.0.0</version>
</dependency>

五、移除 Kafka 自動配置

@SpringBootApplication(exclude = {KafkaAutoConfiguration.class})

六、編寫 Kafka 生產者

6.1 Kafka配置類

package com.chenjiacheng.samples.kafka.config;import com.chenjiacheng.samples.kafka.kafka.UserProducerListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.ProducerListener;import java.util.HashMap;
import java.util.Map;/*** Created by chenjiacheng on 2025/2/27 01:18** @author chenjiacheng* @since 1.0.0*/
@Slf4j
@Configuration
public class KafkaProducerConfig {@Autowiredprivate UserProducerListener userProducerListener;// 配置 User Topic 的 KafkaTemplate 和 ProducerListener@Bean(name = "userProducerFactory")public ProducerFactory<String, String> userProducerFactory() {Map<String, Object> configProps = new HashMap<>();configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);return new DefaultKafkaProducerFactory<>(configProps);}@Bean(name = "userKafkaTemplate")public KafkaTemplate<String, String> userKafkaTemplate() {KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<>(userProducerFactory());kafkaTemplate.setProducerListener(userProducerListener);kafkaTemplate.setDefaultTopic("user");return kafkaTemplate;}
}

6.2 生產者監聽類

package com.chenjiacheng.samples.kafka.kafka;import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.springframework.kafka.support.ProducerListener;
import org.springframework.stereotype.Component;@Slf4j
@Component
public class UserProducerListener implements ProducerListener<String, String> {@Overridepublic void onSuccess(String topic, Integer partition, String key, String value, RecordMetadata recordMetadata) {log.info("User message sent successfully: {}", value);}@Overridepublic void onError(String topic, Integer partition, String key, String value, Exception exception) {log.error("Failed to send user message: {}", value, exception);}@Overridepublic boolean isInterestedInSuccess() {return true;}
}

七、編寫Controller發送Kafka

package com.chenjiacheng.samples.kafka.controller;import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class KafkaController {@Autowiredprivate KafkaTemplate<String, String> orderKafkaTemplate;@GetMapping("/send/user")public String sendUserMessage(@RequestParam("message") String message) {orderKafkaTemplate.sendDefault(message);return "User message sent: " + message;}
}

八、驗證消費者

curl --location --globoff 'http://localhost:8081/send/user?message=hello'

在這里插入圖片描述

Other. Spring Boot 引入 Kafka-0.11

其他操作同上, 僅修改依賴版本號. 也可直接使用 SpingBoot 依賴管理的 spring-kafka.

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>1.3.11.RELEASE</version>
</dependency><dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>0.11.0.0</version>
</dependency>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/71634.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/71634.shtml
英文地址,請注明出處:http://en.pswp.cn/web/71634.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

字符串中的數字之和

題目描述 程序要求能夠提取輸入的字符串中的數字&#xff0c;將數字累加&#xff0c;得到數字之和&#xff0c;如輸入的字符串為"abc76wet23er1.",應該提取數字76,23,1,求和后&#xff0c;即76231100。 輸入格式: 輸入一個字符串&#xff0c;字符串長度不超過100.…

77.ObservableCollection使用介紹1 C#例子 WPF例子

可觀察集合ObservableCollection using System; using System.Collections.ObjectModel;class Program {static void Main(){// 創建一個可觀察集合ObservableCollection<string> list new ObservableCollection<string>();// 注冊集合變化事件list.CollectionCh…

ORACLE 執行查詢語句慢(不走對應索引)

1. 索引未被創建或未正確創建 確保為查詢中涉及的列創建了索引。例如&#xff0c;如果你經常需要按column_name列進行查詢&#xff0c;確保已經為該列創建了索引,索引創建語句 CREATE INDEX idx_column_name ON table_name(column_name); 2、索引不可用 原因:索引可能被標記為不…

r1-reasoning-rag:一種新的 RAG 思路

最近發現了一個開源項目&#xff0c;它提供了一種很好的 RAG 思路&#xff0c;它將 DeepSeek-R1 的推理能力結合 Agentic Workflow 應用于 RAG 檢索 項目地址 https://github.com/deansaco/r1-reasoning-rag.git 項目通過結合 DeepSeek-R1、Tavily 和 LangGraph&#xff0c;實現…

服務器硬件配置統計

服務器型號和SN # dmidecode -t system | grep -E "Product Name|Serial Number" | awk -F: {print $2} PowerEdge R7515 4567CPU型號和物理CPU數量 echo "$(lscpu | grep "Model name" | cut -d : -f2 | sed s/^ *//) x $(lscpu | grep "Soc…

Hadoop、Spark、Flink Shuffle對比

一、Hadoop的shuffle 前置知識&#xff1a; Map任務的數量由Hadoop框架自動計算&#xff0c;等于分片數量&#xff0c;等于輸入文件總大小 / 分片大小&#xff0c;分片大小為HDFS默認值128M&#xff0c;可調 Reduce任務數由用戶在作業提交時通過Job.setNumReduceTasks(int)設…

Docker的常用鏡像

Docker的常用鏡像命令主要包括鏡像的查看、搜索、拉取、刪除、構建等操作&#xff0c;以下是綜合多個來源的總結&#xff1a; 一、基礎鏡像操作 查看本地鏡像 docker images? 顯示所有本地鏡像&#xff0c;包含倉庫名&#xff08;REPOSITORY&#xff09;、標簽&#xff08;TAG…

車載以太網測試-3【Wireshark介紹】

1 摘要 Wireshark 是一款開源的網絡協議分析工具&#xff0c;廣泛用于網絡故障排查、協議分析、網絡安全檢測等領域。它能夠捕獲網絡數據包&#xff0c;并以詳細的、可讀的格式顯示這些數據包的內容。廣泛應用于車載網絡測試&#xff0c;是車載網絡測試工程師必須掌握的工具。…

基于跨模態地圖學習的視覺語言導航

前言 本工作開展的背景&#xff1a; 人類和其他物種構建類似地圖的環境表示來完成尋路&#xff1a; &#xff08;1&#xff09;當人類只使用現成的駕駛或步行路徑到達目標時&#xff0c;構建認知地圖和獲取空間知識的能力就會下降&#xff1b; &#xff08;2&#xff09;另…

nodejs關于后端服務開發的探究

前提 在當前的環境中關于web server的主流開發基本上都是java、php之類的&#xff0c;其中java spring系列基本上占了大頭&#xff0c;而python之流也在奮起直追&#xff0c;但別忘了nodejs也是可以做這個服務的&#xff0c;只是位置有點尷尬&#xff0c;現在就來探究下nodejs…

Ubuntu20.04本地配置IsaacGym Preview 4的G1訓練環境(一)

Ubuntu20.04本地配置IsaacGym Preview 4的G1訓練環境 配置conda虛擬環境安裝pytorch、cuda和cudnn安裝IsaacGym Preview 4配置rsl_rl配置unitree_rl_gym配置unitree_sdk2py 寫在前面&#xff0c;要求完成anaconda配置&#xff0c;若沒完成&#xff0c;請參考本人其余博客&#…

RangeError: Maximum call stack size exceeded

&#x1f90d; 前端開發工程師、技術日更博主、已過CET6 &#x1f368; 阿珊和她的貓_CSDN博客專家、23年度博客之星前端領域TOP1 &#x1f560; 牛客高級專題作者、打造專欄《前端面試必備》 、《2024面試高頻手撕題》、《前端求職突破計劃》 &#x1f35a; 藍橋云課簽約作者、…

八卡5090服務器首發亮相!

AI 人工智能領域熱度居高不下。OpenAI 的 GPT - 4 憑強悍語言處理能力&#xff0c;在內容創作、智能客服等領域廣泛應用。清華大學團隊的 DeepSeek 大模型在深度學習訓練優勢突出&#xff0c;正促使各行業應用端算力需求向推理主導轉變&#xff0c;呈爆發式增長 。 隨著 DeepS…

計算機視覺|Swin Transformer:視覺 Transformer 的新方向

一、引言 在計算機視覺領域的發展歷程中&#xff0c;卷積神經網絡&#xff08;CNN&#xff09; 長期占據主導地位。從早期的 LeNet 到后來的 AlexNet、VGGNet、ResNet 等&#xff0c;CNN 在圖像分類、目標檢測、語義分割等任務中取得了顯著成果。然而&#xff0c;CNN 在捕捉全…

【Leetcode 每日一題】2597. 美麗子集的數目

問題背景 給你一個由正整數組成的數組 n u m s nums nums 和一個 正 整數 k k k。 如果 n u m s nums nums 的子集中&#xff0c;任意兩個整數的絕對差均不等于 k k k&#xff0c;則認為該子數組是一個 美麗 子集。 返回數組 n u m s nums nums 中 非空 且 美麗 的子集數…

常見Web應用源碼泄露問題

文章目錄 前言一、常見的源碼泄露漏洞git源碼泄露SVN源碼泄露DS_Store文件泄漏網站備份壓縮文件泄露WEB-INF/web.xml泄露CVS泄露.hg源碼泄露Bazaar/bzr泄露.swp文件泄露 前言 在Web應用方面對于安全來說&#xff0c;可能大家對SQL注入、XSS跨站腳本攻擊、文件上傳等一些漏洞已…

記錄一次wifi版有人物聯串口服務器調試經過

1、首先買了一個華為的wifi路由器&#xff0c;連接上以后&#xff0c;設置好網絡名字和wifi密碼 2、用網線連接串口服務器&#xff0c;通過192.168.1.1登錄&#xff0c;進行配置 找到無線客戶端配置&#xff0c;先在基本配置中打開5G配置&#xff0c;然后再去5.8G配置中設置 …

Android 平臺架構系統啟動流程詳解

目錄 一、平臺架構模塊 1.1 Linux 內核 1.2 硬件抽象層 (HAL) 1.3 Android 運行時 1.4 原生 C/C 庫 1.5 Java API 框架 1.6 系統應用 二、系統啟動流程 2.1 Bootloader階段 2.2 內核啟動 2.3 Init進程&#xff08;PID 1&#xff09; 2.4 Zygote與System Serv…

【Windows下Gitbook快速入門使用】

Windows下Gitbook快速入門使用 1 工具安裝1.1 Node.js下載安裝1.1 環境變量1.2 npm配置1.3 安裝gitbook 2 gitbook使用2.1 gitbook 無法執行2.2 gitbook常用命令 Gitbook是一個軟件&#xff0c;使用Git和Markdown來編排書本&#xff1b; GitBook helps you pushlish beautiful …

RK3588V2--HYM8563TS RTC 實時時鐘適配移植

1. 什么是RTC RTC&#xff08;Real-Time Clock&#xff0c;實時時鐘&#xff09;是一種電子設備或芯片&#xff0c;它用于保持當前時間和日期&#xff0c;即使系統關閉或斷電也能持續計時。RTC 通常用于計算機、嵌入式系統、物聯網設備等需要精確時間管理的場景。 1.1 RTC 的…