Spring-kafka快速Demo示例

使用Spring-Kafka快速发送/接受Kafka消息示例代码,项目结构是最基础的SpringBoot结构,提前安装好Kafka,确保Kafka已经正确启动

pom.xml,根据个人情况更换springboot、java版本等

c 复制代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>kafka-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kafka-study</name>
    <description>kafka-study</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properies

c 复制代码
spring.application.name=kafka-study
server.port=8081
spring.kafka.producer.bootstrap-servers=127.0.0.1:9092

启动类

c 复制代码
package org.example.kafkastudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KafkaStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(KafkaStudyApplication.class, args);
    }

}

Controller

c 复制代码
package org.example.kafkastudy.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * KafkaController
 * @date 2024/12/26
 */
@RestController
public class KafkaController {
    private final Logger logger = LoggerFactory.getLogger(KafkaController.class);

    @Autowired
    private KafkaTemplate<Object, Object> template;

    @GetMapping("/send/{input}")
    public void sendFoo(@PathVariable String input) {
        this.template.send("topic_input", input);
    }
    @KafkaListener(id = "webGroup", topics = "topic_input")
    public void listen(String input) {
        logger.info("input value: {}" , input);
    }

}

启动主启动类,浏览器调用接口localhost:8081/send/xxxx,将执行向Kafka的topic_input主题发送xxxx内容,同时控制台中可看到有xxxx内容的打印,测试成功

相关推荐
莫名其妙小饼干4 分钟前
记忆旅游系统|Java|SSM|VUE| 前后端分离
java·开发语言·maven·mssql
码蜂窝编程官方15 分钟前
【含开题报告+文档+PPT+源码】基于SpringBoot的线上动物园售票系统设计
java·vue.js·spring boot·后端·spring
我自飞扬临天下30 分钟前
Elasticsearch操作笔记版
java·笔记·elasticsearch
NHuan^_^1 小时前
RabbitMQ基础篇之Java客户端 基于注解声明队列交换机
java·rabbitmq·java-rabbitmq
NHuan^_^1 小时前
RabbitMQ基础篇之Java客户端 消息转换器
java·rabbitmq·java-rabbitmq
小汤猿人类1 小时前
RabbitMQ案例
java·rabbitmq·java-rabbitmq
Cikiss1 小时前
微服务实战——购物车模块实战
java·开发语言·后端·spring·微服务·springcloud
程序猿进阶1 小时前
大循环引起CPU负载过高
java·开发语言·后端·性能优化·并发编程·架构设计·问题排查
xmh-sxh-13141 小时前
网关的主要类型和它们的特点
java
李老头探索2 小时前
深入解析 JVM vs JDK vs JRE:三者区别与联系详解
java·开发语言·jvm