无法在Kafka Streams应用程序中查询本地状态存储

时间:2018-06-05 10:16:28

标签: java spring apache-kafka apache-kafka-streams spring-kafka

我正在构建一个带有spring-kafka的kafka流应用程序,用于按键对记录进行分组并应用一些业务逻辑。我正在遵循spring-kafka-streams doc上所述的配置,但问题是,当我想从本地存储中检索值时,我收到以下错误:

org.apache.kafka.streams.errors.InvalidStateStoreException: The state store, user-data-response-count, may have migrated to another instance.
  at org.apache.kafka.streams.state.internals.QueryableStoreProvider.getStore(QueryableStoreProvider.java:60)
  at org.apache.kafka.streams.KafkaStreams.store(KafkaStreams.java:1053)
  at com.umantis.management.service.UserDataManagementService.broadcastUserDataRequest(UserDataManagementService.java:121)

这是我的KafkaStreamsConfiguration:

@Configuration
@EnableConfigurationProperties(EventsKafkaProperties.class)
@EnableKafka
@EnableKafkaStreams
public class KafkaConfiguration {

@Value("${app.kafka.streams.application-id}")
private String applicationId;

// This contains both the bootstrap servers and the schema registry url
@Autowired
private EventsKafkaProperties eventsKafkaProperties;

@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
public StreamsConfig streamsConfig() {
    Map<String, Object> props = new HashMap<>();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.eventsKafkaProperties.getBrokers());
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, this.eventsKafkaProperties.getSchemaRegistryUrl());
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

    return new StreamsConfig(props);
}

@Bean
public KGroupedStream<String, UserDataResponse> responseKStream(StreamsBuilder streamsBuilder, TopicUtils topicUtils) {
    final Map<String, String> serdeConfig = Collections.singletonMap("schema.registry.url", this.eventsKafkaProperties.getSchemaRegistryUrl());

    final Serde<UserDataResponse> valueSpecificAvroSerde = new SpecificAvroSerde<>();
    valueSpecificAvroSerde.configure(serdeConfig, false);

    return streamsBuilder
            .stream("myTopic", Consumed.with(Serdes.String(), valueSpecificAvroSerde))
            .groupByKey();
}

这是我的服务代码在getKafkaStreams().store失败:

@Slf4j
@Service
public class UserDataManagementService {

    private static final String RESPONSE_COUNT_STORE = "user-data-response-count";

    @Autowired
    private StreamsBuilderFactoryBean streamsBuilderFactory;

    public UserDataResponse broadcastUserDataRequest() {
        this.responseGroupStream.count(Materialized.as(RESPONSE_COUNT_STORE));

        if (!this.streamsBuilderFactory.isRunning()) {
            throw new KafkaStoreNotAvailableException();
        }

        // here we should have a single running kafka instance
        ReadOnlyKeyValueStore<String, Long> countStore =
                this.streamsBuilderFactory.getKafkaStreams().store(RESPONSE_COUNT_STORE, QueryableStoreTypes.keyValueStore());

        ...
    }

上下文:我在Spring启动测试中在单个实例上运行应用程序,并且我确保kafka实例处于运行状态。我在this问题上搜索了来自apache的文档,但我的情况似乎不匹配。

任何人都能指出我做错了什么并找到了解决办法吗?

我对Kafka Streams很新,所以任何帮助都会受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

好的,只是看到我在询问流工厂是否正在运行但我还没有询问kakfa流实例是否实际运行。

轮询streamsBuilderFactory.getKafkaStreams().state解决了这个问题。