Kafka入门到精通(四)-SpringBoot+Kafka

news/2024/7/7 18:56:41 标签: kafka, spring boot, 分布式

一丶IDEA创建一个空项目

        

二丶添加相关依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.12</version>
        </dependency>
    </dependencies>

三丶编写简单生产者

    /**
     * 简单的生产者消费者
     * @param message
     */
    @GetMapping("/kafka/normal/message")
    public void sendNormalMessage(@RequestParam("message") String message) {
        log.info("======================="+message);
        kafkaTemplate.send("sb_topic", 0, System.currentTimeMillis(), "key1", message);
    }

四丶编写简单消费者

@Component
public class KafkaConsumer {
 
    //监听消费
    //@KafkaListener(topics = {"sb_topic"})
    @KafkaListener(topics = {"sb_topic","callbackOne_topic"}, groupId = "testGroup")
    public void onNormalMessage(ConsumerRecords<String,Object> records) {
        for (ConsumerRecord<String, Object> record : records) {
            System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
        }
    }
 
}
 

这里有个坑,ConsumerRecord如果不加s会报错,我之间在借鉴他人代码的时候出现的,不知道是不是版本问题。我也刚用kafka,正在研究哈哈,见谅见谅;

postman请求:

成功:

结尾:目前只是一个简单的demo,后续我在完善,我也正在学习这玩意儿,哈哈,喜欢的朋友点个赞收藏吧;


http://www.niftyadmin.cn/n/5534909.html

相关文章

服务器被劫持

当服务器被劫持时&#xff0c;可以采取一系列措施来修复和加强安全防护&#xff0c;确保服务器的安全性和数据的完整性。以下是一些建议的步骤和策略&#xff1a;12 确认服务器被劫持&#xff1a;首先需要确认服务器是否被劫持。这可以通过检查服务器的日志、网络流量、以及任…

Lambda表达式讲解

简介: Lambda表达式的使用场景非常广泛,主要包括函数式编程、集合操作、排序、线程编程、GUI事件处理、数据处理、Web开发等。 函数式编程:Lambda表达式是函数式编程的重要特性,可以用于替代传统的匿名内部类,简化代码,提高可读性。 集合操作:Lambda表达式可以与集合…

web平台—apache

web平台—apache 1. 学apache前需要知道的知识点2. apache详解2.1 概述2.2 工作模式2.3 启动apache网站整体流程2.4 相关文件保存位置2.5 配置文件详解 3. apache配置实验实验1&#xff1a;设置apache的目录别名实验2&#xff1a;apache的用户认证实验3&#xff1a;虚拟主机 (重…

SpringCloud Gateway 网关获取或修改接口响应数据

文章目录 前言一、获取响应数据并打印 前言 我们的网关在之前只记录了接口请求日志&#xff0c;响应日志是没有做记录的&#xff0c;在后续别人对接我们开放平台时出现了一些问题没法确认当时的一个数值状态&#xff0c;照成了很多不必要的麻烦&#xff0c;后来决定在网关添加上…

Flutter——最详细(Badge)使用教程

背景 主要常用于组件叠加上圆点提示&#xff1b; 使用场景&#xff0c;消息数量提示&#xff0c;消息红点提示 属性作用backgroundColor红点背景色smallSize设置红点大小isLabelVisible是否显示offset设置红点位置alignment设置红点位置child设置底部组件 代码块 class Badge…

复分析——第8章——共形映射(E.M. Stein R. Shakarchi)

第8章 共形映射(Conformal Mappings) The results I found for polygons can be extended under very general assumptions. I have undertaken this research because it is a step towards a deeper understanding of the mapping problem, for which not much has hap…

鸿蒙登录页面及页面跳转的设计

目录 任务目标任务分析任务实施1.新建工程项目HMLogin2.设计登录页面Index.visual3.设计第二个页面SecondPage4.修改Index.ets代码5.修改SecondPage.ets代码6.运行工程 任务目标 设计一个简单的登录页面&#xff0c;要求可以将第一页的登录信息&#xff0c;传递到第二个页面&a…

办公软件WPS与Office的区别

临近计算机考试很多同学在纠结我是报wps好&#xff1f;还是ms office好&#xff1f;下面就来详细说说。 1、wps属于国内金山公司的办公软件&#xff0c;里面包含word、Excel和PPT。考试是2021年开始的&#xff01; 2、MS&#xff08;Microsoft 微软&#xff09; office属于美…