{"id":1595,"date":"2022-01-24T14:56:10","date_gmt":"2022-01-24T06:56:10","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1595"},"modified":"2022-01-24T16:21:07","modified_gmt":"2022-01-24T08:21:07","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8centos7%e5%ae%89%e8%a3%85rabbitmq%e7%9a%84php%e6%89%a9%e5%b1%95","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1595","title":{"rendered":"\u5982\u4f55\u5728Centos7\u5b89\u88c5rabbitmq\u7684PHP\u6269\u5c55"},"content":{"rendered":"<p>1.\u5148\u5b89\u88c5rabbitmq-c<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">wget https:\/\/github.com\/alanxz\/rabbitmq-c\/releases\/download\/v0.8.0\/rabbitmq-c-0.8.0.tar.gz\r\n \r\ntar -zxvf rabbitmq-c-0.8.0.tar.gz\r\n \r\ncd rabbitmq-c-0.8.0\r\n \r\n.\/configure --prefix=\/usr\/local\/rabbitmq-c\r\n \r\nmake &amp;&amp; make install<\/pre>\n<p>2.\u63a5\u4e0b\u6765\u5b89\u88c5amqp\u7684php\u6269\u5c55<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">wget https:\/\/pecl.php.net\/get\/amqp-1.11.0.tgz\r\n\r\ntar -zxvf amqp-1.11.0.tgz\r\n\r\ncd amqp-1.11.0\r\n \r\nphpize\r\n \r\n.\/configure --with-php-config=\/www\/server\/php\/74\/bin\/php-config --with-librabbitmq-dir=\/usr\/local\/rabbitmq-c\r\n \r\nmake &amp;&amp; make install<\/pre>\n<p>3.\u91cd\u65b0php-fpm\u670d\u52a1\uff0c\u67e5\u770bphp\u5bf9\u5e94\u7248\u672c\u4e2d\u662f\u5426AMQP\u6269\u5c55<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">systemctl\u00a0 restart php-fpm<\/pre>\n<p><strong>MQ\u4e00\u4e9b\u57fa\u672c\u7684\u6982\u5ff5:<\/strong><\/p>\n<ul>\n<li>\u6d88\u606f\u751f\u4ea7\u8005\uff1a\u5c31\u662f\u63d0\u4f9b\u6d88\u606f\u7684\u4e00\u65b9\uff0c\u8fd9\u4e00\u65b9\u4e3b\u8981\u662f\u9762\u5411\u4e1a\u52a1\u6216\u8005\u7528\u6237\u7684\u4e00\u65b9\u3002<\/li>\n<li>\u6d88\u606f\u6d88\u8d39\u8005\uff1a\u5c31\u662f\u63a5\u6536\u6d88\u606f\u5e76\u8fdb\u884c\u5904\u7406\u7684\u4e00\u65b9\uff0c\u8fd9\u4e00\u65b9\u4e00\u822c\u90fd\u662f\u5728\u540e\u7aef\u8f6e\u8be2\u7684\u8fd0\u884c\u7740\uff0c\u65f6\u523b\u51c6\u5907\u63a5\u6536\u65b0\u7684\u6d88\u606f\u5e76\u5904\u7406\u3002<\/li>\n<li>\u4fe1\u9053\uff08Channel\uff09:\u7528\u4e00\u4e2a\u5f62\u8c61\u7684\u6bd4\u55bb\u5c31\u662f\uff0c\u6709\u4e86\u5149\u7ea4\uff0c\u6211\u4eec\u80fd\u591f\u5728\u5149\u7ea4\u4e0a\u8fdb\u884c\u6253\u5ea7\u673a\u7535\u8bdd\u3001\u770b\u7535\u89c6\u3001\u4e0a\u7f51\u7b49\u7b49\uff0c\u4e00\u4e2a\u94fe\u63a5\u80fd\u591f\u63d0\u4f9b\u591a\u79cd\u65b9\u5f0f\u7684\u901a\u8baf\u3002<\/li>\n<li>\u6d88\u606f\u961f\u5217(Message Queue)\uff1a\u5c31\u662fRabbitMQ\u5bf9\u6d88\u606f\u751f\u4ea7\u8005\u751f\u4ea7\u7684\u6d88\u606f\u8fdb\u884c\u4e00\u5b9a\u7684\u6392\u961f\u4ece\u800c\u5f62\u6210\u961f\u5217\u4ee5\u4f9b\u6d88\u8d39\u8005\u5904\u7406\u3002<\/li>\n<li>\u6d88\u606f\u961f\u5217\u521b\u5efa\u8005\uff1a\u6d88\u606f\u961f\u5217\u662f\u7531\u6d88\u8d39\u8005\u7533\u8bf7\u521b\u5efa\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u6d88\u8d39\u8005\u77e5\u9053\u600e\u4e48\u5904\u7406\u6d88\u606f\uff0c\u800c\u4e0d\u662f\u6d88\u606f\u751f\u4ea7\u8005\u521b\u5efa\u7684\u3002<\/li>\n<li>\u6d88\u606f\u8def\u7531\u8f6c\u53d1\uff1a\u4e00\u4e2a\u6d88\u8d39\u8005\u53ef\u80fd\u4f1a\u7533\u8bf7\u521b\u5efa\u591a\u4e2a\u6d88\u606f\u961f\u5217\uff0c\u90a3\u4e48\u751f\u4ea7\u8005\u7684\u6d88\u606f\u5e94\u8be5\u4f20\u9012\u5230\u90a3\u4e2a\u961f\u5217\u5462\uff1f\u8fd9\u4e3b\u8981\u7531Exchange\u6765\u8fdb\u884c\u6307\u5b9a\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1605\" src=\"https:\/\/qiniu.shijitianchen.com\/2022\/01\/20220124082108959.jpg\" width=\"443\" height=\"161\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>PHP\u7248Demo:<\/strong><\/p>\n<p><strong>\u6d88\u8d39\u8005\u7aef: mq_server.php<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function mq_server(){\r\n    set_time_limit(0);\r\n    \/\/1.\u521b\u5efaAMQP\u5bf9\u8c61\r\n    $connnection = new \\AMQPConnection(array(\r\n        'host'      =&gt;  'localhost',\r\n        'posrt'     =&gt;  5672,\r\n        'virtual'   =&gt;  '\/',\r\n        'login'     =&gt;  \"\",\r\n        'password'  =&gt;  \"\"\r\n    ));\r\n    $connnection-&gt;connect();\r\n \r\n    \/\/2.\u521b\u5efa\u4fe1\u9053channel\uff0c\u4fe1\u9053\u5c31\u50cf\u5149\u7ea4\uff0c\u7c7b\u4f3c\u4e0a\u9762\u53ef\u4ee5\u7ed1\u5b9a\u4e2a\u7ec4\u4ef6\r\n    $channelObj = new \\AMQPChannel($connnection);\r\n \r\n    \/\/3.\u521b\u5efa\u6d88\u606f\u8def\u7531\u8f6c\u53d1\r\n    $exchangeObj = new \\AMQPExchange($channelObj);\r\n    $exchangeObj-&gt;setName('first');\r\n    $exchangeObj-&gt;setType(AMQP_EX_TYPE_DIRECT);\r\n \r\n    \/\/4.\u521b\u5efa\u6d88\u606f\u961f\u5217\r\n    $queueObj = new \\AMQPQueue($channelObj);\r\n    $queueObj-&gt;setName('firstqueue');\r\n \r\n    \/\/5.\u6d88\u606f\u961f\u5217\u7ed1\u5b9aexchange\u4ea4\u6362\u8def\u7531\r\n    $queueObj-&gt;bind('first','queue_route');\r\n    while(true){\r\n        $queueObj-&gt;consume(function (\\AMQPEnvelope $envelope,\\AMQPQueue $queue){\r\n                $msg = $envelope-&gt;getBody();\r\n                echo $msg;\r\n                $queue-&gt;ack($envelope-&gt;getDeliveryTag());\r\n        });\r\n    }\r\n}\r\n \r\nmq_server();<\/pre>\n<p><strong>\u751f\u4ea7\u8005\u7aef\uff1aclient.php<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function mq_client(){\r\n    $connection = new \\AMQPConnection(array(\r\n        \"host\"          =&gt;  \"localhost\",\r\n        \"port\"          =&gt;  5672,\r\n        \"virtual\"       =&gt;  \"\/\",\r\n        \"login\"         =&gt;  \"\",\r\n        \"password\"  =&gt;  \"\"\r\n    ));\r\n    $connection-&gt;connect();\r\n \r\n    \/\/\u521b\u5efa\u4fe1\u9053channel\r\n    $channelObj     = new \\AMQPChannel($connection);\r\n    $exchangeObj    = new \\AMQPExchange($channelObj);\r\n    \/\/\u4fe1\u9053channel\r\n    $exchangeObj-&gt;setName(\"first\");\r\n    $exchangeObj-&gt;setType(AMQP_EX_TYPE_DIRECT);\r\n \r\n    $messageBodoy = \"welcome to RabbitMQ\";\r\n \r\n    $exchangeObj-&gt;publish($messageBodoy,'queue_route');\r\n \r\n    $connection-&gt;disconnect();\r\n}\r\n \r\nmq_client();<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5148\u5b89\u88c5rabbitmq-c wget https:\/\/github.com\/alanxz\/rabbitmq [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1596,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2],"tags":[102],"class_list":["post-1595","post","type-post","status-publish","format-standard","hentry","category-linux","category-php","tag-rabbitmq"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1595"}],"version-history":[{"count":6,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1595\/revisions"}],"predecessor-version":[{"id":1606,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1595\/revisions\/1606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/media\/1596"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}