{"id":38437,"date":"2025-05-20T13:02:34","date_gmt":"2025-05-20T05:02:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38437.html"},"modified":"2025-05-20T13:02:34","modified_gmt":"2025-05-20T05:02:34","slug":"rabbitmq-c%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38437.html","title":{"rendered":"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528"},"content":{"rendered":"<h2>\u4ecb\u7ecd<\/h2>\n<p>RabbitMQ \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6d88\u606f\u4ee3\u7406\u548c\u961f\u5217\u670d\u52a1\u5668&#xff0c;\u7528\u4e8e\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u3002\u5b83\u5b9e\u73b0\u4e86\u9ad8\u7ea7\u6d88\u606f\u961f\u5217\u534f\u8bae(AMQP)&#xff0c;\u540c\u65f6\u4e5f\u652f\u6301\u5176\u4ed6\u534f\u8bae\u5982 STOMP\u3001MQTT \u7b49\u3002<\/p>\n<h3>\u6838\u5fc3\u6982\u5ff5<\/h3>\n<li>\n<p>Producer(\u751f\u4ea7\u8005): \u53d1\u9001\u6d88\u606f\u7684\u5e94\u7528\u7a0b\u5e8f<\/p>\n<\/li>\n<li>\n<p>Consumer(\u6d88\u8d39\u8005): \u63a5\u6536\u6d88\u606f\u7684\u5e94\u7528\u7a0b\u5e8f<\/p>\n<\/li>\n<li>\n<p>Queue(\u961f\u5217): \u5b58\u50a8\u6d88\u606f\u7684\u7f13\u51b2\u533a<\/p>\n<\/li>\n<li>\n<p>Exchange(\u4ea4\u6362\u673a): \u63a5\u6536\u751f\u4ea7\u8005\u53d1\u9001\u7684\u6d88\u606f\u5e76\u6839\u636e\u89c4\u5219\u8def\u7531\u5230\u961f\u5217<\/p>\n<\/li>\n<li>\n<p>Binding(\u7ed1\u5b9a): \u8fde\u63a5\u4ea4\u6362\u673a\u548c\u961f\u5217\u7684\u89c4\u5219<\/p>\n<\/li>\n<li>\n<p>Message(\u6d88\u606f): \u5305\u542b\u6709\u6548\u8f7d\u8377(payload)\u548c\u6807\u7b7e(label)\u7684\u6570\u636e<\/p>\n<\/li>\n<h2>\u5b89\u88c5<\/h2>\n<h3><span style=\"color:#000000\">\u5b89\u88c5 <\/span><span style=\"color:#000000\">RabbitMQ <\/span><\/h3>\n<p> sudo apt install rabbitmq-server <\/p>\n<p> <span style=\"color:#000000\">RabbitMQ <\/span><br \/>\n <span style=\"color:#000000\">\u7684\u7b80\u5355\u4f7f\u7528 <\/span><br \/>\n # \u542f\u52a8\u670d\u52a1<br \/>\nsudo systemctl start rabbitmq-server.service<br \/>\n# \u67e5\u770b\u670d\u52a1\u72b6\u6001<br \/>\nsudo systemctl status rabbitmq-server.service<br \/>\n# \u5b89\u88c5\u5b8c\u6210\u7684\u65f6\u5019\u9ed8\u8ba4\u6709\u4e2a\u7528\u6237 guest &#xff0c;\u4f46\u662f\u6743\u9650\u4e0d\u591f&#xff0c;\u8981\u521b\u5efa\u4e00\u4e2a<br \/>\nadministrator \u7528\u6237&#xff0c;\u624d\u53ef\u4ee5\u505a\u4e3a\u8fdc\u7a0b\u767b\u5f55\u548c\u53d1\u8868\u8ba2\u9605\u6d88\u606f&#xff1a;<br \/>\n#\u6dfb\u52a0\u7528\u6237<br \/>\nsudo rabbitmqctl add_user root 123456<br \/>\n#\u8bbe\u7f6e\u7528\u6237 tag<br \/>\nsudo rabbitmqctl set_user_tags root administrator<br \/>\n#\u8bbe\u7f6e\u7528\u6237\u6743\u9650<br \/>\nsudo rabbitmqctl set_permissions -p \/ root &#034;.&#034; &#034;.&#034; &#034;.*&#034;<br \/>\n# RabbitMQ \u81ea\u5e26\u4e86 web \u7ba1\u7406\u754c\u9762,\u6267\u884c\u4e0b\u9762\u547d\u4ee4\u5f00\u542f<br \/>\nsudo rabbitmq-plugins enable rabbitmq_management <\/p>\n<p>  <span style=\"color:#000000\">\u8bbf\u95ee <\/span><br \/>\n  <span style=\"color:#000000\">webUI <\/span><br \/>\n  <span style=\"color:#000000\">\u754c\u9762\u7684\u9ed8\u8ba4\u7aef\u53e3\u4e3a <\/span><br \/>\n  <span style=\"color:#000000\">15672\u3002<\/span> <\/p>\n<h3><span style=\"color:#000000\">\u5b89\u88c5 <\/span><span style=\"color:#000000\">RabbitMQ <\/span><span style=\"color:#000000\">\u7684 <\/span><span style=\"color:#000000\">C&#043;&#043;<\/span><span style=\"color:#000000\">\u5ba2\u6237\u7aef\u5e93 <\/span><\/h3>\n<ul>\n<li><span style=\"color:#000000\">C <\/span><span style=\"color:#000000\">\u8bed\u8a00\u5e93&#xff1a;<\/span>https:\/\/github.com\/alanxz\/rabbitmq-c<\/li>\n<li><span style=\"color:#000000\">C&#043;&#043;<\/span><span style=\"color:#000000\">\u5e93&#xff1a;https:\/\/github.com\/CopernicaMarketingSoftware\/AMQP-CPP\/tree\/master<\/span><\/li>\n<\/ul>\n<p>    AMQP \u662f\u4e00\u4e2a\u5f00\u653e\u6807\u51c6\u7684\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u4e13\u4e3a\u9762\u5411\u6d88\u606f\u7684\u4e2d\u95f4\u4ef6\u8bbe\u8ba1&#xff0c;RabbitMQ \u662f\u5176\u6700\u8457\u540d\u7684\u5b9e\u73b0\u4e4b\u4e00\u3002<br \/>\n   <span style=\"color:#000000\">\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528 <\/span><br \/>\n   <span style=\"color:#000000\">AMQP-CPP <\/span><br \/>\n   <span style=\"color:#000000\">\u5e93\u6765\u7f16\u5199\u5ba2\u6237\u7aef\u7a0b\u5e8f\u3002<\/span> <\/p>\n<p>    \u00a0<br \/>\n   sudo apt install libev-dev #libev \u7f51\u7edc\u5e93\u7ec4\u4ef6<br \/>\ngit clone https:\/\/github.com\/CopernicaMarketingSoftware\/AMQP-CPP.git<br \/>\ncd AMQP-CPP\/<br \/>\nmake<br \/>\nmake install <\/p>\n<p>    <span style=\"color:#000000\">\u5b89\u88c5\u62a5\u9519&#xff1a; <\/span> <\/p>\n<p>    \/usr\/include\/openssl\/macros.h:147:4: error: #error<br \/>\n&#034;OPENSSL_API_COMPAT expresses an impossible API compatibility level&#034;<br \/>\n147 | # error &#034;OPENSSL_API_COMPAT expresses an impossible API<br \/>\ncompatibility level&#034; | ^~~~~<br \/>\nIn file included from \/usr\/include\/openssl\/ssl.h:18,<br \/>\nfrom linux_tcp\/openssl.h:20,<br \/>\nfrom linux_tcp\/openssl.cpp:12:<br \/>\n\/usr\/include\/openssl\/bio.h:687:1: error: expected constructor,<br \/>\ndestructor, or type conversion before \u2018DEPRECATEDIN_1_1_0\u2019<br \/>\n687 | DEPRECATEDIN_1_1_0(int BIO_get_port(const char *str, unsigned short *port_ptr)) <\/p>\n<p>    <span style=\"color:#000000\">\u8fd9\u79cd\u9519\u8bef&#xff0c;\u8868\u793a <\/span><br \/>\n    <span style=\"color:#000000\">ssl <\/span><br \/>\n    <span style=\"color:#000000\">\u7248\u672c\u51fa\u73b0\u95ee\u9898\u3002 <\/span> <\/p>\n<p>    <span style=\"color:#000000\">\u89e3\u51b3\u65b9\u6848&#xff1a;\u5378\u8f7d\u5f53\u524d\u7684 <\/span><br \/>\n    <span style=\"color:#000000\">ssl <\/span><br \/>\n    <span style=\"color:#000000\">\u5e93&#xff0c;\u91cd\u65b0\u8fdb\u884c\u4fee\u590d\u5b89\u88c5<\/span> <\/p>\n<p>    sudo dpkg -P &#8211;force-all libevent-openssl-2.1-7<br \/>\nsudo dpkg -P &#8211;force-all openssl<br \/>\nsudo dpkg -P &#8211;force-all libssl-dev<br \/>\nsudo apt &#8211;fix-broken install <\/p>\n<p>     <span style=\"color:#000000\">\u4fee\u590d\u540e&#xff0c;\u91cd\u65b0\u8fdb\u884c <\/span><br \/>\n     <span style=\"color:#000000\">make\u3002<\/span> <\/p>\n<h2><span style=\"color:#000000\">AMQP-CPP <\/span><span style=\"color:#000000\">\u5e93\u7684\u7b80\u5355\u4f7f\u7528 <\/span><\/h2>\n<h3><span style=\"color:#000000\">\u4ecb\u7ecd <\/span><\/h3>\n<ul>\n<li><span style=\"color:#000000\">AMQP-CPP <\/span><span style=\"color:#000000\">\u662f\u7528\u4e8e\u4e0e <\/span><span style=\"color:#000000\">RabbitMq <\/span><span style=\"color:#000000\">\u6d88\u606f\u4e2d\u95f4\u4ef6\u901a\u4fe1\u7684 <\/span><span style=\"color:#000000\">c&#043;&#043;<\/span><span style=\"color:#000000\">\u5e93\u3002\u5b83\u80fd\u89e3\u6790\u4ece <\/span><span style=\"color:#000000\">RabbitMq \u670d\u52a1\u53d1\u9001\u6765\u7684\u6570\u636e&#xff0c;\u4e5f\u53ef\u4ee5\u751f\u6210\u53d1\u5411 RabbitMq <\/span><span style=\"color:#000000\">\u7684\u6570\u636e\u5305\u3002<\/span><span style=\"color:#000000\">AMQP-CPP <\/span><span style=\"color:#000000\">\u5e93\u4e0d\u4f1a\u5411 RabbitMq \u5efa\u7acb\u7f51\u7edc\u8fde\u63a5&#xff0c;\u6240\u6709\u7684\u7f51\u7edc <\/span><span style=\"color:#000000\">io <\/span><span style=\"color:#000000\">\u7531\u7528\u6237\u5b8c\u6210\u3002 <\/span><\/li>\n<li><span style=\"color:#000000\">\u5f53\u7136&#xff0c;AMQP-CPP \u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u7f51\u7edc\u5c42\u63a5\u53e3&#xff0c;\u5b83\u9884\u5b9a\u4e49\u4e86 <\/span><span style=\"color:#000000\">TCP <\/span><span style=\"color:#000000\">\u6a21\u5757&#xff0c;\u7528\u6237\u5c31\u4e0d\u7528\u81ea\u5df1\u5b9e\u73b0\u7f51\u7edc io<\/span><span style=\"color:#000000\">&#xff0c;\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u62e9 <\/span><span style=\"color:#000000\">libevent<\/span><span style=\"color:#000000\">\u3001<\/span><span style=\"color:#000000\">libev<\/span><span style=\"color:#000000\">\u3001<\/span><span style=\"color:#000000\">libuv<\/span><span style=\"color:#000000\">\u3001<\/span><span style=\"color:#000000\">asio <\/span><span style=\"color:#000000\">\u7b49\u5f02\u6b65\u901a\u4fe1\u7ec4\u4ef6&#xff0c;\u9700\u8981\u624b\u52a8\u5b89\u88c5\u5bf9\u5e94\u7684\u7ec4\u4ef6\u3002 <\/span><\/li>\n<li><span style=\"color:#000000\">AMQP-CPP <\/span><span style=\"color:#000000\">\u5b8c\u5168\u5f02\u6b65&#xff0c;\u6ca1\u6709\u963b\u585e\u5f0f\u7684\u7cfb\u7edf\u8c03\u7528&#xff0c;\u4e0d\u4f7f\u7528\u7ebf\u7a0b\u5c31\u80fd\u591f\u5e94\u7528\u5728\u9ad8\u6027\u80fd\u5e94\u7528\u4e2d\u3002 <\/span><\/li>\n<li><span style=\"color:#000000\">\u6ce8\u610f&#xff1a;\u5b83\u9700\u8981 c&#043;&#043;17 <\/span><span style=\"color:#000000\">\u7684\u652f\u6301\u3002 <\/span><\/li>\n<\/ul>\n<h3><span style=\"color:#000000\">\u4f7f\u7528 <\/span><\/h3>\n<p>      <span style=\"color:#000000\">AMQP-CPP <\/span><br \/>\n      <span style=\"color:#000000\">\u7684\u4f7f\u7528\u6709\u4e24\u79cd\u6a21\u5f0f&#xff1a; <\/span> <\/p>\n<ul>\n<li><span style=\"color:#000000\">\u4f7f\u7528\u9ed8\u8ba4\u7684 TCP <\/span><span style=\"color:#000000\">\u6a21\u5757\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1 <\/span><\/li>\n<li><span style=\"color:#000000\">\u4f7f\u7528\u6269\u5c55\u7684 libevent<\/span><span style=\"color:#000000\">\u3001<\/span><span style=\"color:#000000\">libev<\/span><span style=\"color:#000000\">\u3001<\/span><span style=\"color:#000000\">libuv<\/span><span style=\"color:#000000\">\u3001<\/span><span style=\"color:#000000\">asio <\/span><span style=\"color:#000000\">\u5f02\u6b65\u901a\u4fe1\u7ec4\u4ef6\u8fdb\u884c\u901a\u4fe1 <\/span><\/li>\n<\/ul>\n<h4><span style=\"color:#000000\">TCP <\/span><span style=\"color:#000000\">\u6a21\u5f0f <\/span><\/h4>\n<p>      <span style=\"color:#000000\">\u8be5\u6a21\u5f0f\u4e0b\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u7c7b\u7ee7\u627f\u81ea AMQP::TcpHandler <\/span><br \/>\n      <span style=\"color:#000000\">\u7c7b&#xff0c; \u5b83\u8d1f\u8d23\u7f51\u7edc\u5c42\u7684 <\/span><br \/>\n      <span style=\"color:#000000\">TCP <\/span><br \/>\n      <span style=\"color:#000000\">\u8fde\u63a5&#xff0c;\u91cd\u5199\u76f8\u5173\u51fd\u6570&#xff0c; \u5176\u4e2d\u5fc5\u987b\u91cd\u5199 monitor \u51fd\u6570\u3002\u5728 monitor \u51fd\u6570\u4e2d\u9700\u8981\u5b9e\u73b0\u7684\u662f\u5c06 <\/span><br \/>\n      <span style=\"color:#000000\">fd <\/span><br \/>\n      <span style=\"color:#000000\">\u653e\u5165 <\/span><br \/>\n      <span style=\"color:#000000\">eventloop(select<\/span><br \/>\n      <span style=\"color:#000000\">\u3001<\/span><br \/>\n      <span style=\"color:#000000\">epoll)<\/span><br \/>\n      <span style=\"color:#000000\">\u4e2d\u76d1\u63a7&#xff0c; \u5f53 <\/span><br \/>\n      <span style=\"color:#000000\">fd \u53ef\u5199\u53ef\u8bfb\u5c31\u7eea\u4e4b\u540e&#xff0c; \u8c03\u7528 AMQP-CPP <\/span><br \/>\n      <span style=\"color:#000000\">\u7684 <\/span><br \/>\n      <span style=\"color:#000000\">connection-&gt;process(fd, flags)<\/span><br \/>\n      <span style=\"color:#000000\">\u65b9\u6cd5<\/span> <\/p>\n<p>TCP \u6a21\u5f0f\u4f7f\u7528\u8f83\u4e3a\u9ebb\u70e6&#xff0c;\u4e0d\u8fc7\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u7f51\u7edc\u5c42\u96c6\u6210\u80fd\u529b&#xff0c;\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7f51\u7edc\u5e93\u8fdb\u884c\u96c6\u6210\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u5efa\u8bae\u7ed3\u5408\u4e8b\u4ef6\u5faa\u73af\u5e93&#xff08;\u5982libuv\u3001Boost.Asio\u7b49&#xff09;\u4f7f\u7528\u4ee5\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u3002<\/p>\n<h4><span style=\"color:#000000\">\u6269\u5c55\u6a21\u5f0f <\/span><\/h4>\n<p>       <span style=\"color:#000000\">\u4ee5 <\/span><br \/>\n       <span style=\"color:#000000\">libev <\/span><br \/>\n       <span style=\"color:#000000\">\u4e3a\u4f8b&#xff0c; \u6211\u4eec\u4e0d\u5fc5\u8981\u81ea\u5df1\u5b9e\u73b0 monitor \u51fd\u6570&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 AMQP::LibEvHandler\u3002<\/span> <\/p>\n<h3><span style=\"color:#000000\">\u5e38\u7528\u7c7b\u4e0e\u63a5\u53e3\u4ecb\u7ecd <\/span><\/h3>\n<h4 style=\"background-color:transparent\"><span style=\"color:#000000\">Channel <\/span><\/h4>\n<p>        <span style=\"color:#000000\">channel <\/span><br \/>\n        <span style=\"color:#000000\">\u662f\u4e00\u4e2a\u865a\u62df\u8fde\u63a5&#xff0c;\u4e00\u4e2a\u8fde\u63a5\u4e0a\u53ef\u4ee5\u5efa\u7acb\u591a\u4e2a\u901a\u9053\u3002\u5e76\u4e14\u6240\u6709\u7684 <\/span><br \/>\n        <span style=\"color:#000000\">RabbitMq <\/span><br \/>\n        <span style=\"color:#000000\">\u6307\u4ee4\u90fd\u662f\u901a\u8fc7 channel <\/span><br \/>\n        <span style=\"color:#000000\">\u4f20\u8f93&#xff0c;\u6240\u4ee5\u8fde\u63a5\u5efa\u7acb\u540e\u7684\u7b2c\u4e00\u6b65&#xff0c;\u5c31\u662f\u5efa\u7acb <\/span><br \/>\n        <span style=\"color:#000000\">channel<\/span><br \/>\n        <span style=\"color:#000000\">\u3002\u56e0\u4e3a\u6240\u6709\u64cd\u4f5c\u662f\u5f02\u6b65\u7684&#xff0c;\u6240\u4ee5\u5728 channel <\/span><br \/>\n        <span style=\"color:#000000\">\u4e0a\u6267\u884c\u6307\u4ee4\u7684\u8fd4\u56de\u503c\u5e76\u4e0d\u80fd\u4f5c\u4e3a\u64cd\u4f5c\u6267\u884c\u7ed3\u679c&#xff0c;\u5b9e\u9645\u4e0a\u5b83\u8fd4\u56de\u7684\u662f Deferred <\/span><br \/>\n        <span style=\"color:#000000\">\u7c7b&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u5b83\u5b89\u88c5\u5904\u7406\u51fd\u6570\u3002<\/span> <\/p>\n<p>        namespace AMQP<br \/>\n{<br \/>\n    using SuccessCallback &#061; std::function&lt;void()&gt;;<br \/>\n    using ErrorCallback &#061; std::function&lt;void(const char *message)&gt;;<br \/>\n    using FinalizeCallback &#061; std::function&lt;void()&gt;;<\/p>\n<p>    using QueueCallback &#061; std::function&lt;void(const std::string &amp;name,<br \/>\n                                             uint32_t messagecount, uint32_t consumercount)&gt;;<br \/>\n    using DeleteCallback &#061; std::function&lt;void(uint32_t deletedmessages)&gt;;<\/p>\n<p>    using MessageCallback &#061; std::function&lt;void(const Message &amp;message,<br \/>\n                                               uint64_t deliveryTag, bool redelivered)&gt;;<br \/>\n    \/\/ \u5f53\u4f7f\u7528\u53d1\u5e03\u8005\u786e\u8ba4\u65f6&#xff0c;\u5f53\u670d\u52a1\u5668\u786e\u8ba4\u6d88\u606f\u5df2\u88ab\u63a5\u6536\u548c\u5904\u7406\u65f6&#xff0c;\u5c06\u8c03\u7528AckCallback<br \/>\n    using AckCallback &#061; std::function&lt;void(uint64_t deliveryTag, bool multiple)&gt;;<br \/>\n    \/\/ \u4f7f\u7528\u786e\u8ba4\u5305\u88f9\u901a\u9053\u65f6&#xff0c;\u5f53\u6d88\u606f\u88ab ack\/nacked \u65f6&#xff0c;\u4f1a\u8c03\u7528\u8fd9\u4e9b\u56de\u8c03<br \/>\n    using PublishAckCallback &#061; std::function&lt;void()&gt;;<br \/>\n    using PublishNackCallback &#061; std::function&lt;void()&gt;;<br \/>\n    using PublishLostCallback &#061; std::function&lt;void()&gt;;<br \/>\n    class Channel<br \/>\n    {<br \/>\n        Channel(Connection *connection);<br \/>\n        bool connected();<\/p>\n<p>        \/\/ \u58f0\u660e\u4ea4\u6362\u673a,\u5982\u679c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7a7a\u540d\u79f0&#xff0c;\u5219\u670d\u52a1\u5668\u5c06\u5206\u914d\u4e00\u4e2a\u540d\u79f0\u3002<br \/>\n        \/\/ &#064;param name \u4ea4\u6362\u673a\u7684\u540d\u79f0<br \/>\n        \/\/ &#064;param type \u4ea4\u6362\u7c7b\u578b<br \/>\n        \/\/     enum ExchangeType<br \/>\n        \/\/     {<br \/>\n        \/\/         fanout, \u5e7f\u64ad\u4ea4\u6362&#xff0c;\u7ed1\u5b9a\u7684\u961f\u5217\u90fd\u80fd\u62ff\u5230\u6d88\u606f<br \/>\n        \/\/         direct, \u76f4\u63a5\u4ea4\u6362&#xff0c;\u53ea\u5c06\u6d88\u606f\u4ea4\u7ed9 routingkey \u4e00\u81f4\u7684\u961f\u5217<br \/>\n        \/\/         topic, \u4e3b\u9898\u4ea4\u6362&#xff0c;\u5c06\u6d88\u606f\u4ea4\u7ed9\u7b26\u5408 bindingkey \u89c4\u5219\u7684\u961f\u5217<br \/>\n        \/\/         headers,<br \/>\n        \/\/         consistent_hash,<br \/>\n        \/\/         message_deduplication<br \/>\n        \/\/     };<br \/>\n        \/\/ &#064;param flags \u4ea4\u6362\u673a\u6807\u5fd7<br \/>\n        \/\/     \u4ee5\u4e0b flags \u53ef\u7528\u4e8e\u4ea4\u6362\u673a&#xff1a;<br \/>\n        \/\/     *-durable \u6301\u4e45\u5316&#xff0c;\u91cd\u542f\u540e\u4ea4\u6362\u673a\u4f9d\u7136\u6709\u6548<br \/>\n        \/\/     *-autodelete \u5220\u9664\u6240\u6709\u8fde\u63a5\u7684\u961f\u5217\u540e&#xff0c;\u81ea\u52a8\u5220\u9664\u4ea4\u6362<br \/>\n        \/\/     *-passive \u4ec5\u88ab\u52a8\u68c0\u67e5\u4ea4\u6362\u673a\u662f\u5426\u5b58\u5728<br \/>\n        \/\/     *-internal \u521b\u5efa\u5185\u90e8\u4ea4\u6362<br \/>\n        \/\/ &#064;param arguments \u5176\u4ed6\u53c2\u6570<\/p>\n<p>        Deferred &amp;declareExchange(const std::string_view &amp;name,<br \/>\n                                  ExchangeType type, int flags, const Table &amp;arguments);<\/p>\n<p>        \/\/ \u58f0\u660e\u961f\u5217,\u5982\u679c\u4e0d\u63d0\u4f9b\u540d\u79f0&#xff0c;\u670d\u52a1\u5668\u5c06\u5206\u914d\u4e00\u4e2a\u540d\u79f0\u3002<br \/>\n        \/\/ &#064;param name \u961f\u5217\u7684\u540d\u79f0<br \/>\n        \/\/ &#064;param flags \u6807\u5fd7\u7ec4\u5408<br \/>\n        \/\/     flags \u53ef\u4ee5\u662f\u4ee5\u4e0b\u503c\u7684\u7ec4\u5408&#xff1a;<br \/>\n        \/\/     -durable \u6301\u4e45\u961f\u5217\u5728\u4ee3\u7406\u91cd\u65b0\u542f\u52a8\u540e\u4ecd\u7136\u6709\u6548<br \/>\n        \/\/     -autodelete \u5f53\u6240\u6709\u8fde\u63a5\u7684\u4f7f\u7528\u8005\u90fd\u79bb\u5f00\u65f6&#xff0c;\u81ea\u52a8\u5220\u9664\u961f\u5217<br \/>\n        \/\/     -passive \u4ec5\u88ab\u52a8\u68c0\u67e5\u961f\u5217\u662f\u5426\u5b58\u5728*-exclusive \u961f\u5217\u4ec5\u5b58\u5728\u4e8e\u6b64\u8fde\u63a5&#xff0c;\u5e76\u4e14\u5728\u8fde\u63a5\u65ad\u5f00\u65f6\u81ea\u52a8\u5220\u9664<br \/>\n        \/\/ &#064;param arguments \u53ef\u9009\u53c2\u6570<\/p>\n<p>        DeferredQueue &amp;declareQueue(const std::string_view &amp;name,<br \/>\n                                    int flags, const Table &amp;arguments);<\/p>\n<p>        \/\/ \u5c06\u961f\u5217\u7ed1\u5b9a\u5230\u4ea4\u6362\u673a<br \/>\n        \/\/ &#064;param exchange \u6e90\u4ea4\u6362\u673a<br \/>\n        \/\/ &#064;param queue \u76ee\u6807\u961f\u5217<br \/>\n        \/\/ &#064;param routingkey \u8def\u7531\u5bc6\u94a5<br \/>\n        \/\/ &#064;param arguments \u5176\u4ed6\u7ed1\u5b9a\u53c2\u6570<\/p>\n<p>        Deferred &amp;bindQueue(const std::string_view &amp;exchange, const std::string_view &amp;queue,<br \/>\n                            const std::string_view &amp;routingkey, const Table &amp;arguments);<\/p>\n<p>        \/\/    \u5c06\u6d88\u606f\u53d1\u5e03\u5230 exchange&#xff0c;\u5fc5\u987b\u63d0\u4f9b\u4ea4\u6362\u673a\u7684\u540d\u79f0\u548c\u8def\u7531\u5bc6\u94a5\u3002\u7136\u540eRabbitMQ \u5c06\u5c1d\u8bd5\u5c06\u6d88\u606f\u53d1\u9001\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u961f\u5217\u3002<br \/>\n        \/\/    \u4f7f\u7528\u53ef\u9009\u7684 flags \u53c2\u6570&#xff0c;\u53ef\u4ee5\u6307\u5b9a\u5982\u679c\u6d88\u606f\u65e0\u6cd5\u8def\u7531\u5230\u961f\u5217\u65f6\u5e94\u8be5\u53d1\u751f\u7684\u60c5\u51b5\u3002<br \/>\n        \/\/    &#064;param exchange \u8981\u53d1\u5e03\u5230\u7684\u4ea4\u6613\u6240<br \/>\n        \/\/    &#064;param routingkey \u8def\u7531\u5bc6\u94a5<br \/>\n        \/\/    &#064;param envelope \u8981\u53d1\u9001\u7684\u5b8c\u6574\u4fe1\u5c01<br \/>\n        \/\/    &#064;param message \u8981\u53d1\u9001\u7684\u6d88\u606f<br \/>\n        \/\/    &#064;param size \u6d88\u606f\u7684\u5927\u5c0f<br \/>\n        \/\/    &#064;param flags \u53ef\u9009\u6807\u5fd7<br \/>\n        \/\/         \u53ef\u4ee5\u63d0\u4f9b\u4ee5\u4e0b flags&#xff1a;<br \/>\n        \/\/         -mandatory \u5982\u679c\u8bbe\u7f6e&#xff0c;\u670d\u52a1\u5668\u5c06\u8fd4\u56de\u672a\u53d1\u9001\u5230\u961f\u5217\u7684\u6d88\u606f<br \/>\n        \/\/         -immediate \u5982\u679c\u8bbe\u7f6e&#xff0c;\u670d\u52a1\u5668\u5c06\u8fd4\u56de\u65e0\u6cd5\u7acb\u5373\u8f6c\u53d1\u7ed9\u4f7f\u7528\u8005\u7684\u6d88\u606f\u3002<br \/>\n        bool publish(const std::string_view &amp;exchange, const std::string_view &amp;routingKey,<br \/>\n                     const std::string &amp;message, int flags &#061; 0);<\/p>\n<p>        \/\/    \u544a\u8bc9 RabbitMQ \u670d\u52a1\u5668\u6211\u4eec\u5df2\u51c6\u5907\u597d\u4f7f\u7528\u6d88\u606f-\u4e5f\u5c31\u662f\u8ba2\u9605\u961f\u5217\u6d88\u606f,\u8c03\u7528\u6b64\u65b9\u6cd5\u540e&#xff0c;RabbitMQ \u5f00\u59cb\u5411\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\u4f20\u9012\u6d88\u606f\u3002<br \/>\n        \/\/    &#064;param queue \u60a8\u8981\u4f7f\u7528\u7684\u961f\u5217<br \/>\n        \/\/    &#064;param tag \u5c06\u4e0e\u6b64\u6d88\u8d39\u64cd\u4f5c\u5173\u8054\u7684\u6d88\u8d39\u8005\u6807\u8bb0<br \/>\n        \/\/     consumer tag \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6807\u8bc6\u7b26&#xff0c;\u5982\u679c\u4ee5\u540e\u60f3\u901a\u8fc7 channel::cancel()\u8c03\u7528\u505c\u6b62\u5b83&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6807\u8bc6\u4f7f\u7528\u8005\u3002<br \/>\n        \/\/     \u5982\u679c\u60a8\u6ca1\u6709\u6307\u5b9a\u4f7f\u7528\u8005 tag&#xff0c;\u670d\u52a1\u5668\u5c06\u4e3a\u60a8\u5206\u914d\u4e00\u4e2a\u3002<br \/>\n        \/\/    &#064;param flags \u5176\u4ed6\u6807\u8bb0<br \/>\n        \/\/    &#064;param arguments \u5176\u4ed6\u53c2\u6570<br \/>\n        \/\/     \u652f\u6301\u4ee5\u4e0b flags&#xff1a;<br \/>\n        \/\/     -nolocal \u5982\u679c\u8bbe\u7f6e\u4e86&#xff0c;\u5219\u4e0d\u4f1a\u540c\u65f6\u6d88\u8017\u5728\u6b64\u901a\u9053\u4e0a\u53d1\u5e03\u7684\u6d88\u606f<br \/>\n        \/\/     -noack \u5982\u679c\u8bbe\u7f6e\u4e86&#xff0c;\u5219\u4e0d\u5fc5\u5bf9\u5df2\u6d88\u8d39\u7684\u6d88\u606f\u8fdb\u884c\u786e\u8ba4<br \/>\n        \/\/     -exclusive \u8bf7\u6c42\u72ec\u5360\u8bbf\u95ee&#xff0c;\u53ea\u6709\u6b64\u4f7f\u7528\u8005\u53ef\u4ee5\u8bbf\u95ee\u961f\u5217<\/p>\n<p>        DeferredConsumer &amp;consume(const std::string_view &amp;queue,<br \/>\n                                  const std::string_view &amp;tag, int flags, const Table &amp;arguments);<\/p>\n<p>        \/\/ \u786e\u8ba4\u63a5\u6536\u5230\u7684\u6d88\u606f&#xff0c;\u5f53\u5728 DeferredConsumer::onReceived()\u65b9\u6cd5\u4e2d\u63a5\u6536\u5230\u6d88\u606f\u65f6&#xff0c;\u5fc5\u987b\u786e\u8ba4\u8be5\u6d88\u606f&#xff0c;<br \/>\n        \/\/ \u4ee5\u4fbf RabbitMQ \u5c06\u5176\u4ece\u961f\u5217\u4e2d\u5220\u9664&#xff08;\u9664\u975e\u4f7f\u7528 noack \u9009\u9879\u6d88\u8d39&#xff09;\u3002<br \/>\n        \/\/ &#064;param deliveryTag \u6d88\u606f\u7684\u552f\u4e00 delivery \u6807\u7b7e<br \/>\n        \/\/ &#064;param flags \u53ef\u9009\u6807\u5fd7<br \/>\n        bool ack(uint64_t deliveryTag, int flags &#061; 0);<br \/>\n    };<br \/>\n    class DeferredConsumer<br \/>\n    {<br \/>\n        \/\/ \u6ce8\u518c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u8be5\u51fd\u6570\u5728\u6d88\u8d39\u8005\u542f\u52a8\u65f6\u88ab\u8c03\u7528\u3002<br \/>\n        DeferredConsumer &amp;onSuccess(const ConsumeCallback &amp;callback);<br \/>\n        \/\/ \u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff0c;\u7528\u4e8e\u63a5\u6536\u5230\u4e00\u4e2a\u5b8c\u6574\u6d88\u606f\u7684\u65f6\u5019\u88ab\u8c03\u7528<br \/>\n        void MessageCallback(const AMQP::Message &amp;message, uint64_t deliveryTag, bool redelivered);<br \/>\n        DeferredConsumer &amp;onReceived(const MessageCallback &amp;callback);<br \/>\n        DeferredConsumer &amp;onMessage(const MessageCallback &amp;callback);<br \/>\n    };<br \/>\n    class Message : public Envelope<br \/>\n    {<br \/>\n        const std::string &amp;exchange();<br \/>\n        const std::string &amp;routingkey();<br \/>\n    };<br \/>\n    class Envelope : public MetaData<br \/>\n    {<br \/>\n        const char *body();<br \/>\n        uint64_t bodySize();<br \/>\n    };<br \/>\n} <\/p>\n<h4><span style=\"color:#000000\">ev <\/span><\/h4>\n<p>         typedef struct ev_async<br \/>\n{<br \/>\n    EV_WATCHER(ev_async);<br \/>\n    EV_ATOMIC_T sent; \/* private *\/<br \/>\n} ev_async;<br \/>\n\/\/ break type<br \/>\nenum<br \/>\n{<br \/>\n    EVBREAK_CANCEL &#061; 0, \/* undo unloop *\/<br \/>\n    EVBREAK_ONE &#061; 1,    \/* unloop once *\/<br \/>\n    EVBREAK_ALL &#061; 2     \/* unloop all loops *\/<br \/>\n};<br \/>\nstruct ev_loop *ev_default_loop(unsigned int flags EV_CPP(&#061; 0));<br \/>\n#define EV_DEFAULT ev_default_loop(0)<br \/>\nint ev_run(struct ev_loop *loop);<br \/>\nvoid ev_break(struct ev_loop *loop, int32_t break_type);<br \/>\nvoid (*callback)(struct ev_loop *loop, ev_async *watcher, int32_t revents);<br \/>\nvoid ev_async_init(ev_async *w, callback cb);<br \/>\nvoid ev_async_start(struct ev_loop *loop, ev_async *w);<br \/>\nvoid ev_async_send(struct ev_loop *loop, ev_async *w); <\/p>\n<h2>\u4f7f\u7528\u6848\u4f8b<\/h2>\n<p>          <span style=\"color:#000000\">\u4e8c\u6b21\u5c01\u88c5\u601d\u60f3&#xff1a; <\/span> <\/p>\n<p>          <span style=\"color:#000000\">\u5b9e\u73b0\u4e00\u53f0\u4e3b\u673a\u5c06\u6d88\u606f\u53d1\u5e03\u7ed9\u53e6\u4e00\u53f0\u4e3b\u673a\u8fdb\u884c\u5904\u7406\u7684\u529f\u80fd&#xff0c;\u53ef\u4ee5\u5bf9 mq \u7684\u64cd\u4f5c\u8fdb\u884c\u7b80\u5355\u7684\u5c01\u88c5&#xff0c;\u4f7f mq \u7684\u64cd\u4f5c\u66f4\u52a0\u7b80\u4fbf&#xff0c;\u5c01\u88c5\u4e00\u4e2a MQClient<\/span><br \/>\n          <span style=\"color:#000000\">&#xff1a; <\/span> <\/p>\n<ul>\n<li><span style=\"color:#000000\">\u63d0\u4f9b\u58f0\u660e\u6307\u5b9a\u4ea4\u6362\u673a\u4e0e\u961f\u5217&#xff0c;\u5e76\u8fdb\u884c\u7ed1\u5b9a\u7684\u529f\u80fd<\/span><\/li>\n<li><span style=\"color:#000000\">\u63d0\u4f9b\u5411\u6307\u5b9a\u4ea4\u6362\u673a\u53d1\u5e03\u6d88\u606f\u7684\u529f\u80fd <\/span><\/li>\n<li><span style=\"color:#000000\">\u63d0\u4f9b\u8ba2\u9605\u6307\u5b9a\u961f\u5217\u6d88\u606f&#xff0c;\u5e76\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u6d88\u606f\u6d88\u8d39\u5904\u7406\u7684\u529f\u80fd<\/span><\/li>\n<\/ul>\n<p>rabbitmq.hpp<\/p>\n<p>         #pragma once<br \/>\n#include &lt;ev.h&gt;<br \/>\n#include &lt;amqpcpp.h&gt;<br \/>\n#include &lt;amqpcpp\/libev.h&gt;<br \/>\n#include &lt;openssl\/ssl.h&gt;<br \/>\n#include &lt;openssl\/opensslv.h&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &#034;logger.hpp&#034;<\/p>\n<p>class MQClient<br \/>\n{<br \/>\npublic:<br \/>\n    using ptr &#061; std::shared_ptr&lt;MQClient&gt;;<br \/>\n    using MessageCallback &#061; std::function&lt;void(const char *, size_t)&gt;;<br \/>\n    MQClient(const std::string &amp;user, const std::string &amp;password, const std::string &amp;host)<br \/>\n    {<br \/>\n        \/\/ 1.\u5b9e\u4f8b\u5316\u5e95\u5c42\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\u7684IO\u4e8b\u4ef6\u76d1\u63a7\u53e5\u67c4<br \/>\n        _loop &#061; EV_DEFAULT;<br \/>\n        \/\/ 2.\u5b9e\u4f8b\u5316LibEvHandler\u53e5\u67c4&#xff0c;\u5c06AMQP\u6846\u67b6\u4e0e\u4e8b\u4ef6\u76d1\u63a7\u5173\u8054\u8d77\u6765<br \/>\n        _handler &#061; std::make_unique&lt;AMQP::LibEvHandler&gt;(_loop);<br \/>\n        \/\/ 3.\u5b9e\u4f8b\u5316\u8fde\u63a5\u5bf9\u8c61<br \/>\n        \/\/ amqp:\/\/root:2162627569&#064;127.0.0.1:5672\/<br \/>\n        std::string url &#061; &#034;amqp:\/\/&#034; &#043; user &#043; &#034;:&#034; &#043; password &#043; &#034;&#064;&#034; &#043; host &#043; &#034;\/&#034;;<br \/>\n        AMQP::Address address(url);<br \/>\n        _connection &#061; std::make_unique&lt;AMQP::TcpConnection&gt;(_handler.get(), address);<br \/>\n        \/\/ 4.\u5b9e\u4f8b\u5316\u4fe1\u9053\u5bf9\u8c61<br \/>\n        _channel &#061; std::make_unique&lt;AMQP::TcpChannel&gt;(_connection.get());<br \/>\n        \/\/ 5.\u542f\u52a8\u5e95\u5c42\u7f51\u7edc\u901a\u4fe1\u6846\u67b6&#xff0c;\u5f00\u542fIO<br \/>\n        _loop_thread &#061; std::thread([this]()<br \/>\n                                   { ev_run(_loop, 0); });<br \/>\n    }<br \/>\n    ~MQClient()<br \/>\n    {<br \/>\n        ev_async_init(&amp;_async_watcher, watcher_callback);<br \/>\n        ev_async_start(_loop, &amp;_async_watcher);<br \/>\n        ev_async_send(_loop, &amp;_async_watcher);<br \/>\n        _loop_thread.join();<br \/>\n        _loop &#061; nullptr;<br \/>\n    }<br \/>\n    void declareComponents(const std::string &amp;exchange, const std::string &amp;queue,<br \/>\n                           const std::string &amp;routing_key &#061; &#034;routing_key&#034;, AMQP::ExchangeType exchange_type &#061; AMQP::ExchangeType::direct)<br \/>\n    {<br \/>\n        \/\/ \u58f0\u660e\u4ea4\u6362\u673a<br \/>\n        _channel-&gt;declareExchange(exchange, exchange_type)<br \/>\n            .onError([&amp;exchange](const char *msg)<br \/>\n                     {<br \/>\n        LOG_ERROR(&#034;{}\u4ea4\u6362\u673a\u521b\u5efa\u5931\u8d25&#xff1a;{}&#034;,exchange,msg);<br \/>\n        exit(1); })<br \/>\n            .onSuccess([&amp;exchange]()<br \/>\n                       { LOG_INFO(&#034;{}\u4ea4\u6362\u673a\u521b\u5efa\u6210\u529f&#xff01;&#034;, exchange); });<br \/>\n        \/\/ \u58f0\u660e\u961f\u5217<br \/>\n        _channel-&gt;declareQueue(queue)<br \/>\n            .onError([&amp;queue](const char *msg)<br \/>\n                     {<br \/>\n        LOG_ERROR(&#034;{}\u961f\u5217\u521b\u5efa\u5931\u8d25&#xff1a;{}&#034;,queue,msg);<br \/>\n        exit(1); })<br \/>\n            .onSuccess([&amp;queue]()<br \/>\n                       { LOG_INFO(&#034;{}\u961f\u5217\u521b\u5efa\u6210\u529f&#xff01;&#034;, queue); });<br \/>\n        \/\/ 6.\u7ed1\u5b9a\u4ea4\u6362\u673a\u548c\u961f\u5217<br \/>\n        _channel-&gt;bindQueue(exchange, queue, routing_key)<br \/>\n            .onError([&amp;exchange, &amp;queue](const char *msg)<br \/>\n                     {<br \/>\n        LOG_ERROR(&#034;{} &#8211; {}\u7ed1\u5b9a\u5931\u8d25&#xff1a;{}&#034;,exchange,queue,msg);<br \/>\n        exit(1); })<br \/>\n            .onSuccess([&amp;exchange, &amp;queue]()<br \/>\n                       { LOG_INFO(&#034;{} &#8211; {}\u7ed1\u5b9a\u6210\u529f&#xff01;&#034;, exchange, queue); });<br \/>\n    }<br \/>\n    bool publish(const std::string &amp;exchange, const std::string &amp;msg, const std::string &amp;routing_key &#061; &#034;routing_key&#034;)<br \/>\n    {<br \/>\n        LOG_DEBUG(&#034;\u5411\u4ea4\u6362\u673a {}-{} \u53d1\u5e03\u6d88\u606f&#xff01;&#034;, exchange, routing_key);<br \/>\n        bool ret &#061; _channel-&gt;publish(exchange, routing_key, msg);<br \/>\n        if (ret &#061;&#061; false)<br \/>\n        {<br \/>\n            LOG_ERROR(&#034;{} \u53d1\u5e03\u6d88\u606f\u5931\u8d25&#xff1a;&#034;, exchange);<br \/>\n            return false;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n   void consume(const std::string &amp;queue, const MessageCallback &amp;cb)<br \/>\n    {<br \/>\n        LOG_DEBUG(&#034;\u5f00\u59cb\u8ba2\u9605 {} \u961f\u5217\u6d88\u606f&#xff01;&#034;, queue);<br \/>\n        _channel-&gt;consume(queue, &#034;consume-tags&#034;)<br \/>\n            .onReceived([this, &amp;cb](const AMQP::Message &amp;message, uint32_t deliveryTag, bool redelivered)<br \/>\n                        {<br \/>\n            cb(message.body(),message.bodySize());<br \/>\n            _channel-&gt;ack(deliveryTag); })<br \/>\n            .onError([&amp;queue](const char *message)<br \/>\n                     {<br \/>\n                    LOG_ERROR(&#034;\u8ba2\u9605 {} \u961f\u5217\u6d88\u606f\u5931\u8d25: {}&#034;, queue, message);<br \/>\n                    exit(1); });<br \/>\n    }<\/p>\n<p>private:<br \/>\n    static void watcher_callback(struct ev_loop *loop, ev_async *watcher, int32_t revents)<br \/>\n    {<br \/>\n        ev_break(loop, EVBREAK_ALL);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    struct ev_async _async_watcher;<br \/>\n    struct ev_loop *_loop;<br \/>\n    std::unique_ptr&lt;AMQP::LibEvHandler&gt; _handler;<br \/>\n    std::unique_ptr&lt;AMQP::TcpConnection&gt; _connection;<br \/>\n    std::unique_ptr&lt;AMQP::TcpChannel&gt; _channel;<br \/>\n    std::thread _loop_thread;<br \/>\n}; <\/p>\n<p>consume.cc<\/p>\n<p>         #include &#034;rabbitmq.hpp&#034;<br \/>\n#include &#034;logger.hpp&#034;<br \/>\n#include &lt;gflags\/gflags.h&gt;<\/p>\n<p>DEFINE_string(user, &#034;root&#034;, &#034;rabbitmq\u8bbf\u95ee\u7528\u6237\u540d&#034;);<br \/>\nDEFINE_string(password, &#034;2162627569&#034;, &#034;rabbitmq\u8bbf\u95ee\u5bc6\u7801&#034;);<br \/>\nDEFINE_string(host, &#034;127.0.0.1:5672&#034;, &#034;rabbitmq\u670d\u52a1\u5668\u5730\u5740\u4fe1\u606f host:port&#034;);<\/p>\n<p>DEFINE_bool(run_mode, false, &#034;\u7a0b\u5e8f\u7684\u8fd0\u884c\u6a21\u5f0f&#xff0c;false-\u8c03\u8bd5&#xff1b; true-\u53d1\u5e03&#xff1b;&#034;);<br \/>\nDEFINE_string(log_file, &#034;&#034;, &#034;\u53d1\u5e03\u6a21\u5f0f\u4e0b&#xff0c;\u7528\u4e8e\u6307\u5b9a\u65e5\u5fd7\u7684\u8f93\u51fa\u6587\u4ef6&#034;);<br \/>\nDEFINE_int32(log_level, 0, &#034;\u53d1\u5e03\u6a21\u5f0f\u4e0b&#xff0c;\u7528\u4e8e\u6307\u5b9a\u65e5\u5fd7\u8f93\u51fa\u7b49\u7ea7&#034;);<\/p>\n<p>void callback(const char *body, size_t sz)<br \/>\n{<br \/>\n    std::string msg;<br \/>\n    msg.assign(body, sz);<br \/>\n    LOG_DEBUG(&#034;{}&#034;, msg);<br \/>\n}<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    google::ParseCommandLineFlags(&amp;argc, &amp;argv, true);<br \/>\n    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);<\/p>\n<p>    MQClient client(FLAGS_user, FLAGS_password, FLAGS_host);<br \/>\n    client.declareComponents(&#034;test-exchange&#034;, &#034;test-queue&#034;, &#034;test-queue-key&#034;);<br \/>\n    client.consume(&#034;test-queue&#034;, callback);<br \/>\n    std::this_thread::sleep_for(std::chrono::seconds(60));<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>publish.cc<\/p>\n<p>         #include &#034;rabbitmq.hpp&#034;<br \/>\n#include &#034;logger.hpp&#034;<br \/>\n#include &lt;gflags\/gflags.h&gt;<\/p>\n<p>DEFINE_string(user, &#034;root&#034;, &#034;rabbitmq\u8bbf\u95ee\u7528\u6237\u540d&#034;);<br \/>\nDEFINE_string(password, &#034;2162627569&#034;, &#034;rabbitmq\u8bbf\u95ee\u5bc6\u7801&#034;);<br \/>\nDEFINE_string(host, &#034;127.0.0.1:5672&#034;, &#034;rabbitmq\u670d\u52a1\u5668\u5730\u5740\u4fe1\u606f host:port&#034;);<\/p>\n<p>DEFINE_bool(run_mode, false, &#034;\u7a0b\u5e8f\u7684\u8fd0\u884c\u6a21\u5f0f&#xff0c;false-\u8c03\u8bd5&#xff1b; true-\u53d1\u5e03&#xff1b;&#034;);<br \/>\nDEFINE_string(log_file, &#034;&#034;, &#034;\u53d1\u5e03\u6a21\u5f0f\u4e0b&#xff0c;\u7528\u4e8e\u6307\u5b9a\u65e5\u5fd7\u7684\u8f93\u51fa\u6587\u4ef6&#034;);<br \/>\nDEFINE_int32(log_level, 0, &#034;\u53d1\u5e03\u6a21\u5f0f\u4e0b&#xff0c;\u7528\u4e8e\u6307\u5b9a\u65e5\u5fd7\u8f93\u51fa\u7b49\u7ea7&#034;);<\/p>\n<p>void callback(const char *body, size_t sz)<br \/>\n{<br \/>\n    std::string msg;<br \/>\n    msg.assign(body, sz);<br \/>\n    LOG_DEBUG(&#034;{}&#034;, msg);<br \/>\n}<br \/>\nint main(int argc, char *argv[])<br \/>\n{<br \/>\n    google::ParseCommandLineFlags(&amp;argc, &amp;argv, true);<br \/>\n    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);<\/p>\n<p>    MQClient client(FLAGS_user, FLAGS_password, FLAGS_host);<br \/>\n    client.declareComponents(&#034;test-exchange&#034;, &#034;test-queue&#034;, &#034;test-queue-key&#034;);<br \/>\n    for (int i &#061; 0; i &lt; 10; i&#043;&#043;)<br \/>\n    {<br \/>\n        std::string msg &#061; &#034;hello world &#8211; &#034; &#043; std::to_string(i);<br \/>\n        bool ret &#061; client.publish(&#034;test-exchange&#034;, msg, &#034;test-queue-key&#034;);<br \/>\n        if (ret &#061;&#061; false)<br \/>\n        {<br \/>\n            std::cout &lt;&lt; &#034;publish \u5931\u8d25&#xff01;\\\\n&#034;;<br \/>\n        }<br \/>\n    }<br \/>\n    std::this_thread::sleep_for(std::chrono::seconds(3));<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>makefile<\/p>\n<p>         all:publish consume<br \/>\npublish:publish.cc<br \/>\ng&#043;&#043; -g -o $&#064; $^ -std&#061;c&#043;&#043;17 -lamqpcpp -lev -lspdlog -lfmt -lgflags<br \/>\nconsume:consume.cc<br \/>\ng&#043;&#043; -g -o $&#064; $^ -std&#061;c&#043;&#043;17 -lamqpcpp -lev -lspdlog -lfmt -lgflags<\/p>\n<p>.PHONY:clean<br \/>\nclean:<br \/>\nrm -f publish consume <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb892\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf10\u6b21\u3002AMQP-CPP\u662f\u7528\u4e8e\u4e0eRabbitMq\u6d88\u606f\u4e2d\u95f4\u4ef6\u901a\u4fe1\u7684c++\u5e93\u3002\u5b83\u80fd\u89e3\u6790\u4eceRabbitMq \u670d\u52a1\u53d1\u9001\u6765\u7684\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u751f\u6210\u53d1\u5411 RabbitMq\u7684\u6570\u636e\u5305\u3002AMQP-CPP\u5e93\u4e0d\u4f1a\u5411 RabbitMq \u5efa\u7acb\u7f51\u7edc\u8fde\u63a5\uff0c\u6240\u6709\u7684\u7f51\u7edcio\u7531\u7528\u6237\u5b8c\u6210\u3002\u5f53\u7136\uff0cAMQP-CPP \u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u7f51\u7edc\u5c42\u63a5\u53e3\uff0c\u5b83\u9884\u5b9a\u4e49\u4e86TCP\u6a21\u5757\uff0c\u7528\u6237\u5c31\u4e0d\u7528\u81ea\u5df1\u5b9e\u73b0\u7f51\u7edc io\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u62e9libeventlibevlibuvasio\u7b49\u5f02\u6b65\u901a\u4fe1\u7ec4\u4ef6\uff0c\u9700\u8981\u624b\u52a8\u5b89\u88c5\u5bf9\u5e94\u7684\u7ec4\u4ef6\u3002AMQP-CPP\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[663,664],"topic":[],"class_list":["post-38437","post","type-post","status-publish","format-standard","hentry","category-server","tag-rabbitmq","tag-664"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/38437.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb892\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf10\u6b21\u3002AMQP-CPP\u662f\u7528\u4e8e\u4e0eRabbitMq\u6d88\u606f\u4e2d\u95f4\u4ef6\u901a\u4fe1\u7684c++\u5e93\u3002\u5b83\u80fd\u89e3\u6790\u4eceRabbitMq \u670d\u52a1\u53d1\u9001\u6765\u7684\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u751f\u6210\u53d1\u5411 RabbitMq\u7684\u6570\u636e\u5305\u3002AMQP-CPP\u5e93\u4e0d\u4f1a\u5411 RabbitMq \u5efa\u7acb\u7f51\u7edc\u8fde\u63a5\uff0c\u6240\u6709\u7684\u7f51\u7edcio\u7531\u7528\u6237\u5b8c\u6210\u3002\u5f53\u7136\uff0cAMQP-CPP \u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u7f51\u7edc\u5c42\u63a5\u53e3\uff0c\u5b83\u9884\u5b9a\u4e49\u4e86TCP\u6a21\u5757\uff0c\u7528\u6237\u5c31\u4e0d\u7528\u81ea\u5df1\u5b9e\u73b0\u7f51\u7edc io\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u62e9libeventlibevlibuvasio\u7b49\u5f02\u6b65\u901a\u4fe1\u7ec4\u4ef6\uff0c\u9700\u8981\u624b\u52a8\u5b89\u88c5\u5bf9\u5e94\u7684\u7ec4\u4ef6\u3002AMQP-CPP\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38437.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T05:02:34+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38437.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38437.html\",\"name\":\"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-20T05:02:34+00:00\",\"dateModified\":\"2025-05-20T05:02:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38437.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38437.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38437.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/38437.html","og_locale":"zh_CN","og_type":"article","og_title":"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb892\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf10\u6b21\u3002AMQP-CPP\u662f\u7528\u4e8e\u4e0eRabbitMq\u6d88\u606f\u4e2d\u95f4\u4ef6\u901a\u4fe1\u7684c++\u5e93\u3002\u5b83\u80fd\u89e3\u6790\u4eceRabbitMq \u670d\u52a1\u53d1\u9001\u6765\u7684\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u751f\u6210\u53d1\u5411 RabbitMq\u7684\u6570\u636e\u5305\u3002AMQP-CPP\u5e93\u4e0d\u4f1a\u5411 RabbitMq \u5efa\u7acb\u7f51\u7edc\u8fde\u63a5\uff0c\u6240\u6709\u7684\u7f51\u7edcio\u7531\u7528\u6237\u5b8c\u6210\u3002\u5f53\u7136\uff0cAMQP-CPP \u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u7f51\u7edc\u5c42\u63a5\u53e3\uff0c\u5b83\u9884\u5b9a\u4e49\u4e86TCP\u6a21\u5757\uff0c\u7528\u6237\u5c31\u4e0d\u7528\u81ea\u5df1\u5b9e\u73b0\u7f51\u7edc io\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u62e9libeventlibevlibuvasio\u7b49\u5f02\u6b65\u901a\u4fe1\u7ec4\u4ef6\uff0c\u9700\u8981\u624b\u52a8\u5b89\u88c5\u5bf9\u5e94\u7684\u7ec4\u4ef6\u3002AMQP-CPP\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/38437.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-20T05:02:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38437.html","url":"https:\/\/www.wsisp.com\/helps\/38437.html","name":"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-20T05:02:34+00:00","dateModified":"2025-05-20T05:02:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38437.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38437.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38437.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"RabbitMq C++\u5ba2\u6237\u7aef\u7684\u4f7f\u7528"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=38437"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38437\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38437"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}