SpringCloud Stream笔记整理

  1. 添加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>
  2. 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;
  3. 编写消费者:

    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 ;
        }
    }
相关推荐
一只小菜鸡..2 小时前
南京大学 操作系统 (JYY) 学习笔记:导论与历史的轮回 (OS Introduction)
笔记·学习
xqqxqxxq2 小时前
LeetCode Hot100 双指针专项题解笔记
笔记·算法·leetcode
懿路向前2 小时前
【HarmonyOS学习笔记】2026-07-24 | textProcessing 实体识别与踩坑实录
笔记·学习·边缘计算·harmonyos
Y3815326622 小时前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存
春生野草3 小时前
C基础--八大排序(个人笔记)
c语言·笔记·排序算法
Sayuanni%33 小时前
Spring IOC
java·spring·rpc
caimouse4 小时前
MM 学习笔记 09:进程会话与工作集
笔记·学习·reactos
星恒随风4 小时前
C++ 继承进阶:默认成员函数、多继承、虚继承与组合设计
开发语言·c++·笔记·学习
dkbnull4 小时前
Spring Boot依赖注入方式对比详解
spring boot·spring
正儿八经的少年4 小时前
Spring 事务保证数据一致性
java·数据库·spring