-
添加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
相关推荐
深圳雨林凯AI5 小时前
图案裂变的“风格归一化“设计思路:主体保得住,画风才拉得齐|雨林凯AI技术笔记冬木家居7 小时前
40㎡客厅变形记,小家住出大自由[特殊字符]摇滚侠7 小时前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 数据库初始化机制 阅读笔记 36霸道流氓气质7 小时前
Spring AI 输出解析器进阶推送者9 小时前
用宠物照片制作桌宠:从AI拆帧到九点标定的本地化实现笔记一条破秋裤9 小时前
16_TIM编码器接口测速tju新生代魔迷9 小时前
Verilog HDL 学习笔记(十)| 第10章 时序和延迟leonkay11 小时前
C# 锁机制——【2】深入原理血小板要健康11 小时前
链表 阶段算法总结浔溺11 小时前
al+大数据每日学习笔记31