我有多个使用kafka经纪人实施spring cloud流的spring boot应用程序。我想知道是否可以停止或禁用Spring Cloud Stream或kafka代理连接以启动应用程序。
您可以通过在Spring Boot应用程序中禁用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
即使代理不可用,应用程序也应该启动。
您可以在类路径中添加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