繁体   English   中英

当没有 kafka 代理运行时,如何出于开发目的禁用 Spring Cloud 流?

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

我有多个 Spring Boot 应用程序使用 kafka 代理实现 Spring Cloud 流。 我想知道是否可以停止或禁用 Spring Cloud 流或 kafka 代理连接以启用应用程序。

您可以通过在 spring boot 应用程序中禁用 kafka 绑定来完成此操作

  1. 应用类

    import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; @SpringBootApplication(exclude = KafkaAutoConfiguration.class) public class Application { ... }
  2. application.yml(如果使用 yml)

     spring: autoconfigure: exclude: org.org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
  3. application.properties(如果使用属性)

     spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration

即使经纪人不可用,应用程序也应该启动。

您可以在类路径中添加一个 noop 绑定器并使其成为默认绑定器或为您的绑定指定它。 这里有一些 Kotlin 代码:

NoOpBinder 实现类:

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]"
    }
}

一个配置类:

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

在您的配置文件 application.yml 中指定绑定器

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

或者在你的配置文件 application.yml 中指定默认绑定器

spring:
  cloud:
    stream:
      bindings:
        defaultBinder: noop

——

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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