-
添加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
相关推荐
使一颗心免于哀伤33 分钟前
《设计模式之禅》笔记摘录 - 21.状态模式咖啡Beans1 天前
SpringCloud网关Gateway功能实现_落纸2 天前
三大基础无源电子元件——电阻(R)、电感(L)、电容(C)麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案Alice-YUE2 天前
【CSS学习笔记3】css特性2303_Alpha2 天前
SpringBoot青衫客362 天前
Spring异步编程- 浅谈 Reactor 核心操作符熙客2 天前
SpringCloud概述Cyan_RA92 天前
SpringMVC @RequestMapping的使用演示和细节 详解