java kafka

安装

安装下载

导入依赖

java 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>kafka</artifactId>
        <groupId>com.tt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>kafkaProducer</artifactId>

    <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.8.0</version> <!-- 根据需要选择合适的版本 -->
        </dependency>


    </dependencies>

</project>

创建producer项目

配置文件

java 复制代码
# application.yaml
spring:
  kafka:
    bootstrap-servers: localhost:9092
    consumer:
      group-id: myGroup
      #auto.offset.reset = latest 的含义:
      #若一个 Topic 有历史数据,但消费者组是首次启动且未提交过 offset,则不会消费历史消息,只会接收启动后新产生的数据。
      #earliest:无 offset 时从头消费,适合需处理全量数据的场景。
      #none:无 offset 时抛出异常,需手动处理,适合对重复消费敏感的业务。
      auto-offset-reset: earliest
      enable-auto-commit: true
      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
  application:
    name: producer
server:
  port: 9124

发送

java 复制代码
package com.tt.control;

import com.tt.common.Rs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Controller {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;


    @GetMapping("/test")
    public Rs tt(){
        kafkaTemplate.send("tetete","6");
        return Rs.success();
    }


}

创建consumer

依赖一样,配置文件一样

消费

java 复制代码
package com.tt.control;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class HelloKafka {

    @KafkaListener(topics = "tetete")
    public void onMessage(String data){
        System.out.println(data);
    }
}

可视化工具

Kafka Tool

相关推荐
AC赳赳老秦2 分钟前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
提笔了无痕4 分钟前
如何用Go实现整套RAG流程
开发语言·后端·golang
(Charon)7 分钟前
【C++ 面试高频基础:指针、引用、const、static、new/delete 总结】
java·开发语言
Yeats_Liao24 分钟前
Feed流系统设计(三):数据模型与存储设计,从表结构到Redis收件箱
java·javascript·redis
JiaHao汤30 分钟前
分布式事务方案全景:从理论到 Seata 落地
java·分布式·spring·spring cloud
2601_9618752441 分钟前
法考考试时间安排及科目|时间表|资料已整理
开发语言·c#·inverted-index·suffix-tree·sstable·r-tree·lsm-tree
AI科技星1 小时前
数术工坊第八卷:算力革命
c语言·开发语言·网络·量子计算·agi
geovindu1 小时前
go: Generators Pattern
开发语言·后端·设计模式·golang·生成器模式
色空大师1 小时前
【debug调试详解-idea】
java·ide·intellij-idea·调试·远程调试
程序猿阿越1 小时前
AutoMQ源码(一)读、写、Compaction
java·后端·源码