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

相关推荐
dialect_8 小时前
KMP算法
开发语言·c++·算法
captain3768 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z8 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
总要学点什么9 小时前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
文丑颜不良啊9 小时前
LeetCode1481~1500题目解析
java·leetcode
2601_962885729 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓10 小时前
调用openrouter的免费API模型
java·前端·javascript
步行cgn10 小时前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊10 小时前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every10 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea