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(); }
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(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var hosts = new string[] { "192.168.209.133:9092", "192.168.209.134:9092", "192.168.209.135:9092" }; #region 日志记录 services.AddKafkaConsumer(options => { options.BootstrapServers = hosts; options.EnableAutoCommit = true;//自动提交 options.GroupId = "group.1"; options.Subscribers = KafkaSubscriber.From("topic.logger"); }).AddListener(result => { Console.WriteLine("Message From topic.logger:" + result.Message); }); #endregion #region Kafka services.AddKafkaConsumer(options => { options.BootstrapServers = hosts; options.EnableAutoCommit = false; options.GroupId = "group.2"; options.Subscribers = KafkaSubscriber.From("topic.kafka"); }).AddListener(result =>//直接在lambda表达式中完成消费逻辑 { Console.WriteLine("Message From topic.kafka:" + result.Message); result.Commit(); }).AddListener <KafkaConsumerListener>();//实现IKafkaConsumerListener接口完成消费逻辑 #endregion services.AddControllers(); }