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);
    }
}
相关推荐
Ciderw15 分钟前
MySQL日志undo log、redo log和binlog详解
数据库·c++·redis·后端·mysql·面试·golang
m0_7482359525 分钟前
SpringBoot:解决前后端请求跨域问题(详细教程)
java·spring boot·后端
ezreal_pan1 小时前
kafka消费能力压测:使用官方工具
分布式·kafka
xiao-xiang1 小时前
kafka-集群缩容
分布式·kafka
比花花解语1 小时前
Kafka在Windows系统使用delete命令删除Topic时出现的问题
windows·分布式·kafka
解决方案工程师1 小时前
【Kafka】Kafka高性能解读
分布式·kafka
yellowatumn1 小时前
RocketMq\Kafka如何保障消息不丢失?
分布式·kafka·rocketmq
坚定信念,勇往无前2 小时前
springboot单机支持1w并发,需要做哪些优化
java·spring boot·后端
后端小肥肠2 小时前
【AI编程】Java程序员如何用Cursor 3小时搞定CAS单点登录前端集成
前端·后端·cursor
老友@2 小时前
OnlyOffice:前端编辑器与后端API实现高效办公
前端·后端·websocket·编辑器·onlyoffice