1 修改rabbitmq安装目录中etc下rabbitmq.config.example文件,如下:
%% -*- mode: erlang -*-
%% ----------------------------------------------------------------------------
%% RabbitMQ Sample Configuration File.
%%
%% Related doc guide: http://www.rabbitmq.com/configure.html. See
%% http://rabbitmq.com/documentation.html for documentation ToC.
%% ----------------------------------------------------------------------------
[
{rabbit,
[%%
%% Networking
%% ====================
%%
%% Related doc guide: http://www.rabbitmq.com/networking.html.
%% By default, RabbitMQ will listen on all interfaces, using
%% the standard (reserved) AMQP port.
%%
%% {tcp_listeners, [5672]},
{tcp_listeners, [5672]},
{loopback_users, ["jc"]}, 说明:这里的jc为新添加的管理员
..........
2 网页访问端口默认为15672,程序中访问端口默认为5671。
3 测试代码。
发送消息代码:
#include "SimpleAmqpClient/SimpleAmqpClient.h"
#include <iostream>
#pragma comment ( lib,"SimpleAmqpClient.2.lib" )
using namespace AmqpClient;
int _tmain(int argc, _TCHAR* argv[])
{
std::string queue_name = "hemo";
// AmqpClient::Channel::ptr_t channel;
auto channel = Channel::Create("192.168.0.119");
//创建channel
channel->DeclareQueue(queue_name, false, true, false, false);
//创建队列,第一个参数为队列名称
std::string message;
std::cin >> message;
channel->BasicPublish("", queue_name,
BasicMessage::Create(message));
//第一个是exchange名称,第二个参数是routing_key(此处可理解为消息会被送往的队列)。
std::cout << "[x] send " << message << std::endl;
getchar();
return 0;
}
接受消息代码:
#include <SimpleAmqpClient/SimpleAmqpClient.h>
#include <iostream>
#pragma comment ( lib,"SimpleAmqpClient.2.lib" )
int _tmain(int argc, _TCHAR* argv[])
{
std::string queue_name = "hemo";
printf("hello\n");
AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Create("192.168.0.119");
printf("hello 1 \n");
channel->DeclareQueue(queue_name, false, true, false, false);
printf("hello 2 \n");
std::string consumer_tag = channel->BasicConsume(queue_name, "");
//第二个参数为消费者名称,返回值也是消费者名称。
printf("hello 3\n");
while (1) {
std::cout << "[y] wait for the message" << std::endl;
AmqpClient::Envelope::ptr_t envelope =
channel->BasicConsumeMessage(consumer_tag);
std::string buffer = envelope->Message()->Body();
//消息放在信封里,需要解析
std::cout << "[y] receve " << buffer << std::endl;
}
channel->BasicCancel(consumer_tag);
//关闭消费者。
return 0;
}