Spring cloud stream binder kafka 常用配置

Spring Cloud Stream 集成 Kafka 示例

话不多说直接上配置:

spring:
  cloud:
    stream:
      kafka:
        binder:
          auto-add-partitions: true
          consumer-properties:
            max:
              poll:
                interval:
                  ms: 900000
                records: 50
      bindings:
        order-out-channel: #生产者通道
          destination: order-topic
          content-type: application/json
          group: order-group
        order-in-channel: #消费者通道
          destination: order-topic
          content-type: application/json
          group: order-group

1.其中配置Kafka消费者超时配置:

max.poll.records:单次消费者拉取的最大数据条数,默认值500。
max.poll.interval.ms:表示若在阈值时间之内消费者没有消费完上一次poll的消息,consumer client会主动向coordinator发起LeaveGroup请求,触发Rebalance;然后consumer重新发送JoinGroup请求。默认值300s,5分钟

也可以使用Spring集成Kakfa一样配置:

spring:
  kafka:
    consumer:
      max-poll-records: 5
      properties:
        max:
          poll:
            interval:
              ms: 900000 -- 每次消费的处理时间 15分钟

因为binder会合并配置:
org.springframework.cloud.stream.binder.kafka.properties.KafkaBinderConfigurationProperties#mergedConsumerConfiguration
在这里插入图片描述

2.取消重试

order-in-channel: #消费者通道
destination: order-topic
content-type: application/json
group: order-group
consumer:
  max-attempts: 1
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud Stream是一个基于Spring Boot的框架,用于构建可扩展和可靠的消息驱动型微服务应用程序。而Kafka是一个分布式流媒体平台,用于构建实时数据流应用程序。 在Spring Cloud Stream中集成Kafka非常简单。首先,需要在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency> ``` 接下来,需要在项目的application.properties文件中配置Kafka的相关信息,包括Kafka服务器的地址和端口号、Topic的名称等。下面是一个示例配置: ```properties spring.cloud.stream.kafka.binder.brokers=192.168.0.1:9092 spring.cloud.stream.bindings.input.destination=my-topic spring.cloud.stream.bindings.output.destination=my-topic ``` 在上述配置中,`spring.cloud.stream.kafka.binder.brokers`指定Kafka服务器的地址和端口号,`spring.cloud.stream.bindings.input.destination`和`spring.cloud.stream.bindings.output.destination`分别指定了输入和输出的Topic名称。 然后,可以使用`@EnableBinding`注解启用绑定器并定义输入和输出的通道。例如,可以创建一个消费者类并定义一个`@Input`注解,用于接收来自Kafka Topic的消息: ```java @EnableBinding(Processor.class) public class Consumer { @StreamListener(Processor.INPUT) public void receive(String message) { System.out.println("Received message: " + message); } } ``` 类似地,可以创建一个生产者类并定义一个`@Output`注解,用于将消息发送到Kafka Topic: ```java @EnableBinding(Processor.class) public class Producer { @Autowired private MessageChannel output; public void send(String message) { output.send(MessageBuilder.withPayload(message).build()); } } ``` 以上是使用Spring Cloud Stream配置Kafka的基本步骤。通过这种方式,我们可以轻松地实现消息驱动型的微服务应用程序,并且享受到Kafka作为分布式流媒体平台的优势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张小帅和刘美美

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值