简体   繁体   English

如何在 Kafka 流中使用实例特定主题?

[英]How to use instance specific topics in Kafka streams?

I have this topology:我有这个拓扑:

Topology topology = new Topology();

//WS connection processor
topology.addSource(WS_CONNECTION_SOURCE, new StringDeserializer(), new WebSocketConnectionEventDeserializer(), KafkaTopics.WS_CONNECTION_EVENTS_TOPIC)
    .addProcessor(SESSION_PROCESSOR, WSUserSessionProcessor::new, WS_CONNECTION_SOURCE)
    .addStateStore(sessionStoreBuilder, SESSION_PROCESSOR)
    .addSink(WS_STATUS_SINK, KafkaTopics.WS_USER_ONLINE_STATUS_TOPIC, stringSerializer, stringSerializer, SESSION_PROCESSOR)

//WS session routing
    .addSource(WS_ROUTING_BY_SESSION_SOURCE, new StringDeserializer(), new StringDeserializer(),
                    KafkaTopics.WS_DELIVERY_TOPIC)
    .addProcessor(WS_ROUTING_BY_SESSION_PROCESSOR, WSSessionRoutingProcessor::new,
                    WS_ROUTING_BY_SESSION_SOURCE)
    .addStateStore(userConnectedNodesStoreBuilder, WS_ROUTING_BY_SESSION_PROCESSOR, SESSION_PROCESSOR)

//WS delivery
    .addSource(WS_DELIVERY_SOURCE, new StringDeserializer(), new StringDeserializer(),
                    INSTANCE_SPECIFIC_TOPIC)
    .addProcessor(WS_DELIVERY_PROCESSOR,  WSDeliveryProcessor::new, WS_DELIVERY_SOURCE);  

The source mentioned last in the topology is topic specific to each app instance.拓扑中最后提到的源是特定于每个应用程序实例的主题。 I want that topic to be processed only by that instance.我希望该主题仅由该实例处理。 The data to this topic is pushed by the previous processor based on which instance has to process that message.该主题的数据由前一个处理器根据必须处理该消息的实例推送。

But once stream is started, it is trying to assign instance specific topic partitions also to other instances.但是一旦 stream 启动,它就会尝试将特定于实例的主题分区也分配给其他实例。 Can we achieve this requirement in Kafka streams?我们可以在 Kafka 流中实现这个要求吗?

I want one topic to be processed only by specific instance.我希望一个主题仅由特定实例处理。

What you want is not possible.你想要的是不可能的。 For a Kafka Streams program, all instances of the same application need to be exactly the same and thus need to have the same input topics.对于 Kafka Streams 程序,同一应用程序的所有实例需要完全相同,因此需要具有相同的输入主题。

You would need to split your application into 4 apps: the first app executes the shared partition of the program and writes into the 3 different topics.您需要将您的应用程序拆分为 4 个应用程序:第一个应用程序执行程序的共享分区并写入 3 个不同的主题。 Additionally, you have 3 more applications (with own application.id s) each reading from one those topics.此外,您还有 3 个应用程序(具有自己的application.id ),每个应用程序都从这些主题中读取。

Note that if you want, you can run multiple KafkaStreams clients in the same JVM.请注意,如果需要,您可以在同一个 JVM 中运行多个KafkaStreams客户端。

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

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