Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 546
Daniel Stefanelli Avatar asked Jan 28 '20 15:01

Daniel Stefanelli


People also ask

What is difference between Kafka and Kafka streams?

Every topic in Kafka is split into one or more partitions. Kafka partitions data for storing, transporting, and replicating it. Kafka Streams partitions data for processing it. In both cases, this partitioning enables elasticity, scalability, high performance, and fault tolerance.

What is Spring cloud stream binder Kafka?

Spring Cloud Stream is a framework for building highly scalable event-driven microservices connected with shared messaging systems.


2 Answers

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:

  • https://cloud.spring.io/spring-cloud-stream/spring-cloud-stream.html#multiple-binders
like image 145
GUISSOUMA Issam Avatar answered Oct 29 '22 15:10

GUISSOUMA Issam


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
    
like image 37
rlawlor Avatar answered Oct 29 '22 15:10

rlawlor