1

What's an «idiomatic» way of configuring (providing host and port info) and accessing Interactive Queries in Kafka Streams + Spring Boot application?

What's the correct way to access KafkaStreams instance to access the state stores?

I'm aware of InteractiveQueriesService in spring-cloud-stream but I wasn't to use just spring-kafka library with Spring Boot.

Thank you

Vik Gamov
  • 5,446
  • 1
  • 26
  • 46

1 Answers1

2

Spring Boot auto-configures Kafka Streams support: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-kafka-streams.

The infrastructure underneath is hidden in the StreamsBuilderFactoryBean. See Spring for Apache Kafka docs: https://docs.spring.io/spring-kafka/docs/2.6.2/reference/html/#streams-spring

So, you indeed can get access into that spawn KafkaStreams instance using API StreamsBuilderFactoryBean.getKafkaStreams().

There is no high-level support for Interactive Queries, but probably having access to that KafkaStreams instance, it should not be so hard for you to go ahead with state stores interaction. Looks like that InteractiveQueryService is just a delegation to KafkaStreams.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • 2
    hi Artem. your answers are always appreciated. just created a small POC https://gist.github.com/gAmUssA/69d4d6da2cf06b5015b3514f14a8e210. the main concern is how to access `KafkaStreams`. I'm trying to inject it to RestController but during rest controller initialization KafkaStreams instance may not be available. so I use `EventListener` to update the instance after the context is refreshed. it works but looks like hack to me. Coudl you suggest a better/idiomatic way to do it? – Vik Gamov Oct 19 '20 at 16:55
  • 1
    Yes, you can't do `streamsBuilderFactoryBean.getKafkaStreams()` from the ctor, just after injection. That internal `KafkaStreams` instance is initialized from the `start()` phase later on. I don't see a big problem to use that `streamsBuilderFactoryBean.getKafkaStreams()` from your `getValue()` REST method. Or you can initialize it "on demand" manner via some `private` method... On the other hand your `@EventListener(ContextRefreshedEvent.class)` is OK, too. Since it is really a point when we have `StreamsBuilderFactoryBean` started already. – Artem Bilan Oct 19 '20 at 17:14