public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ExecutionContext context, ILogger log) { log.LogInformation("Sender invoked"); // Retrieve the message body var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); // Deserialize it into a dynamic type dynamic data = JsonConvert.DeserializeObject(requestBody); // Retrieve an instance of the Kafka producer if (KafkaProducer == null) { KafkaProducer = KafkaHelper.GetKafkaProducer(context, log); } // Send to Kafka endpoint var deliveryReport = await KafkaProducer.ProduceAsync(KafkaTopic, DateTime.UtcNow.Ticks, requestBody); return((ActionResult) new OkObjectResult("")); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Starting order validation service in 5 seconds..."); Thread.Sleep(5000); KafkaHelper.Consume("GROUP-VALIDATION", Topics.OrderCreated, Process); }
public Handler(Settings settings, IServiceProvider provider) { this.provider = provider; this.consumer = KafkaHelper.GetPlainConsumer(settings.KafkaServer, settings.GroupId, settings.KafkaUsername, settings.KafkaPassword); }
public static void SetCurrentConfig(DeviceConfig config) { var oldConfig = CurrentConfig; CurrentConfig = config; try { if (CurrentConfig != null && !string.IsNullOrWhiteSpace(CurrentConfig.KafkaServer)) { KafkaProducer = KafkaHelper.GetPlainProducer(CurrentConfig.KafkaServer, CurrentConfig.KafkaUsername, CurrentConfig.KafkaPassword); } else { var oldProducer = KafkaProducer; KafkaProducer = null; if (oldProducer != null) { oldProducer.Dispose(); } } } catch (Exception) { } // scheduler if (CurrentConfig == null) { Task.WhenAll(Scheduler.UnscheduleRemoveOldEventsJob(), Scheduler.UnscheduleSendUnsentEventsJob()).Wait(); return; } Scheduler.StatePath = Constants.Paths.STATE_PATH; Scheduler.CurrentConfig = CurrentConfig; Scheduler.KafkaProducer = KafkaProducer; Scheduler.QCEventImageFolderPath = Settings.Instance.QCEventImageFolderPath; JsonConvert.PopulateObject(CurrentConfig.SendUnsentEventsJobSettings, Scheduler.SendUnsentEventsJobSettings); JsonConvert.PopulateObject(CurrentConfig.RemoveOldEventsJobSettings, Scheduler.RemoveOldEventsJobSettings); if (Scheduler.SendUnsentEventsJobSettings.Enabled) { Scheduler.ScheduleSendUnsentEventsJob( Scheduler.SendUnsentEventsJobSettings.SecsInterval ?? 30, CurrentConfig.NextSUEJobStart).Wait(); } else { Scheduler.UnscheduleSendUnsentEventsJob().Wait(); } if (Scheduler.RemoveOldEventsJobSettings.Enabled) { Scheduler.ScheduleRemoveOldEventsJob( Scheduler.RemoveOldEventsJobSettings.SecsInterval ?? 30, CurrentConfig.NextROEJobStart).Wait(); } else { Scheduler.UnscheduleRemoveOldEventsJob().Wait(); } }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Starting order finance service in 9 seconds..."); Thread.Sleep(9000); KafkaHelper.Consume("GROUP-FINANCE", Topics.OrderValidated, Process); //KafkaHelper.Consume<Order>("GROUP-FOR-ALL", Topics.OrderValidated, Process); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Starting order shipping service in 11 seconds..."); Thread.Sleep(11000); KafkaHelper.Consume("GROUP-SHIPPING", Topics.OrderValidated, Process); //KafkaHelper.Consume<Order>("GROUP-FOR-ALL", Topics.OrderValidated, Process); }
public static IServiceCollection AddKafka(this IServiceCollection services) { using var seriveProvider = services.BuildServiceProvider(); var configuration = seriveProvider.GetService <IConfiguration>(); var producerConfig = new ProducerConfig(); var consumerConfig = new ConsumerConfig(); configuration.Bind("ProducerConfig", producerConfig); configuration.Bind("ConsumerConfig", consumerConfig); services.AddSingleton <ProducerConfig>(producerConfig); services.AddSingleton <ConsumerConfig>(consumerConfig); services.AddSingleton <IBusClient, KafkaBusClient>(); var env = configuration.GetValue <string>("Env"); if (string.IsNullOrWhiteSpace(env)) { throw new ArgumentNullException(nameof(env)); } var bootstrapServers = producerConfig.BootstrapServers; if (string.IsNullOrWhiteSpace(bootstrapServers)) { bootstrapServers = consumerConfig.BootstrapServers; } int numPartitions = 4; int customPartition = configuration.GetValue <int>("KafkaConfig:Partition"); if (customPartition > 0) { numPartitions = customPartition; } short replicationFactor = 1; short customReplicationFactor = configuration.GetValue <short>("KafkaConfig:ReplicationFactor"); if (customReplicationFactor > 0) { replicationFactor = customReplicationFactor; } short inSyncReplicas = 1; short customInSyncReplicas = configuration.GetValue <short>("KafkaConfig:MinInSyncReplicas"); if (customInSyncReplicas > 0) { inSyncReplicas = customInSyncReplicas; } // create topic with default 4 partition for both producer and consumer to prevent the case where consumer run before producer. if (!string.IsNullOrWhiteSpace(bootstrapServers)) { KafkaHelper.CreateTopics(bootstrapServers, env, numPartitions, replicationFactor, inSyncReplicas); } return(services); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { KafkaHelper.Subscribe <AuditTableKafkaMessage <object> >(Constants.KAFKA_URL_SERVER, Constants.TOPIC_AUDIT_TABLE, Constants.GROUP_ID_AUDIT_TABLE, WriteLog); await Task.Delay(1000, stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { KafkaHelper.Subscribe <LogonKafkaMessage>(Constants.KAFKA_URL_SERVER, Constants.TOPIC_LOG_ON, Constants.GROUP_ID_LOG_ON, WriteLog); await Task.Delay(1000, stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { KafkaHelper.Subscribe <UpdateUserNotificationKafkaMessage>(Constants.KAFKA_URL_SERVER, Constants.TOPIC_UPDATE_NOTIFICATION, Constants.GROUP_ID_UPDATE_NOTIFICATION, UpdateNotification); await Task.Delay(1000, stoppingToken); } }
static void UsingKafka() { try { var order = OrderExtensions.Create(); KafkaHelper.Produce(order.CreateMessage(Topics.OrderCreated)); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static void Process(OrderMessage orderMessage) { try { Console.WriteLine($"Processing order {orderMessage.Order.OrderId}"); orderMessage.Order.Ship(); KafkaHelper.Produce(orderMessage.Order.CreateMessage(Topics.OrderShipped)); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static async Task Main(string[] args) { Console.WriteLine("TimeOff Employee Terminal\n"); Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", true, true) .Build(); // Read configs _adminConfig = Configuration.GetSection(nameof(AdminClientConfig)).Get <AdminClientConfig>(); _schemaRegistryConfig = Configuration.GetSection(nameof(SchemaRegistryConfig)).Get <SchemaRegistryConfig>(); _producerConfig = Configuration.GetSection(nameof(ProducerConfig)).Get <ProducerConfig>(); _producerConfig.ClientId = Dns.GetHostName(); await KafkaHelper.CreateTopicAsync(_adminConfig, ApplicationConstants.LeaveApplicationsTopicName, 3); await AddMessagesAsync(); }
public async Task <TResponse <LogOnResponse> > LogOn(LogOnModel request, string userAgent, string ipAddress) { try { string password = Sha512(request.Password); var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <LogOnResponse>(SqlQuery.USER_LOGON, new { request.Username, Password = password }); if (result.IsSuccess) { if (result.Data != null) { result.Data.Token = new AuthenticationModule().GenerateTokenForUser(result.Data.Id, request.Username, _secretKey); KafkaHelper.PublishMessage(Constants.KAFKA_URL_SERVER, Constants.TOPIC_LOG_ON, new LogonKafkaMessage { UserId = result.Data.Id, UserAgent = userAgent, IpAddress = ipAddress, DateTime = DateTime.Now }); return(await Ok(result.Data)); } return(await Fail <LogOnResponse>(ErrorEnum.AUTHENTICATION_WRONG.GetStringValue())); } return(await Fail <LogOnResponse>(result.Message)); } catch (Exception exception) { return(await Fail <LogOnResponse>(exception)); } }
private static async Task Main(string[] args) { Console.WriteLine("TimeOff Manager Terminal\n"); Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", true, true) .Build(); _adminConfig = Configuration.GetSection(nameof(AdminClientConfig)).Get <AdminClientConfig>(); _schemaRegistryConfig = Configuration.GetSection(nameof(SchemaRegistryConfig)).Get <SchemaRegistryConfig>(); _consumerConfig = Configuration.GetSection(nameof(ConsumerConfig)).Get <ConsumerConfig>(); // Read messages from start if no commit exists. _consumerConfig.AutoOffsetReset = AutoOffsetReset.Earliest; _producerConfig = Configuration.GetSection(nameof(ProducerConfig)).Get <ProducerConfig>(); _producerConfig.ClientId = Dns.GetHostName(); await KafkaHelper.CreateTopicAsync(_adminConfig, ApplicationConstants.LeaveApplicationResultsTopicName, 1); await StartManagerConsumer(); }
static void Main(string[] args) { string header = "wenli.drive.kafka测试"; Console.Title = header; Console.WriteLine(header); var color = Console.ForegroundColor; var pub = new KafkaHelper("Test", true); var sub = new KafkaHelper("Test", false); Task.Run(() => { while (true) { var msg = string.Format("{0}这是一条测试消息", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); pub.Pub(new List <string>() { msg }); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("发送消息:" + msg); Console.ForegroundColor = color; Thread.Sleep(500); } }); Task.Run(() => { sub.Sub((msg) => { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(string.Format("收到消息:{0}", msg)); Console.ForegroundColor = color; }); }); Console.ReadLine(); }
/// <summary> /// 生产异步 /// </summary> /// <param name="Key">Message.Key</param> /// <param name="Value">Message.Value</param> /// <param name="Topic">主题</param> /// <returns></returns> public async Task ProduceAsync(TKey Key, TValue Value, string Topic) { var producerBuilder = new ProducerBuilder <TKey, TValue>(ProducerConfig); producerBuilder.SetValueSerializer(new KafkaConverter <TValue>()); using var producer = producerBuilder.Build(); try { var dr = await producer.ProduceAsync(Topic, new Message <TKey, TValue> { Key = Key, Value = Value }); //Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'"); } catch (ProduceException <Null, string> ex) { Logger.Error(LoggerType.KafkaException, $"Topic:{Topic},ServerIp:{KafkaHelper.GetServerIp()},ServerName:{ KafkaHelper.GetServerName()},Delivery failed: { ex.Error.Reason}", null, ex.Message + ex.StackTrace); } }
/// <summary> /// 生产 /// </summary> /// <param name="Key">Message.Key 做消息指定分区投放有用的</param> /// <param name="Value">Message.Value</param> /// <param name="Topic">主题</param> public void Produce(TKey Key, TValue Value, string Topic) { var producerBuilder = new ProducerBuilder <TKey, TValue>(ProducerConfig); producerBuilder.SetValueSerializer(new KafkaConverter <TValue>());//设置序列化方式 using var producer = producerBuilder.Build(); try { producer.Produce(Topic, new Message <TKey, TValue> { Key = Key, Value = Value }, (result) => { if (result.Error.IsError) { Logger.Error(LoggerType.KafkaException, $"Topic:{Topic},ServerIp:{KafkaHelper.GetServerIp()},ServerName:{ KafkaHelper.GetServerName()}", null, $"Delivery Error:{result.Error.Reason}"); } });//Value = JsonConvert.SerializeObject(value) } catch (ProduceException <Null, string> ex) { Logger.Error(LoggerType.KafkaException, $"Topic:{Topic},Delivery failed: { ex.Error.Reason}", null, ex.Message + ex.StackTrace); } }