コード例 #1
0
        public static IServiceCollection AddKafka(
            this IServiceCollection services,
            Action <IKafkaConfigurationBuilder> kafka)
        {
            var configurator = new KafkaFlowConfigurator(
                new MicrosoftDependencyConfigurator(services),
                kafka);

            return(services.AddSingleton(configurator));
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var container = new UnityContainer();

            var configurator = new KafkaFlowConfigurator(
                new UnityDependencyConfigurator(container),
                kafka => kafka
                .UseLogHandler <ConsoleLogHandler>()
                .AddCluster(
                    cluster => cluster
                    .WithBrokers(new[] { "localhost:9092" })
                    .AddConsumer(
                        consumer => consumer
                        .Topic("test-topic")
                        .WithGroupId("print-console-handler")
                        .WithBufferSize(100)
                        .WithWorkersCount(10)
                        .WithAutoOffsetReset(AutoOffsetReset.Latest)
                        .AddMiddlewares(
                            middlewares => middlewares
                            .AddCompressor <GzipMessageCompressor>()
                            .AddSerializer <ProtobufMessageSerializer, SampleMessageTypeResolver>()
                            .AddTypedHandlers(
                                handlers => handlers
                                .WithHandlerLifetime(global::KafkaFlow.InstanceLifetime.Singleton)
                                .AddHandler <PrintConsoleHandler>())
                            )
                        )
                    .AddConsumer(
                        consumer => consumer
                        .Topic("test-topic-json")
                        .WithGroupId("print-console-handler")
                        .WithBufferSize(100)
                        .WithWorkersCount(10)
                        .WithAutoOffsetReset(AutoOffsetReset.Latest)
                        .AddMiddlewares(
                            middlewares => middlewares
                            .AddCompressor <GzipMessageCompressor>()
                            .AddSerializer <JsonMessageSerializer, SampleMessageTypeResolver>()
                            .AddTypedHandlers(
                                handlers => handlers
                                .WithHandlerLifetime(global::KafkaFlow.InstanceLifetime.Singleton)
                                .AddHandler <PrintConsoleHandler>())
                            )
                        )
                    )
                );

            var bus = configurator.CreateBus(new UnityDependencyResolver(container));

            bus.StartAsync().GetAwaiter().GetResult();

            var consumerAcessor = container.Resolve <IConsumerAccessor>();

            while (true)
            {
                var input = Console.ReadLine();

                switch (input)
                {
                case "pause":

                    foreach (var consumer in consumerAcessor.All)
                    {
                        consumer.Pause(consumer.Assignment);
                    }

                    break;

                case "resume":

                    foreach (var consumer in consumerAcessor.All)
                    {
                        consumer.Resume(consumer.Assignment);
                    }

                    break;

                case "exit":
                    return;
                }
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: slang25/kafka-flow
        static void Main(string[] args)
        {
            var container = new UnityContainer();

            var configurator = new KafkaFlowConfigurator(
                new UnityDependencyConfigurator(container),
                kafka => kafka
                .UseLogHandler <ConsoleLogHandler>()
                .AddCluster(
                    cluster => cluster
                    .WithBrokers(new[] { "localhost:9092" })
                    .AddProducer <PrintConsoleProtobufProducer>(
                        producer => producer
                        .DefaultTopic("test-topic")
                        .AddMiddlewares(
                            middlewares => middlewares
                            .AddSerializer <ProtobufMessageSerializer, SampleMessageTypeResolver>()
                            .AddCompressor <GzipMessageCompressor>()
                            )
                        .WithAcks(Acks.All)
                        )
                    .AddProducer <PrintConsoleJsonProducer>(
                        producer => producer
                        .DefaultTopic("test-topic-json")
                        .AddMiddlewares(
                            middlewares => middlewares
                            .AddSerializer <JsonMessageSerializer, SampleMessageTypeResolver>()
                            .AddCompressor <GzipMessageCompressor>()
                            )
                        .WithAcks(Acks.All)
                        )
                    )
                );

            container.RegisterType <PrintConsoleProtobufProducer>();
            container.RegisterType <PrintConsoleJsonProducer>();

            var bus = configurator.CreateBus(new UnityDependencyResolver(container));

            bus.StartAsync().GetAwaiter().GetResult();

            var printConsole     = container.Resolve <PrintConsoleProtobufProducer>();
            var printConsoleJson = container.Resolve <PrintConsoleJsonProducer>();

            while (true)
            {
                Console.Write("Number of messages to produce: ");
                var count = int.Parse(Console.ReadLine());

                for (var i = 0; i < count; i++)
                {
                    printConsole.ProduceAsync(new TestMessage {
                        Text = $"Protobuf Message: {Guid.NewGuid()}"
                    });
                    printConsole.ProduceAsync(new TestMessage2 {
                        Value = $"Protobuf Message 2: {Guid.NewGuid()}"
                    });
                    printConsoleJson.ProduceAsync(new TestMessage {
                        Text = $"Json Message: {Guid.NewGuid()}"
                    });
                }
            }
        }