{"id":4467,"date":"2016-11-20T09:10:40","date_gmt":"2016-11-20T09:10:40","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4467"},"modified":"2016-11-20T09:10:40","modified_gmt":"2016-11-20T09:10:40","slug":"rabbitmq-in-docker","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/rabbitmq-in-docker\/","title":{"rendered":"RabbitMQ in Docker"},"content":{"rendered":"<p>As part of a series of posts on running some core types of applications in Docker, its time to run up a message queue. Let&#8217;s try the Docker RabbitMQ container.<\/p>\n<p>To run, simply use the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndocker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3\r\n<\/pre>\n<p>If you&#8217;d like to use the RabbitMQ management tool, then run this instead (which runs RabbitMQ and the management tool)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndocker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management\r\n<\/pre>\n<p><em>Note: default login for the management UI is via a web browser using http:\/\/host:15672 obviously replacing the host with your server IP address. The default login and password are guest\/guest.<\/em><\/p>\n<p>From Visual Studio, create two test applications, one for sending messages and the second for receiving messages (mine are both console application) and using NuGet, add RabbitMQ.Client to them both. I&#8217;m going to just duplicate the code from the <a href=\"https:\/\/www.rabbitmq.com\/tutorials\/tutorial-one-dotnet.html\" target=\"_blank\">RabbitMQ tutorial<\/a> here. So the application for sending messages should look like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstatic void Main(string&#x5B;] args)\r\n{\r\n   var factory = new ConnectionFactory\r\n   {\r\n      HostName = &quot;localhost&quot;,\r\n      Port = AmqpTcpEndpoint.UseDefaultPort\r\n   };\r\n   using (var connection = factory.CreateConnection())\r\n   {\r\n      using (var channel = connection.CreateModel())\r\n      {\r\n         channel.QueueDeclare(&quot;hello&quot;, false, false, false, null);\r\n\r\n         var message = &quot;Hello World!&quot;;\r\n         var body = Encoding.UTF8.GetBytes(message);\r\n\r\n         channel.BasicPublish(String.Empty, &quot;hello&quot;, null,body);\r\n         Console.WriteLine(&quot; &#x5B;x] Sent {0}&quot;, message);\r\n      }\r\n   }\r\n   Console.ReadLine();\r\n}\r\n<\/pre>\n<p>The receiver code looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstatic void Main(string&#x5B;] args)\r\n{\r\n   var factory = new ConnectionFactory\r\n   {\r\n      HostName = &quot;localhost&quot;,\r\n      Port = AmqpTcpEndpoint.UseDefaultPort\r\n   };\r\n   using (var connection = factory.CreateConnection())\r\n   {\r\n      using (var channel = connection.CreateModel())\r\n      {\r\n         channel.QueueDeclare(&quot;hello&quot;, false, false, false,null);\r\n\r\n         var consumer = new EventingBasicConsumer(channel);\r\n         consumer.Received += (model, ea) =&gt;\r\n         {\r\n            var body = ea.Body;\r\n            var message = Encoding.UTF8.GetString(body);\r\n            Console.WriteLine(&quot; &#x5B;x] Received {0}&quot;, message);\r\n         };\r\n         channel.BasicConsume(&quot;hello&quot;, true, consumer);\r\n\r\n         Console.WriteLine(&quot;Press &#x5B;enter] to exit.&quot;);\r\n         Console.ReadLine();\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p>Obviously localhost should be replaced with the host name\/ip address of the Docker server running RabbitMQ.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As part of a series of posts on running some core types of applications in Docker, its time to run up a message queue. Let&#8217;s try the Docker RabbitMQ container. To run, simply use the following docker run -d &#8211;hostname my-rabbit &#8211;name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3 If you&#8217;d like to use the RabbitMQ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[102],"tags":[],"class_list":["post-4467","post","type-post","status-publish","format-standard","hentry","category-docker"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4467"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4467\/revisions"}],"predecessor-version":[{"id":4472,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4467\/revisions\/4472"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}