【发布时间】:2019-09-19 14:25:34
【问题描述】:
我有一个 KStream 应用程序,其中包含一堆 KStream、连接和其他操作。我启用了logging.level.org.springframework.kafka.config=debug 来验证正在生成的拓扑,并发现了很多根本没有意义的节点。
然后我将应用程序简化为:
interface ShippingKStreamProcessor {
@Input("input")
fun input(): KStream<Int, Customer>
}
@Suppress("UNCHECKED_CAST")
@Configuration
class ShippingKStreamConfiguration {
@StreamListener
fun process(@Input("input") input: KStream<Int, Customer> {}
}
奇怪的是,这样一个简单的 KStream 声明会生成这个复杂的拓扑:
2019-04-30 23:47:03.881 DEBUG 2944 --- [ main] o.s.k.config.StreamsBuilderFactoryBean : Topologies:
Sub-topology: 0
Source: KSTREAM-SOURCE-0000000000 (topics: [customer])
--> KSTREAM-MAPVALUES-0000000001
Processor: KSTREAM-MAPVALUES-0000000001 (stores: [])
--> KSTREAM-BRANCH-0000000003, KSTREAM-PROCESSOR-0000000002
<-- KSTREAM-SOURCE-0000000000
Processor: KSTREAM-BRANCH-0000000003 (stores: [])
--> KSTREAM-BRANCHCHILD-0000000004, KSTREAM-BRANCHCHILD-0000000005
<-- KSTREAM-MAPVALUES-0000000001
Processor: KSTREAM-BRANCHCHILD-0000000004 (stores: [])
--> KSTREAM-MAPVALUES-0000000007
<-- KSTREAM-BRANCH-0000000003
Processor: KSTREAM-BRANCHCHILD-0000000005 (stores: [])
--> KSTREAM-PROCESSOR-0000000006
<-- KSTREAM-BRANCH-0000000003
Processor: KSTREAM-MAPVALUES-0000000007 (stores: [])
--> none
<-- KSTREAM-BRANCHCHILD-0000000004
Processor: KSTREAM-PROCESSOR-0000000002 (stores: [])
--> none
<-- KSTREAM-MAPVALUES-0000000001
Processor: KSTREAM-PROCESSOR-0000000006 (stores: [])
--> none
<-- KSTREAM-BRANCHCHILD-0000000005
原生 Kafka 应用程序中的相同简单流会产生更合乎逻辑的拓扑:
fun main(args: Array<String>) {
val builder = StreamsBuilder()
val streamsConfiguration = Properties()
streamsConfiguration[StreamsConfig.APPLICATION_ID_CONFIG] = "kafka-shipping-service"
streamsConfiguration[StreamsConfig.BOOTSTRAP_SERVERS_CONFIG] = "http://localhost:9092"
streamsConfiguration[AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG] = "http://localhost:8081"
val serdeConfig = mapOf(
AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG to "http://localhost:8081",
AbstractKafkaAvroSerDeConfig.VALUE_SUBJECT_NAME_STRATEGY to TopicRecordNameStrategy::class.java.name
)
//val byteArraySerde = Serdes.ByteArray()
val intSerde = Serdes.IntegerSerde()
val customerSerde = SpecificAvroSerde<Customer>()
customerSerde.configure(serdeConfig, false)
val customerStream = builder.stream<Int, Customer>("customer",
Consumed.with(intSerde, customerSerde)) as KStream<Int, Customer>
val topology = builder.build()
println(topology.describe())
val streams = KafkaStreams(topology, streamsConfiguration)
streams.start()
}
拓扑:
Topologies:
Sub-topology: 0
Source: KSTREAM-SOURCE-0000000000 (topics: [customer])
--> none
Spring Cloud Stream 生成如此复杂拓扑的原因是什么?
【问题讨论】:
-
这很有趣。会调查的。我能想到的一个原因是由于活页夹需要做的消息转换。因此,如果您可以启用本机解码/编码,我认为这会稍微减少拓扑。除此之外,还有其他一些路径,binder 可能会添加更多的拓扑,例如使用 DLQ 等。
标签: kotlin apache-kafka spring-cloud apache-kafka-streams spring-cloud-stream