-
添加kafka stream依赖
xml<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency> -
application.yml中添加配置
yml--- #stream config spring: cloud: stream: binders: myKafka1: type: kafka environment: spring: kafka: bootstrap-servers: 127.0.0.1:9092 bindings: helloFunc-in-0: destination: hello-topic group: hello-local-test-10 binder: myKafka1 consumer: batch-mode: true helloFunc-out-0: destination: hello-topic group: hello-local-test-10 binder: myKafka1 consumer: batch-mode: true # 注意 function 节点与stream 同级,而非子节点 function: definition: helloFunc; -
编写消费者:
java@Slf4j @Component @RequiredArgsConstructor public class HelloConsumer { @Bean public Consumer<Message<List<String>>> helloFunc() { return message -> { log.info("---------------------> "); List<String> list = message.getPayload(); boolean result = this.handle(list); if (result) { Acknowledgment acknowledgment = message.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT, Acknowledgment.class); if (acknowledgment != null) { acknowledgment.acknowledge(); } } else { throw new RuntimeException("消费数据出错!"); } }; } private boolean handle(List<String> list){ log.info("list size : {}", list.size()); if (!CollectionUtils.isEmpty(list)){ log.info("group first message : {}", list.get(0)); } return true ; } }
SpringCloud Stream笔记整理
yicj2024-03-15 14:53
相关推荐
cao2333几秒前
关于 x1 y1 等变量名冲突的指正(校对版)Demons_kirit1 小时前
CTFHub 信息泄露通关笔记:Git泄露 Log - 教程xhyu612 小时前
【学习笔记】图像质量评估方法 (5.生成图像是否符合用户要求)2601_961146203 小时前
实验室用等离子表面处理设备处理新能源汽车塑料件的一点记录知产xiao_xin3 小时前
专利主体及权利归属CoderYanger3 小时前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)学电子她就能回来吗3 小时前
LeetCode Hot 100|普通数组专题学习笔记(零基础版)2601_965384263 小时前
从微生态与流体力学视角看:“口水臭”的系统级成因与清洁边界XIAEE4 小时前
一个即时通讯项目是怎么长出来的:(Spring Boot 3 + Vue 3 微前端 + 悟空 IM + LiveKit)卿卿的产品经理日记4 小时前
【AI产品经理实战】Day 38|try-except 踩坑实录:报错是数据