java在kstreams应用程序中使用自定义Kafka状态存储
我们正在使用spring cloud stream Hoxton RC7项目中包含的卡夫卡流(因此使用提供的卡夫卡流和卡夫卡客户端版本[2.3.1])
ext {
set('springCloudVersion', 'Hoxton.SR7')
}
...
dependencies {
// spring cloud stream
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka-streams'
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka'
implementation("org.springframework.cloud:spring-cloud-stream")
// redis
implementation 'io.lettuce:lettuce-core'
implementation 'org.springframework.data:spring-data-redis'
testCompile 'it.ozimov:embedded-redis:0.7.2'
...
我们已经实现了一个kstreams应用程序
@Bean
public Consumer<KStream<String, IncomingEvent>> process() {
return input -> {
我们在其中进行一些聚合,例如:
.aggregate(Foo::new, (key, value1, aggregate) ->
(aggregate == null || aggregate.getLastModified() == null || this.mustProcess(key, value1))
? value1
: aggregate,
materialized
)
现在具体化的应该是自定义外部状态存储(Redis):
Materialized<String, Foo, KeyValueStore<Bytes, byte[]>> materialized =
Materialized.as("redis-store");
它由StoreBuilder Bean提供:
@Bean
public StoreBuilder<KeyValueStore<String, Foo>> builder(RedisKeyValueStoreBytes redisKeyValueStoreBytes){
return Stores.keyValueStoreBuilder(supplier(redisKeyValueStoreBytes),
new Serdes.StringSerde(),
new SomeFooSerde());
}
public static KeyValueBytesStoreSupplier supplier(RedisKeyValueStoreBytes redisKeyValueStoreBytes) {
return new KeyValueBytesStoreSupplier() {
@Override
public String name() {
return "redis-store";
}
@Override
public KeyValueStore<Bytes, byte[]> get() {
return redisKeyValueStoreBytes;
}
@Override
public String metricsScope() {
return "redis-session-state";
}
};
}
现在,我使用嵌入式卡夫卡测试应用程序:
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@SpringBootTest(classes = {TestConfigurationTests.class})
@EmbeddedKafka(count = 3, ports = {29901, 29902, 29903}, zookeeperPort = 33991)
public class TestKafkaIntegration {
我尝试访问状态存储并查询添加的项目的位置:
ReadOnlyKeyValueStore<String, Foo> queryableStore = interactiveQueryService.getQueryableStore(
"redis-store", QueryableStoreTypes.keyValueStore());
return queryableStore;
但当我运行测试时,我收到一个错误:
Caused by: org.springframework.kafka.KafkaException: Could not start stream: ; nested exception is org.springframework.kafka.KafkaException: Could not start stream: ; nested exception is org.apache.kafka.streams.errors.TopologyException: Invalid topology: StateStore redis-store is already added.
几个问题:
- [1]解释的使用自定义状态存储的示例在处理器中使用它。这是否自动意味着我不能在聚合中使用自定义状态存储李>
- 当无法在聚合中使用它时,使用自定义状态存储还有什么意义李>
- 当我稍微更改上面的kstreams代码并定义一个处理器而不是在聚合方法中使用物化时,错误会发生变化,然后它会在尝试执行getQueryableStore时抱怨缺少状态“redis store”store。但事实上,我可以看到addStateStoreBeans注册了“redis存储”。这怎么会发生李>
我希望使用自定义状态存储的原因是,我无法(非常容易)为应用程序实例提供专用硬盘。为了快速启动应用程序,我希望避免在每次启动应用程序时处理完整的变更日志(最好一天进行几次,目前需要一个多小时)。现在是最后一个问题:
- 使用自定义外部状态存储时,我是否能够恢复到应用程序重新启动时的最后一个状态李>
# 1 楼答案
您正在使用Materialized.as(java.lang.String storeName),它将创建(具体化)一个具有给定名称的
StateStore
(“redis store”在这里)。另一方面,通过builder(RedisKeyValueStoreBytes redisKeyValueStoreBytes)
,您正在创建另一个同名的另一个StateStore
,springframework可能会自动将其添加到拓扑中,这样您就会出现“store is ready added”错误问题1:可以在聚合中使用自定义状态存储;与Materialized.as(KeyValueBytesStoreSupplier supplier)一起使用
问题2:还可以使用带有转换器的
StateStore
或自定义处理器进行交互式查询;还有一个全局StateStore
可以访问整个主题,而不是只使用KafkaStreams实例分配的分区(参见addGlobalStore和globalTable)问题3:我猜您没有(手动)向拓扑注册状态存储;见Topology.addStateStore(StoreBuilder<?> storeBuilder, java.lang.String... processorNames)和Connecting Processors and State Stores
问题4:是的,状态存储is loaded from a changelog topic(使用optimizations时可能是原始主题)