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