简体   繁体   English

将多个事件类型添加到 .net 中的同一个 kafka 主题

[英]Adding multiple event type to same kafka topic in .net

I am trying to add multiple schemas to the same subject in the schema registry, so I have set ValueSubjectNameStrategy to SubjectNameStrategy.TopicRecord , also set the register automatically to AutomaticRegistrationBehavior.Always .我正在尝试向架构注册表中的同一主题添加多个架构,因此我已将ValueSubjectNameStrategy设置为SubjectNameStrategy.TopicRecord ,还将注册自动设置为AutomaticRegistrationBehavior.Always But while auto registering the schema it still using the SubjectNameStrategy.Topic strategy.但是在自动注册模式时,它仍然使用 SubjectNameStrategy.Topic 策略。

 var schemaRegistryConfig = new SchemaRegistryConfig { Url = "http://localhost:8081", ValueSubjectNameStrategy = SubjectNameStrategy.TopicRecord };
        var registry = new CachedSchemaRegistryClient(schemaRegistryConfig);
        var builder = new ProducerBuilder<string, SplitLineKGN>(KafkaConfig.Producer.GetConfig(_config.GetSection("KafkaProducer")))
                    .SetAvroValueSerializer(registry, registerAutomatically: AutomaticRegistrationBehavior.Always)
                    .SetErrorHandler((_, error) => Console.Error.WriteLine(error.ToString()));
        _producerMsg = builder.Build();
await _producerMsg.ProduceAsync("MyTopic", new Message<string, SampleMessage> { Key = key, Value = line });

how to auto register multiple schemas to a topic?如何将多个模式自动注册到一个主题?

  1. SchemaRegistryConfig.ValueSubjectNameStrategy is deprecated, it should now be configured using the serializer's configuration: code SchemaRegistryConfig.ValueSubjectNameStrategy已弃用,现在应该使用序列化程序的配置进行配置: 代码

  2. For producing multiple event types with a single producer you have to use AvroSerializer<ISpecificRecord> as described below:要使用单个生产者生成多种事件类型,您必须使用AvroSerializer<ISpecificRecord> ,如下所述:

     var schemaRegistryConfig = new SchemaRegistryConfig { Url = "http://localhost:8081" }; using var schemaRegistryClient = new CachedSchemaRegistryClient(schemaRegistryConfig); var avroSerializerConfig = new AvroSerializerConfig { SubjectNameStrategy = SubjectNameStrategy.TopicRecord, AutoRegisterSchemas = true }; var producerConfig = KafkaConfig.Producer.GetConfig(_config.GetSection("KafkaProducer")); using var producer = new ProducerBuilder<string, ISpecificRecord>(producerConfig) .SetValueSerializer(new AvroSerializer<ISpecificRecord>(schemaRegistryClient, avroSerializerConfig)) .SetErrorHandler((_, error) => Console.Error.WriteLine(error)) .Build(); await producer.ProduceAsync("MyTopic", new Message<string, ISpecificRecord> { Key = key, Value = line });

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

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