当前位置: 首页 > news >正文

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

废话不多说直接上代码:
1.pom

		<!-- 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.配置

spring:application:name: mqdemokafka:bootstrap-servers: 10.228.48.28:39092,10.228.48.19:39092,10.228.48.21:39092consumer:group-id: my-groupauto-offset-reset: earliestproducer:key-serializer: org.apache.kafka.common.serialization.StringSerializervalue-serializer: org.apache.kafka.common.serialization.StringSerializer

3.service

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

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);@Autowiredprivate 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);}
}
http://www.lryc.cn/news/374480.html

相关文章:

  • Ant Design Vue 动态表头和数据填充
  • 在Spring Cloud项目中集成Springdoc OpenAPI生成OpenAPI 3文档的详细解析
  • Linux shell 重定向输入和输出
  • electron录制工具-视频保存、编辑页面
  • curl命令行发送post/get请求
  • Redis 分片集群
  • 学习分享-Callable 和 Runnable 任务
  • three.js 基础01
  • 使用file.transferTo()做Java文件复制,目标文件存在时,是抛异常还是覆盖写入?
  • Python:线性查找法
  • IDEA 设置主题、背景图片、背景颜色
  • 【elementui源码解析】如何实现自动渲染md文档-第三篇
  • this指针如何使C++成员指针可调用
  • Redis数据结构之字符串(sds)
  • tokenization(二)子词切分方法
  • 慈善组织管理系统设计
  • 大疆Pocket3手持记录仪格式化恢复方法
  • Mybatis的面试题
  • 渗透测试之内核安全系列课程:Rootkit技术初探(五)
  • 探索C嘎嘎的奇妙世界:第三关---缺省参数与函数重载
  • docker拉取镜像太慢解决方案
  • 仅凭一图,即刻定位,AI图像定位技术
  • 跟着刘二大人学pytorch(第---12---节课之RNN基础篇)
  • 父亲节 | 10位名家笔下的父亲,读懂那份孤独而深沉的父爱
  • 股市中的牛市和熊市是什么?它们是怎么来的?
  • 基于51单片机万年历设计—显示温度农历
  • springboot-自定义properties文件
  • java类的访问权限
  • 【SpringBoot + Vue 尚庭公寓实战】标签和配套管理接口实现接口实现(六)
  • Web前端中横线:深入探索与实际应用