在没有kafka代理运行的情况下,如何禁用SpringCloudStream进行开发?

bvhaajcl  于 2021-06-05  发布在  Kafka
关注(0)|答案(2)|浏览(371)

我有多个spring boot应用程序实现了带有kafka代理的spring云流。我想知道我是否可以停止或禁用springcloudstream或kafka代理连接来启动应用程序。

33qvvth1

33qvvth11#

您可以通过在spring引导应用程序中禁用kafka绑定来实现这一点
应用程序类

import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;

@SpringBootApplication(exclude = KafkaAutoConfiguration.class)

public class Application {
  ...
}

application.yml(如果使用yml)

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

application.properties(如果使用属性)

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

uelo1irk2#

即使代理不可用,应用程序也应该启动。
您可以在类路径中添加一个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()

}

//资源>meta inf>spring.binders

noop: com.demo.NoOpBinderServiceConfigurer

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

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

相关问题