springboot 搭建一个 测试Kafka 集群连通性demo

废话不多说直接上代码:

1.pom

xml 复制代码
		<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
		<dependency>
			<groupId>org.springframework.kafka</groupId>
			<artifactId>spring-kafka</artifactId>
			<version>3.1.1</version>
		</dependency>

2.配置

yaml 复制代码
spring:
  application:
    name: mqdemo
  kafka:
    bootstrap-servers: 10.228.48.28:39092,10.228.48.19:39092,10.228.48.21:39092
    consumer:
      group-id: my-group
      auto-offset-reset: earliest
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

3.service

java 复制代码
package com.example.demo.service;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author wangjn
 * @Description
 * @createTime 2024-06-13 14:02:00
 */
@Service
public class KafkaService {

    private final KafkaTemplate<String, String> kafkaTemplate;

    public KafkaService(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String topic, String message) {
        kafkaTemplate.send(topic, message);
    }

    @KafkaListener(topics = "my-topic")
    public void listen(String message) {
        System.out.println("Received message: " + message);
    }
}

4.controller

java 复制代码
package com.example.demo.web;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangjn
 * @Description
 * @createTime 2024-06-13 14:08:00
 */
@RestController
public class KafkaController {
    private static final Logger LOGGER = LoggerFactory.getLogger(KafkaController.class);

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @PostMapping("/sendToKafka")
    public String sendMessageToKafka(@RequestParam("message") String message) {
        kafkaTemplate.send("my-topic", message);
        return "Message sent to Kafka successfully!";
    }

    @GetMapping("/consumeFromKafka")
    public String simulateConsumeFromKafka() {
        return "Checking for messages...";
    }

    @KafkaListener(topics = "my-topic")
    public void listenToKafka(String message) {
        LOGGER.info("Message received from Kafka: {}", message);
    }
}
相关推荐
松就是我902983 分钟前
如何设计多Agent
后端
高频因子挖掘机6 分钟前
盘中筛选要同时看价格和盘口?按需分层比“一次全拿”更好维护
后端·github·api
asong11 分钟前
从写代码到部署上线,Cloudflare 给 AI 配了一把新钥匙
前端·javascript·后端
RobinDevNotes11 分钟前
PagedAttention原理,KV缓存也能分页
人工智能·后端
卷无止境13 分钟前
当AI写代码遇见Rust为什么会卡壳
后端·python
Percy_kk16 分钟前
Django的CRUD映射
后端·django
用户EasyAdminBlazor18 分钟前
Blazor Server 性能优化实战:Circuit、数据库、Redis 与并发
后端
王中阳Go21 分钟前
Agent 第一句就 500,我们查了三轮:入口日志少打了一个参数
后端·agent·ai编程
羑悻22 分钟前
穿越Docker内核迷雾:揭秘镜像分层存储的叠加态与卷挂载的多维空间穿梭技术
后端·docker·容器
励志不掉头发的内向程序员22 分钟前
从鼠标点击到画出一条线:CAD 交互层的状态机设计
后端·架构