<p>I am using Spring kafka implementation and I need to start and stop my kafka consumer through an REST API. For that i am using KafkaListenerEndpointRegistry endpointRegistry</p>
<p>endpointRegistry.getListenerContainer(&quot;consumer1&quot;).stop();</p>
<p>endpointRegistry.getListenerContainer(&quot;consumer1&quot;).start();</p>
<p>We are deploying the microservice on kubernetes pod so there might be multiple deployments for the same microservice. how could i manage to start and stop the consumer on all the container.</p>

<p>Kubernetes offers nothing to automatically broadcast an http-request to all pods for a service; so you have to do it yourself.</p>
<p><strong>Broadcasting over Kafka</strong></p>
<p>You can publish the start/stop command from the single instance that receives the http-request to a topic, dedicated for broadcasting commands between all instances.</p>
<p>Of course, you must make sure that each instance can read all message on that topic, so you need to prevent the partitions from being balanced between these instances. You can achieve that by setting a unique group-id (e.g. by suffixing your normal groupId with a UUID) on the Consumer for that topic.</p>
<p><strong>Broadcasting over Http</strong></p>
<p>Kubernetes knows which pods are listening on which endpoints, and you can get that information in your service. Spring Cloud Kubernetes (<a href="https://cloud.spring.io/spring-cloud-static/spring-cloud-kubernetes/2.0.0.M1/reference/html/#ribbon-discovery-in-kubernetes" rel="nofollow noreferrer">https://cloud.spring.io/spring-cloud-static/spring-cloud-kubernetes/2.0.0.M1/reference/html/#ribbon-discovery-in-kubernetes</a>) makes it easy to get at that information; there's probably lots of different ways to do that, with Spring Cloud Kubernetes it would go something like this:</p>
<p>Receive the command on the randomly selected pod, get the ServerList from Ribbon (it contains all the instances and the ip-address/port where they can be reached) for your service, and send a new http-request to each of them.</p>
<p>I would prefer the Kafka-approach for its robustness, the http-approach might be easier to implement, if you're already using Spring Cloud.</p>
