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);
    }
}
相关推荐
bagadesu26 分钟前
使用Docker构建Node.js应用的详细指南
java·后端
没有bug.的程序员26 分钟前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud
q***71853 小时前
海康威视摄像头ISUP(原EHOME协议) 摄像头实时预览springboot 版本java实现,并可以在浏览器vue前端播放(附带源码)
java·前端·spring boot
勇哥java实战分享4 小时前
第一次用 Ollama 跑视觉模型:Qwen2.5-VL 7B 给了我一个意外惊喜
后端
码事漫谈5 小时前
从后端开发者到Agent工程师:一份系统性的学习指南
后端
码事漫谈5 小时前
后端开发如何将创新转化为专利?案例、流程与实操指南
后端
化作星辰5 小时前
java 给鉴权kafka2.7(sasl)发送消息权限异常处理
java·大数据·开发语言·kafka
过往记忆6 小时前
Kafka 作为事件流的商业模式正在消亡
分布式·kafka
KYumii6 小时前
智慧判官-分布式编程评测平台
vue.js·spring boot·分布式·spring cloud·java-rabbitmq
user_admin_god6 小时前
企业级管理系统的站内信怎么轻量级优雅实现
java·大数据·数据库·spring boot