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);
    }
}
相关推荐
爱上妖精的尾巴18 分钟前
7-3 WPS JS宏 keys、values、entries、JSON.stringify 循环对象中的属性
后端·restful·wps·jsa
为所欲为、Lynn1 小时前
用FastJson的Filter自动映射枚举
java·spring boot
JIngJaneIL1 小时前
基于java+ vue学生成绩管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
老华带你飞1 小时前
智能菜谱推荐|基于java + vue智能菜谱推荐系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
LucianaiB2 小时前
历史十大帝王拉到一个群聊会发生什么事?朱元璋直接开喷
后端
h7ml2 小时前
在Spring Boot中集成企业微信API的统一异常处理与日志追踪方案
spring boot·企业微信
苏三的开发日记3 小时前
flink集群服务搭建
后端
程序帝国3 小时前
SpringBoot整合RediSearch(完整,详细,连接池版本)
java·spring boot·redis·后端·redisearch
源码获取_wx:Fegn08953 小时前
基于springboot + vueOA工程项目管理系统
java·vue.js·spring boot·后端·spring
小哀23 小时前
2025年总结: 我还在往前走
前端·后端·全栈