-
添加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
相关推荐
惜.己15 分钟前
前端笔记(四)故渊ZY16 分钟前
SpringMVC核心原理与实战全解析我命由我1234521 分钟前
开发中的英语积累 P21:Parentable、Guideline、Manifest、Expire、Condition、Optimize秋邱21 分钟前
Java基础语法核心:程序结构、注释规范、变量常量与数据类型廋到被风吹走25 分钟前
【Spring】核心类研究价值排行榜BOF_dcb39 分钟前
TCP/IP网络协议Lv117700843 分钟前
Visual Studio中的常量和只读变量深蓝海拓1 小时前
PySide6从0开始学习的笔记(十四)创建一个简单的实用UI项目czlczl200209251 小时前
SpringBoot实践:从验证码到业务接口的完整交互生命周期Han_coding12081 小时前
从原理到实战:基于游标分页解决深分页问题(附源码方案)