bch*_*392 2 stateful apache-kafka rocksdb apache-kafka-streams
我有一个执行一些状态处理的微服务。应用程序从输入主题构建 KStream,进行一些状态处理,然后将数据写入输出主题。
我将在同一组中运行 3 个该应用程序。当微服务发生故障时,我需要存储 3 个参数,接管的微服务可以查询共享状态存储并从崩溃的服务停止的地方继续。
我正在考虑将这 3 个参数推送到状态存储中,并在其他微服务接管时查询数据。从我的研究中,我看到了很多人们使用状态存储执行事件计数的例子,但这并不完全是我想要的,有谁知道一个例子或者解决这个问题的正确方法是什么?
所以你想做两件事:
A。关闭的服务必须存储参数:
如果您想以一种简单的方式执行此操作,那么您所要做的就是在与状态存储关联的主题中写入一条消息(您正在使用 读取的消息KTable)。使用Kafka Producer API或KStream(可能是kTable.toStream())来完成此操作,仅此而已。
否则,您可以手动创建状态存储:
// take these serde as just an example
Serde<String> keySerde = Serdes.String();
Serde<String> valueSerde = Serdes.String();
KeyValueBytesStoreSupplier storeSupplier = inMemoryKeyValueStore(stateStoreName);
streamsBuilder.addStateStore(Stores.keyValueStoreBuilder(storeSupplier, keySerde, valueSerde));
Run Code Online (Sandbox Code Playgroud)
然后在变压器或处理器中使用它来向其中添加项目;你必须在变压器/处理器中声明这一点:
// depending on the serde above you might have something else then String
private KeyValueStore<String, String> stateStore;
Run Code Online (Sandbox Code Playgroud)
并初始化stateStore变量:
@Override
public void init(ProcessorContext context) {
stateStore = (KeyValueStore<String, String>) context.getStateStore(stateStoreName);
}
Run Code Online (Sandbox Code Playgroud)
然后使用该stateStore变量:
@Override
public KeyValue<String, String> transform(String key, String value) {
// using stateStore among other actions you might take here
stateStore.put(key, processedValue);
}
Run Code Online (Sandbox Code Playgroud)
b. 读取接管服务中的参数:
您可以使用 Kafka 消费者来完成此操作,但使用 Kafka Streams 时,您首先必须使存储可用;最简单的方法是创建一个 KTable;然后您必须获取使用 KTable 自动创建的可查询商店名称;然后你必须真正进入商店;然后从存储中提取记录值(即按其键的参数值)。
// this example is a modified copy of KTable javadocs example
final StreamsBuilder streamsBuilder = new StreamsBuilder();
// Creating a KTable over the topic containing your parameters a store shall automatically be created.
//
// The serde for your MyParametersClassType could be
// new org.springframework.kafka.support.serializer.JsonSerde(MyParametersClassType.class)
// though further configurations might be necessary here - e.g. setting the trusted packages for the ObjectMapper behind JsonSerde.
//
// If the parameter-value class is a String then you could use Serdes.String() instead of a MyParametersClassType serde.
final KTable paramsTable = streamsBuilder.table("parametersTopicName", Consumed.with(Serdes.String(), <<your InstanceOfMyParametersClassType serde>>));
...
// see the example from KafkaStreams javadocs for more KafkaStreams related details
final KafkaStreams streams = ...;
streams.start()
...
// get the queryable store name that is automatically created with the KTable
final String queryableStoreName = paramsTable.queryableStoreName();
// get access to the store
ReadOnlyKeyValueStore view = streams.store(queryableStoreName, QueryableStoreTypes.timestampedKeyValueStore());
// extract a record value from the store
InstanceOfMyParametersClassType parameter = view.get(key);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1626 次 |
| 最近记录: |