Beispiel #1
0
        public static void KafkaTest()
        {
            var header = "kafka测试";

            Console.Title = header;
            Console.WriteLine(header);
            ConsoleColor color = Console.ForegroundColor;

            var pub = new KafkaHelper("Test", true);

            var sub = new KafkaHelper("Test", false);

            Task.Run(() =>
            {
                while (true)
                {
                    var msg = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}这是一条测试消息";

                    pub.Pub(new List <string> {
                        msg
                    });
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"发消息:{msg}");

                    Thread.Sleep(2000);
                }
            });

            Task.Run(() =>
            {
                sub.Sub(msg =>
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine($"收到消息:{msg}");
                });
            });

            Console.ReadLine();
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            var header = "kafka测试";

            Console.Title = header;
            Console.WriteLine(header);
            ConsoleColor color = Console.ForegroundColor;

            var pub = new KafkaHelper("Test", true);

            var sub = new KafkaHelper("Test", false);

            Task.Run(() =>
            {
                while (true)
                {
                    string msg = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}这是一条测试消息";
                    pub.Pub(new List <string>()
                    {
                        msg
                    });

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"发送消息{msg}");

                    Thread.Sleep(2000);
                }
            });

            Task.Run(() =>
            {
                sub.Sub(msg =>
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine($"收到消息{msg}");
                });
            });

            Console.ReadLine();

            //string invalidArgErrorMessage = "有效的args是:produce或consume";

            //if (args.Length < 1)
            //{
            //    throw (new Exception(invalidArgErrorMessage));
            //}

            //string intent = args[1];

            //_topic = ConfigurationManager.AppSettings["Topic"];
            //if (String.Equals(intent, "consume", StringComparison.OrdinalIgnoreCase))
            //{
            //    Console.WriteLine("开始消费者服务");
            //    Consume();
            //}
            //else if (String.Equals(intent, "produce", StringComparison.OrdinalIgnoreCase))
            //{
            //    Console.WriteLine("开始生产者服务");
            //    Produce();
            //}
            //else
            //{
            //    throw (new Exception(invalidArgErrorMessage));
            //}
        }