﻿
{"id":13911,"date":"2020-03-03T02:11:43","date_gmt":"2020-03-03T02:11:43","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=13911"},"modified":"2020-03-03T02:11:43","modified_gmt":"2020-03-03T02:11:43","slug":"rabbitmq-header-exchange","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/rabbitmq-header-exchange\/","title":{"rendered":"RabbitMQ &#8211; Header Exchange"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde mesaj kuyruk sistemi olan RabbitMQ&#8217;da kuyruktaki mesajlar\u0131n se\u00e7icili\u011fini artt\u0131rmak i\u00e7in kullan\u0131lan Header Exchange \u00fczerine klavye oynat\u0131yor olaca\u011f\u0131z.<\/p>\n<h3>Header Exchange \u00c7al\u0131\u015fma Mekanizmas\u0131 Nas\u0131ld\u0131r?<\/h3>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.png\" alt=\"RabbitMQ - Header Exchange\" width=\"1150\" height=\"419\" class=\"alignnone size-full wp-image-13913\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.png 1150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange-300x109.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange-1024x373.png 1024w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange-768x280.png 768w\" sizes=\"auto, (max-width: 1150px) 100vw, 1150px\" \/><\/a><br \/>\nHeader Exchange i\u00e7in bir \u00f6nceki makalemizde ele ald\u0131\u011f\u0131m\u0131z <a href=\"https:\/\/www.gencayyildiz.com\/blog\/rabbitmq-topic-exchange\/\" rel=\"noopener noreferrer\" target=\"_blank\">Topic Exchange<\/a>&#8216;in key &#8211; value olarak tan\u0131mlanan halidir diyebiliriz. Yani anlayaca\u011f\u0131n\u0131z g\u00f6nderilen mesajlar\u0131n routing key de\u011ferini topic exchange&#8217;de oldu\u011fu gibi .(nokta) ile de\u011filde, ilgili mesaj\u0131n header k\u0131sm\u0131ndan verilen key &#8211; value format\u0131nda de\u011fer ile olu\u015fturarak, bu keylerle e\u015fle\u015fen kuyruklara ileten bir exchange t\u00fcr\u00fcd\u00fcr.<\/p>\n<h3>Kullan\u0131m\u0131<\/h3>\n<h4 style=\"color:gray;\">Publisher<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            ConnectionFactory factory = new ConnectionFactory();\r\n            factory.Uri = new Uri(&quot;amqp:\/\/hkhjerrt:hcqiavAqll6-co4abXnSqUBh_hHifz-Z@hornet.rmq.cloudamqp.com\/hkhjerrt&quot;);\r\n\r\n            using (IConnection connection = factory.CreateConnection())\r\n            using (IModel channel = connection.CreateModel())\r\n            {\r\n                channel.ExchangeDeclare(&quot;headerexchange&quot;, type: ExchangeType.Headers);\r\n                for (int i = 1; i &lt;= 100; i++)\r\n                {\r\n                    byte&#x5B;] bytemessage = Encoding.UTF8.GetBytes($&quot;{i}. mesaj&quot;);\r\n\r\n                    IBasicProperties properties = channel.CreateBasicProperties();\r\n                    properties.Persistent = true;\r\n                    properties.Headers = new Dictionary&lt;string, object&gt;()\r\n                    {\r\n                        &#x5B;&quot;no&quot;] = args&#x5B;0] == &quot;1&quot; ? &quot;123456&quot; : &quot;654321&quot;\r\n                    };\r\n\r\n\r\n                    channel.BasicPublish(exchange: &quot;headerexchange&quot;, routingKey: string.Empty, basicProperties: properties, body: bytemessage);\r\n                }\r\n            }\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnek kod blo\u011funda <em><strong>16.<\/strong><\/em> sat\u0131r\u0131 incelerseniz e\u011fer g\u00f6nderilecek mesaja dair gerekli ayarlar\u0131 yapmam\u0131z\u0131 sa\u011flayan &#8220;IBasicProperties&#8221; t\u00fcr\u00fcnden nesnemiz \u00fczerinden &#8220;Headers&#8221; propertysine verilen &#8216;Dictionary&#8217; koleksiyonunda &#8216;no&#8217; keyine kar\u015f\u0131l\u0131k d\u0131\u015far\u0131dan verilen arg\u00fcman valuesuna g\u00f6re &#8216;123456&#8217; ya da &#8216;654321&#8217; de\u011ferleri girilmektedir. B\u00f6ylece &#8216;1&#8217; arg\u00fcman de\u011feri verilen publisherlar <em>{key :&#8217;no&#8217;, value :&#8217;123456&#8242;}<\/em> header de\u011ferine e\u015fde\u011fer routing key&#8217;e sahip kuyruklara mesaj iletecekken di\u011fer arg\u00fcman de\u011ferlerindeki publisherlar ise <em>{key :&#8217;no&#8217;, value :&#8217;654321&#8242;}<\/em> header de\u011ferindeki kuyruklara iletecektirler.<\/p>\n<h4 style=\"color:gray;\">Consumer<\/h4>\n<p>Di\u011fer exchange t\u00fcrlerine nazaran Header Exchange&#8217;te kuyruk manuel olu\u015fturularak haz\u0131r hale getirilmelidir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            ConnectionFactory factory = new ConnectionFactory();\r\n            factory.Uri = new Uri(&quot;amqp:\/\/hkhjerrt:hcqiavAqll6-co4abXnSqUBh_hHifz-Z@hornet.rmq.cloudamqp.com\/hkhjerrt&quot;);\r\n\r\n            using (IConnection connection = factory.CreateConnection())\r\n            using (IModel channel = connection.CreateModel())\r\n            {\r\n                channel.ExchangeDeclare(&quot;headerexchange&quot;, type: ExchangeType.Headers);\r\n\r\n                channel.QueueDeclare($&quot;kuyruk-{args&#x5B;0]}&quot;, false, false, false, null);\r\n\r\n                channel.QueueBind(queue: $&quot;kuyruk-{args&#x5B;0]}&quot;, exchange: &quot;headerexchange&quot;, routingKey: string.Empty, new Dictionary&lt;string, object&gt;\r\n                {\r\n                    &#x5B;&quot;x-match&quot;] = &quot;all&quot;,\r\n                    &#x5B;&quot;no&quot;] = args&#x5B;0] == &quot;1&quot; ? &quot;123456&quot; : &quot;654321&quot;,\r\n                });\r\n\r\n                channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);\r\n\r\n                EventingBasicConsumer consumer = new EventingBasicConsumer(channel);\r\n                channel.BasicConsume($&quot;kuyruk-{args&#x5B;0]}&quot;, false, consumer);\r\n                consumer.Received += (sender, e) =&gt;\r\n                {\r\n                    Console.WriteLine($&quot;{Encoding.UTF8.GetString(e.Body)}. mesaj&quot;);\r\n                    channel.BasicAck(e.DeliveryTag, false);\r\n                };\r\n                Console.Read();\r\n            }\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funa g\u00f6z atarsan\u0131z <strong><em>11.<\/em><\/strong> sat\u0131rda manuel bir kuyruk olu\u015fturulmakta ve <strong><em>13.<\/em><\/strong> sat\u0131rda bu kuyru\u011fa ilgili exchange ile birlikte header bilgileri set edilerek bind edilmektedir. Yine burada publisherda oldu\u011fu gibi verilen arg\u00fcman de\u011ferine g\u00f6re key &#8211; value verisindeki value k\u0131sm\u0131 belirlenecek ve e\u015fle\u015fen kuyruk hangisi ise mesajlar\u0131 o anki consumer taraf\u0131ndan t\u00fcketilecektir.<\/p>\n<p>Burada header&#8217;a verilen &#8216;x-match&#8217; key de\u011ferine dikkatinizi \u00e7ekerim. &#8216;x-match&#8217;; &#8216;all&#8217; veya &#8216;any&#8217; olmak \u00fczere iki de\u011fer alabilen ve fonksiyonel olarak header de\u011ferlerinden herhangi biriyle yahut hepsiyle e\u015fle\u015fme durumunu kontrol edip duruma g\u00f6re ilgili consumer\u0131 ilgili kuyru\u011fa subscribe eden bir niteli\u011fe sahiptir.<\/p>\n<h3>Test<\/h3>\n<p>Uygulamay\u0131 derleyip \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131m\u0131zda <a href=\"https:\/\/hornet.rmq.cloudamqp.com\/\" rel=\"noopener noreferrer\" target=\"_blank\">hornet.rmq.cloudamqp.com<\/a> adresindeki cloud servisine g\u00f6z att\u0131\u011f\u0131m\u0131zda header exchange&#8217;in olu\u015fturuldu\u011funu g\u00f6rebilmekteyiz.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.jpg\" alt=\"RabbitMQ - Header Exchange\" width=\"407\" height=\"582\" class=\"alignnone size-full wp-image-13933\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.jpg 407w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange-210x300.jpg 210w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQ-Header-Exchange.gif\" alt=\"RabbitMQ - Header Exchange\" width=\"800\" height=\"408\" class=\"alignnone size-full wp-image-13936\" \/><\/a><br \/>\nNihai olarak g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere kuyruklardaki key &#8211; value de\u011feri ile e\u015fle\u015fen consumerlar ilgili mesajlar\u0131 t\u00fcketmekte ve i\u015flemektedirler. B\u00f6ylece publisherlar hedef consumerlara mesaj\u0131 ileterek en ba\u015fta vurgulad\u0131\u011f\u0131m\u0131z se\u00e7icili\u011fi artt\u0131rm\u0131\u015f ve i\u015f b\u00f6l\u00fcm\u00fcn\u00fc net sa\u011flam\u0131\u015f bulunmaktad\u0131rlar.<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230; <\/p>\n<p>Not : \u00d6rnek projeyi indirebilmek i\u00e7in <a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/03\/RabbitMQHeaderExchangeExample.zip\">buraya<\/a> t\u0131klay\u0131n\u0131z.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde mesaj kuyruk sistemi olan RabbitMQ&#8217;da kuyruktaki mesajlar\u0131n se\u00e7icili\u011fini artt\u0131rmak i\u00e7in kullan\u0131lan Header Exchange \u00fczerine klavye oynat\u0131yor olaca\u011f\u0131z. Header Exchange \u00c7al\u0131\u015fma Mekanizmas\u0131 Nas\u0131ld\u0131r? Header Exchange i\u00e7in bir \u00f6nceki makalemizde ele ald\u0131\u011f\u0131m\u0131z Topic&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":13913,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3156],"tags":[3230,3158,3231],"class_list":["post-13911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-rabbitmq","tag-header-exchange","tag-rabbitmq","tag-rabbitmq-header-exchange"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/13911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=13911"}],"version-history":[{"count":27,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/13911\/revisions"}],"predecessor-version":[{"id":13942,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/13911\/revisions\/13942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/13913"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=13911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=13911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=13911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}