簡體   English   中英

在 Spring 引導中通過 Kafka 消費批次

[英]Consume Batches through Kafka in Spring boot

我是Kafka的新手,想通過消費者批量處理。

通讀文檔,發現從 3.0 版本開始我們可以啟用批處理。

目前我們正在為 kafka 使用Spring Boot 2.1.3.RELEASE及以下依賴項:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

    <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>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>

在開始屬性和代碼更改之前,我需要在 pom.xml 中進行哪些更改? 我需要更改 Springboot 版本嗎?

您需要 Boot 2.3.1 和雲 Hoxton.SR6。

批處理模式僅支持函數式編程風格,不 @StreamListtener

編輯

對於當前版本(Boot 2.7,cloud 2021.0.3),這可以正常工作(使用首選功能模型),無需任何反序列化器。

@SpringBootApplication
public class So627947721Application {

    public static void main(String[] args) {
        SpringApplication.run(So627947721Application.class, args);
    }

    @Bean
    public Consumer<List<Foo>> input() {
        return System.out::println;
    }

    @Bean
    ApplicationRunner runner(KafkaTemplate<byte[], byte[]> template) {
        return args -> {
            template.send("input-in-0", "{\"bar\":\"baz\"}".getBytes());
            template.send("input-in-0", "{\"bar\":\"qux\"}".getBytes());
        };
    }

    public static class Foo {

        String bar;

        public String getBar() {
            return this.bar;
        }

        public void setBar(String bar) {
            this.bar = bar;
        }

        @Override
        public String toString() {
            return "Foo [bar=" + this.bar + "]";
        }

    }

}
spring.cloud.stream.bindings.input-in-0.group=foo
spring.cloud.stream.bindings.input-in-0.consumer.batch-mode=true
[Foo [bar=baz], Foo [bar=qux]]

您可以使用@StreamListener 將其作為一個批次使用。 你只需要給一個反序列化器。 例子:

你只需要給一個反序列化器。

public class Person {

    private String name;
    private String surname;
    .........
}


   @StreamListener(value = PersonStream.INPUT)
    private void personBulkReceiver(List<Person> person) {
        System.out.println("personBulkReceiver : " + person.size());
    }


spring:
  cloud:
    stream:
      kafka:
      binders:
        bulkKafka:
          type: kafka
          environment:
            spring:
              cloud:
                stream:
                  kafka:
                    binder:
                      brokers: localhost:9092
                      configuration:
                        max.poll.records: 1500
                        fetch.min.bytes: 1000000
                        fetch.max.wait.ms: 10000
                        value.deserializer: tr.cloud.stream.examples.PersonDeserializer
      bindings:
        person-topic-in:
          binder: bulkKafka
          destination: person-topic
          contentType: application/person
          group : omercelik
          consumer:
            batch-mode: true

public class PersonDeserializer extends JsonDeserializer<Person> {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM