The code for ReceiveLogs.cs:
using System; using RabbitMQ.Client; using RabbitMQ.Client.Events; using System.Text; class ReceiveLogs { public static void Main() { var factory = new ConnectionFactory() { HostName = "localhost" }; using(var connection = factory.CreateConnection()) using(var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: "logs", type: "fanout"); var queueName = channel.QueueDeclare().QueueName; channel.QueueBind(queue: queueName, exchange: "logs", routingKey: ""); Console.WriteLine(" [*] Waiting for logs."); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] {0}", message); }; channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }(ReceiveLogs.cs source)
Follow the setup instructions from tutorial one to generate the EmitLogs and ReceiveLogs projects.
´Ó½Ì³ÌµÄµÚÒ»Õ¿ªÊ¼£¬¸úË氲װ˵Ã÷À´Éú³É EmitLogs ºÍ ReceiveLogs ¹¤³Ì¡£
If you want to save logs to a file, just open a console and type:
Èç¹ûÄãÏë±£´æÈÕÖ¾µ½Ò»¸öÎļþ£¬Ö»Ðè´ò¿ª¿ØÖÆ̨²¢ÊäÈ룺
cd ReceiveLogs dotnet run > logs_from_rabbit.logIf you wish to see the logs on your screen, spawn a new terminal and run:
Èç¹ûÄãÏëÔÚÆÁÄ»Éϲ鿴ÈÕÖ¾£¬ÖØ¿ªÒ»¸öеÄÖն˲¢ÔËÐУº
cd ReceiveLogs dotnet runAnd of course, to emit logs type:
µ±È»£¬²úÉúÈÕÖ¾Ö»ÐèÊäÈ룺
cd EmitLog dotnet runUsing rabbitmqctl list_bindings you can verify that the code actually creates bindings and queues as we want. With two ReceiveLogs.cs programs running you should see something like:
ʹÓà rabbitmqctl list_bindings ÃüÁîÄã¿ÉÒÔºËʵ´úÂëµÄÈ·ÒѾ´´½¨ÁËÎÒÃÇÆÚÍûµÄ°ó¶¨ºÍ¶ÓÁУ¬°éËæ×Å ReceiveLogs.cs ³ÌÐòµÄÔËÐÐÄãÓ¦¸Ã¿ÉÒÔ¿´µ½ÀàËÆÈçÏÂÄÚÈÝ£º
sudo rabbitmqctl list_bindings # => Listing bindings ... # => logs exchange amq.gen-JzTY20BRgKO-HjmUJj0wLg queue [] # => logs exchange amq.gen-vso0PVvyiRIL2WoV3i48Yg queue [] # => ...done.The interpretation of the result is straightforward: data from exchange logs goes to two queues with server-assigned names. And that's exactly what we intended.
¶Ô½á¹ûµÄ½âÊ;ͷdz£¼ò½àÃ÷ÁË£ºÀ´×Ô logs ½»»»»úµÄÊý¾Ý½«È¥ÍùÁ½¸öÓÉ·þÎñ¶Ë·ÖÅäÃû³ÆµÄ¶ÓÁУ¬¶øÕâÇ¡ºÃÊÇÎÒÃÇËùÆÚÍûµÄ¡£
posted @
¡¡