我有多个弹簧启动应用程序通过kafka代理实现弹簧云流。我想知道我是否可以停止或禁用 Spring 云流或 kafka 代理连接以使应用程序能够启动。
即使代理不可用,应用程序也应该启动。
您可以在类路径中添加 noop Binder 并使其成为默认绑定程序,或者为您的绑定指定它。 这里有一些使用 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
在配置文件应用程序中指定活页夹.yml
spring:
cloud:
stream:
bindings:
my-binding:
destination: my-destination
group: my-group
binder: noop
或者在配置文件 application.yml 中指定默认绑定器
spring:
cloud:
stream:
bindings:
defaultBinder: noop
--
参考:
- https://cloud.spring.io/spring-cloud-stream/spring-cloud-stream.html#multiple-binders
您可以通过在 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