コード例 #1
0
        #pragma warning disable 8618
        public EventConsumer(
            IEventDeserializer eventDeserializer,
            EventConsumerConfig config,
            ILogger <EventConsumer <TAggregate, TAggregateId, TDeserializer> > logger)
        {
            _eventDeserializer = eventDeserializer;
            _logger            = logger;

            var aggregateType = typeof(TAggregate);

            var consumerConfig = new ConsumerConfig
            {
                GroupId            = config.ConsumerGroup,
                BootstrapServers   = config.KafkaConnectionString,
                AutoOffsetReset    = AutoOffsetReset.Earliest,
                EnablePartitionEof = true
            };

            var consumerBuilder        = new ConsumerBuilder <TAggregateId, string>(consumerConfig);
            var keyDeserializerFactory = new KeyDeserializerFactory();

            consumerBuilder.SetKeyDeserializer(keyDeserializerFactory.Create <TDeserializer, TAggregateId>());

            _eventConsumer = consumerBuilder.Build();

            var topicName = $"{config.TopicBaseName}-{aggregateType.Name}";

            _eventConsumer.Subscribe(topicName);
        }
コード例 #2
0
        public static IServiceCollection AddKafkaEventProducer <TAggregate, TAggregateId>(
            this IServiceCollection services,
            EventConsumerConfig configuration)
            where TAggregate : class, IAggregate <TAggregateId>
            where TAggregateId : class, IAggregateId
        {
            return(services.AddSingleton <IEventProducer <TAggregate, TAggregateId> >(
                       ctx =>
            {
                var logger = ctx.GetRequiredService <ILogger <EventProducer <TAggregate, TAggregateId> > >();

                return new EventProducer <TAggregate, TAggregateId>(
                    configuration.TopicBaseName,
                    configuration.KafkaConnectionString,
                    logger);
            }));
        }