Пример #1
0
        public async Task LoggerTest()
        {
            int         counter     = 0;
            KafkaServer kafkaServer = new KafkaServer();

            kafkaServer.Register(services =>
            {
                services.AddLogging(builder =>
                {
                    builder.SetMinimumLevel(LogLevel.Trace);
                });
                services.AddKafkaLogger(options =>
                {
                    options.BootstrapServers = hosts;
                    options.Category         = nameof(KafkaServiceTest);
                    options.InitializeCount  = 10;
                    //options.Key = nameof(KafkaServiceTest);
                    options.MinLevel        = LogLevel.Trace;
                    options.Topic           = $"service.{logger}";
                    options.ApplicationName = nameof(KafkaServiceTest);
                });

                services.AddKafkaConsumer(options =>
                {
                    options.BootstrapServers = hosts;
                    options.EnableAutoCommit = true;
                    options.GroupId          = $"service.{group}.logger";
                    options.Subscribers      = KafkaSubscriber.From($"service.{logger}");
                }).AddListener(result =>
                {
                    WriteLogger(result);
                    if (result.Message.Contains(nameof(KafkaServiceTest)))
                    {
                        counter++;
                    }
                });
            });
            await kafkaServer.StartAsync();

            Thread.Sleep(1000);//等待运行1秒

            var serviceProvider = kafkaServer.ServiceProvider;
            var loggerFactory   = serviceProvider.GetService <ILoggerFactory>();
            var _logger         = loggerFactory.CreateLogger <KafkaServiceTest>();

            _logger.LogTrace("LogTrace");
            _logger.LogDebug("LogDebug");
            _logger.LogInformation("LogInformation");
            _logger.LogWarning("LogWarning");
            _logger.LogError("LogError");
            _logger.LogCritical("LogCritical");

            BlockUntil(() => counter >= 6, 3000);

            Thread.Sleep(3000);//等待运行3秒
            Assert.Equal(6, counter);

            await kafkaServer.StopAsync();
        }
Пример #2
0
        public async Task ServiceTest()
        {
            int         counter     = 0;
            KafkaServer kafkaServer = new KafkaServer();

            kafkaServer.Register(services =>
            {
                services.AddLogging(builder =>
                {
                    builder.SetMinimumLevel(LogLevel.Trace);
                });
                services.AddKafkaProducer(options =>
                {
                    options.BootstrapServers = hosts;
                    options.InitializeCount  = 3;
                    options.Key   = "kafka";
                    options.Topic = $"service.{topic}";
                });

                services.AddKafkaConsumer(options =>
                {
                    options.BootstrapServers = hosts;
                    options.EnableAutoCommit = false;
                    options.GroupId          = $"service.{group}.kafka";
                    options.Subscribers      = KafkaSubscriber.From($"service.{topic}");
                }).AddListener(result =>
                {
                    Output.WriteLine(JsonSerializer.Serialize(result));
                    counter++;
                    result.Commit();
                });
            });
            await kafkaServer.StartAsync();

            Thread.Sleep(1000);//等待运行1秒

            var serviceProvider = kafkaServer.ServiceProvider;
            var factory         = serviceProvider.GetService <IKafkaProducerFactory>();
            var producer        = factory.Create();

            for (var i = 0; i < 10; i++)
            {
                await producer.PublishAsync($"{nameof(KafkaServiceTest)}{i}", "message" + i);
            }

            BlockUntil(() => counter >= 10, 3000);

            Thread.Sleep(3000);//等待运行3秒
            Assert.Equal(10, counter);

            await kafkaServer.StopAsync();
        }