zoukankan      html  css  js  c++  java
  • Kafka.net使用编程入门(二)

    1.首先创建一个Topic,命令如下:

    kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic MyTopic

    2.创建两个控制台程序:

    3.KafkaProducer程序: 

    复制代码
    class Program
        {
            static void Main(string[] args)
            {
                do
                {
                    Produce(GetKafkaBroker(), getTopicName());
                    System.Threading.Thread.Sleep(3000);
                } while (true);
            }
    
            private static void Produce(string broker, string topic)
            {
                var options = new KafkaOptions(new Uri(broker));
                var router = new BrokerRouter(options);
                var client = new Producer(router);
    
                var currentDatetime =DateTime.Now;
                var key = currentDatetime.Second.ToString();
                var events = new[] { new Message("Hello World " + currentDatetime, key) };
                client.SendMessageAsync(topic, events).Wait(1500);
                Console.WriteLine("Produced: Key: {0}. Message: {1}", key, events[0].Value.ToUtf8String());
    
                using (client) { }
            }
    
            private static string GetKafkaBroker()
            {
                string KafkaBroker = string.Empty;
                const string kafkaBrokerKeyName = "KafkaBroker";
    
                if (!ConfigurationManager.AppSettings.AllKeys.Contains(kafkaBrokerKeyName))
                {
                    KafkaBroker = "http://localhost:9092";
                }
                else
                {
                    KafkaBroker = ConfigurationManager.AppSettings[kafkaBrokerKeyName];
                }
                return KafkaBroker;
            }
            private static string getTopicName()
            {
                string TopicName = string.Empty;
                const string topicNameKeyName = "Topic";
    
                if (!ConfigurationManager.AppSettings.AllKeys.Contains(topicNameKeyName))
                {
                    throw new Exception("Key "" + topicNameKeyName + "" not found in Config file -> configuration/AppSettings");
                }
                else
                {
                    TopicName = ConfigurationManager.AppSettings[topicNameKeyName];
                }
                return TopicName;
            }
        }
    复制代码

    4.KafkaConsumer程序:

    复制代码
    class Program
        {
            static void Main(string[] args)
            {
                Consume(getKafkaBroker(), getTopicName());
                
            }
    
            private static void Consume(string broker, string topic)
            {   
                var options = new KafkaOptions(new Uri(broker));
                var router = new BrokerRouter(options);
                var consumer = new Consumer(new ConsumerOptions(topic, router));
    
                //Consume returns a blocking IEnumerable (ie: never ending stream)
                foreach (var message in consumer.Consume())
                {
                    Console.WriteLine("Response: Partition {0},Offset {1} : {2}",
                        message.Meta.PartitionId, message.Meta.Offset, message.Value.ToUtf8String());
                }
            }
    
            private static string getKafkaBroker()
            {
                string KafkaBroker = string.Empty;
                var KafkaBrokerKeyName = "KafkaBroker";
    
                if (!ConfigurationManager.AppSettings.AllKeys.Contains(KafkaBrokerKeyName))
                {
                    KafkaBroker = "http://localhost:9092";
                }
                else
                {
                    KafkaBroker = ConfigurationManager.AppSettings[KafkaBrokerKeyName];
                }
                return KafkaBroker;
            }
    
            private static string getTopicName()
            {
                string TopicName = string.Empty;
                var TopicNameKeyName = "Topic";
    
                if (!ConfigurationManager.AppSettings.AllKeys.Contains(TopicNameKeyName))
                {
                    throw new Exception("Key "" + TopicNameKeyName + "" not found in Config file -> configuration/AppSettings");
                }
                else
                {
                    TopicName = ConfigurationManager.AppSettings[TopicNameKeyName];
                }
                return TopicName;
            }
        }
    复制代码

    5.Consumer结果:

  • 相关阅读:
    (zt)在PHP中使用全局变量
    (zt)Flash与C++交互
    (zt)关于Flash Socket通信的安全策略问题的一点心得
    (zt)svn 随服务器启动
    使用InstallShield安装和卸载SQL Server数据库(利用sql脚本)
    异常查看部分代码
    VC为控件添加背景
    深入分析MFC文档视图结构
    VC数据库编程概述
    (转)WEB程序打包详解:(连接SQL2005数据库,修改配置文件,建立虚拟目录)
  • 原文地址:https://www.cnblogs.com/zxtceq/p/9067831.html
Copyright © 2011-2022 走看看