8

I have multiples spring boot apps implementing spring cloud stream with kafka brokers. I'd like to know if I can stop or disable spring cloud stream or kafka broker connections to enable apps to start.

2 Answers2

2

Apps should start even the brokers are not available.

You could add a noop Binder in the classpath and make it a default binder or specify it for your binding. Here some code with Kotlin:

The NoOpBinder implementation class:

package com.demo

import org.slf4j.LoggerFactory
import org.springframework.cloud.stream.binder.Binder
import org.springframework.cloud.stream.binder.Binding
import org.springframework.cloud.stream.binder.ConsumerProperties
import org.springframework.cloud.stream.binder.ProducerProperties
import org.springframework.messaging.MessageChannel

class NoOpBinder : Binder<MessageChannel, ConsumerProperties, ProducerProperties> {
    val logger = LoggerFactory.getLogger(javaClass)!!
    override fun bindConsumer(
        name: String,
        group: String,
        inboundBindTarget: MessageChannel,
        consumerProperties: ConsumerProperties
    ): Binding<MessageChannel> = NoOpBinding(name).also { logger.info("bindConsumer: $it") }

    override fun bindProducer(
        name: String,
        outboundBindTarget: MessageChannel,
        producerProperties: ProducerProperties
    ): Binding<MessageChannel> = NoOpBinding(name).also { logger.info("bindProducer: $it") }

    private class NoOpBinding(val binderName: String) : Binding<MessageChannel> {
        val logger = LoggerFactory.getLogger(javaClass)!!

        override fun getName() = binderName
        override fun unbind() {
            logger.info("unbind: $this")
        }

        override fun toString() = "NoOpBinding [$name]"
    }
}

A configuration class:

package com.demo

import org.springframework.context.annotation.Bean

// Warn: this class is referenced in META-INF/spring.binders and used by spring cloud stream to instantiate binders.
class NoOpBinderServiceConfigurer {

    @Bean
    fun noOpBinder() = NoOpBinder()

}

// resources/META-INF/spring.binders

noop: com.demo.NoOpBinderServiceConfigurer

Specify the binder in your configuration file application.yml

spring:
  cloud:
    stream:
      bindings:
        my-binding:
          destination: my-destination
          group: my-group
          binder: noop

Or specify the default binder in your configuration file application.yml

spring:
  cloud:
    stream:
      bindings:
        defaultBinder: noop

--

Reference:

Paulo Mateus
  • 456
  • 6
  • 10
GUISSOUMA Issam
  • 2,572
  • 2
  • 16
  • 27
0

You can do this by disabling the kafka binding in the spring boot application

  1. Application class

    import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
    
    @SpringBootApplication(exclude = KafkaAutoConfiguration.class)
    
    public class Application {
      ...
    }
    
  2. application.yml (If using yml)

    spring: 
      autoconfigure:
        exclude: org.org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
    
  3. application.properties (If using properties)

    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
    
rlawlor
  • 9
  • 4
  • Hello, thanks for your answer. I tried but it fails with this message: Description: Parameter 0 of method binderConfigurationProperties in org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsBinderSupportAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.kafka.KafkaProperties' that could not be found. – Daniel Stefanelli Jan 29 '20 at 22:53
  • This doesn't works with RabbitMQ because isn't possible disable `AmpqAutoConfiguration` when using `org.springframework.cloud:spring-cloud-stream-binder-rabbit` :( – Paulo Mateus Jul 10 '21 at 22:09