publisher:
public bool RabbitMq_PublishMsg(RabbitMqMsg rabbitMqMsg, RabbitMqMsg_Data data)
{
bool flag = false;
try
{
string ExchangeName = ConfigurationManager.AppSettings["RabbitMq_ExchangeName"];
string QueueName = ConfigurationManager.AppSettings["RabbitMq_QueueName"];
string HostUrl = ConfigurationManager.AppSettings["RabbitMq_HostUrl"];
var RabbitMq_UserName = ConfigurationManager.AppSettings["RabbitMq_UserName"];
var RabbitMq_Password = ConfigurationManager.AppSettings["RabbitMq_Password"];
var RabbitMq_VirtualHost = ConfigurationManager.AppSettings["RabbitMq_VirtualHost"];
var RabbitMq_RoutingKey = ConfigurationManager.AppSettings["RabbitMq_RoutingKey"];
var factory = new ConnectionFactory();
factory.AutomaticRecoveryEnabled = true;
factory.VirtualHost = RabbitMq_VirtualHost;
factory.UserName = RabbitMq_UserName;
factory.Password = RabbitMq_Password;
Uri uri = new Uri(HostUrl);
factory.Endpoint = new AmqpTcpEndpoint(uri);
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: QueueName,durable: true, exclusive: false, autoDelete: false, arguments: null); //queue的持久化需要在声明时指定durable=True
channel.QueueBind(queue: QueueName, exchange: ExchangeName, routingKey: RabbitMq_RoutingKey, arguments: null);
channel.ConfirmSelect(); // 消息发送确认
rabbitMqMsg.data = data;
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(rabbitMqMsg));
var properties = channel.CreateBasicProperties();
properties.Persistent = true;//需要持久化Message,即在Publish的时候指定一个properties
properties.DeliveryMode = 2; // /// Non-persistent (1) or persistent (2). ///
channel.BasicPublish(exchange: ExchangeName, routingKey: RabbitMq_RoutingKey, basicProperties: properties, body: body);
flag = channel.WaitForConfirms();// 消息发送确认状态
}
}
//flag = true;
}catch(Exception ex)
{
}
return flag;
}
private void StartRabbitMqCustomer()
{
try
{
var ExchangeName = ConfigurationManager.AppSettings["RabbitMq_ExchangeName"];
var QueueName = ConfigurationManager.AppSettings["RabbitMq_QueueName"];
var HostUrl = ConfigurationManager.AppSettings["RabbitMq_HostUrl"];
var RabbitMq_UserName = ConfigurationManager.AppSettings["RabbitMq_UserName"];
var RabbitMq_Password = ConfigurationManager.AppSettings["RabbitMq_Password"];
var RabbitMq_VirtualHost = ConfigurationManager.AppSettings["RabbitMq_VirtualHost"];
var RabbitMq_RoutingKey = ConfigurationManager.AppSettings["RabbitMq_RoutingKey"];
MyLogHelper.WriteAsync("配置参数已设置");
var factory = new ConnectionFactory();
// amqp://user:pass@192.168.1.25:port/vhost
//服务器所在的主机ip
factory.AutomaticRecoveryEnabled = true;
factory.VirtualHost = RabbitMq_VirtualHost;
factory.UserName = RabbitMq_UserName;
factory.Password = RabbitMq_Password;
Uri uri = new Uri(HostUrl);
factory.Endpoint = new AmqpTcpEndpoint(uri);
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: ExchangeName, type: "direct", durable: true, autoDelete: false, arguments: null);
channel.QueueDeclare(queue: QueueName,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
channel.QueueBind(queue: QueueName, exchange: ExchangeName, routingKey: RabbitMq_RoutingKey, arguments: null);
//这样RabbitMQ就会使得每个Consumer在同一个时间点最多处理一个Message。换句话说,在接收到该Consumer的ack前,他它不会将新的Message分发给它。
channel.BasicQos(0, 1, false);
var consumer = new EventingBasicConsumer(channel);
//consumerTag 设置 消费者tag以便管理
var exeDir = AppDomain.CurrentDomain.BaseDirectory;
var consumerTag = (ApkCreate.GetConfigStrByKey("RabbitMq_CurrentServerId") ?? "consumerTag") + "_" + exeDir;// 运行服务的当前服务器在系统中的ID
channel.BasicConsume(queue: QueueName,
noAck: false,
consumerTag: consumerTag,
consumer: consumer);
//需要接受方发送ack回执,删除消息 noAck= false
MyLogHelper.WriteAsync("开始接收数据");
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
do_work_with_received_data(message);
channel.BasicAck(ea.DeliveryTag, false);
//与channel.BasicConsume("task_queue", false, null, consumer);对应,服务器会该消息删除
};
while (true) { }
}
}
catch (Exception ex)
{
MyLogHelper.WriteAsync("mq接收消息报错,"+ ex.Message+"\r\n"+ ex.StackTrace);
}
}