简体   繁体   English

如何停止在 Spring Kafka 消费者中收听?

[英]How to stop listening in a Spring Kafka consumer?

I use Spring for Apache Kafka .我将Spring for Apache Kafka I'd like to stop listening to my topic and wait to escape OOM.我想停止听我的话题,等待逃离 OOM。 How can I do it?我该怎么做?

 public static void stopConsumer(final String topic) {    
     ConcurrentMessageListenerContainer<String, String> container 
         = consumersMap.get(topic);
     container.stop();    
 }

Kafka provides an option to pause() and resume() the consumption from a topic. Kafka 提供了一个选项来暂停()和恢复()主题的消费。 You can use these methods by implementing a wait in between to revive from the memory issue.您可以通过在两者之间实现等待以从内存问题中恢复来使用这些方法。

Reference: https://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html参考: https ://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html

You can simply use these Guides to stop listening Kafka consumers.您可以简单地使用这些指南来停止监听 Kafka 消费者。

1- kafka-util 1-卡夫卡工具

2- Start/Stop Kafka Consumers using Spring Kafka 2- 使用 Spring Kafka 启动/停止 Kafka 消费者

With Spring boot and Spring Cloud, there is a way to stop a particular consumer using actuators.使用 Spring boot 和 Spring Cloud,有一种方法可以使用执行器停止特定的消费者。

Kafka Streams binder of Spring Cloud allows us to start or stop a consumer or function binding associated with it. Spring Cloud 的 Kafka Streams binder 允许我们启动或停止与之关联的消费者或函数绑定。

Add management.endpoints.web.exposure.include = bindings in application.properties ( or yaml formatted property in application.yml)在 application.properties 中添加management.endpoints.web.exposure.include = bindings (或在 application.yml 中添加 yaml 格式的属性)

A GET call to http://localhost:9009/actuator/bindings would expose all the bindings.http://localhost:9009/actuator/bindings的 GET 调用将公开所有绑定。

A POST call to http://localhost:9009/actuator/bindings/{name} can start or stop the binding.http://localhost:9009/actuator/bindings/{name}的 POST 调用可以启动或停止绑定。

Sample cURL to Stop a consumer : curl -d '{"state":"STOPPED"}' -H "Content-Type: application/json" -X POST http://localhost:8080/actuator/bindings/consumer-in-0停止消费者的示例 cURL: curl -d '{"state":"STOPPED"}' -H "Content-Type: application/json" -X POST http://localhost:8080/actuator/bindings/consumer-in-0

Sample cURL to Start a consumer : curl -d '{"state":"STARTED"}' -H "Content-Type: application/json" -X POST http://localhost:8080/actuator/bindings/consumer-in-0启动消费者的示例 cURL: curl -d '{"state":"STARTED"}' -H "Content-Type: application/json" -X POST http://localhost:8080/actuator/bindings/consumer-in-0

Visit Spring Cloud Stream Binder Documentation for further details.访问Spring Cloud Stream Binder 文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM