简体   繁体   中英

How can I disable spring cloud stream for development purpose when there are not kafka broker running?

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.

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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