How to disable a Spring Cloud Stream Kafka consumer

Viewed 453

Here is my situation:

We have a Spring cloud Stream 3 Kafka service connected to multiple topics in the same broker but I want to control connecting to a specific topic based on properties.

Every topic has its own binder and binding but the broker is the same for all.

I tried disabling the binding (that was the only solution I found so far) by using the property below and that works for the StreamListener to not receive messages but the connection to the topic and rebalancing is still happening.

spring:
  cloud:
    stream:
      bindings:
        ...
        anotherBinding:
          consumer:
            ...
            autostartup: false
  

I wonder if there is any setting on binder level that prevents it from starting. One of the topics consumer should only be available in one of the environments.

Thanks

1 Answers

Disabling the bindings by setting autoStartup to false should work, I am not sure what the issue is.

It doesn't look like you are using the new functional model, but the StreamListener. If you are using the functional model, here is another thing that you can try. You can disable the bindings by not including the corresponding functions at runtime. For example, assume you have the following two consumers.

@Bean
public Consumer<String> one() {}

@Bean
public Consumer<String> two() {}

When running this app, you can provide the property spring.cloud.function.definition to include/exclude functions. For instance, when you run it with spring.cloud.function.definition=one, then the consumer two will not be activated at all. When running with spring.cloud.function.definition=two, then the consumer one will not be activated.

The downside to the above approach is that if you decide to start the other function once the app started (given autoStartup is false on the other function), it will not work as it was not part of the original bindings through spring.cloud.function.definition. However, based on your requirements, this is probably not an issue as you know which environments are targeted for the corresponding topics. In other words, if you know that consumer one needs to always consume from the topic one, then you don't include consumer two as part of the definition.

Related