/// <summary> /// RPC服务端 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="exchange"></param> /// <param name="queue"></param> /// <param name="isProperties"></param> /// <param name="handler"></param> /// <param name="isDeadLetter"></param> public void RpcService <T>(string exchange, string queue, bool isProperties, Func <T, T> handler, bool isDeadLetter) { //队列声明 var channel = GetModel(queue, isProperties); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var msgStr = SerializeExtension.DeserializeUtf8(body); var msg = msgStr.FromJson <T>(); var props = ea.BasicProperties; var replyProps = channel.CreateBasicProperties(); replyProps.CorrelationId = props.CorrelationId; try { msg = handler(msg); } catch (Exception ex) { ex.GetInnestException().WriteToFile("队列接收消息", "RabbitMq"); } finally { channel.BasicPublish(exchange, props.ReplyTo, replyProps, msg.ToJson().SerializeUtf8()); channel.BasicAck(ea.DeliveryTag, false); } }; channel.BasicConsume(queue, false, consumer); }
/// <summary> /// 接收消息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queue">队列名称</param> /// <param name="isProperties"></param> /// <param name="handler">消费处理</param> /// <param name="isDeadLetter"></param> public void Subscribe <T>(string queue, bool isProperties, Action <T> handler, bool isDeadLetter) where T : class { //队列声明 var channel = GetModel(queue, isProperties, "Wk.ExchangeName", "Wk.ExchangeName"); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var msgStr = SerializeExtension.DeserializeUtf8(body); var msg = msgStr.FromJson <T>(); try { handler(msg); } catch (Exception ex) { ex.GetInnestException().WriteToFile("队列接收消息", "RabbitMq"); if (!isDeadLetter) { PublishToDead <DeadLetterQueue>(queue, msgStr, ex); } } finally { channel.BasicAck(ea.DeliveryTag, false); } }; channel.BasicConsume(queue, false, consumer); }
/// <summary> /// 获取消息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="exchange"></param> /// <param name="queue"></param> /// <param name="routingKey"></param> /// <param name="handler">消费处理</param> private void Pull <T>(string exchange, string queue, string routingKey, Action <T> handler) where T : class { var channel = GetModel(exchange, queue, routingKey); var result = channel.BasicGet(queue, false); if (ObjectExtension.IsNull(result)) { return; } var msg = SerializeExtension.DeserializeUtf8(result.Body).FromJson <T>(); try { handler(msg); } catch (Exception ex) { ex.GetInnestException().WriteToFile("队列接收消息", "RabbitMq"); } finally { channel.BasicAck(result.DeliveryTag, false); } }
/// <summary> /// RPC客户端 /// </summary> /// <param name="exchange"></param> /// <param name="queue"></param> /// <param name="routingKey"></param> /// <param name="body"></param> /// <param name="isProperties"></param> /// <returns></returns> public string RpcClient(string exchange, string queue, string routingKey, string body, bool isProperties = false) { var channel = GetModel(exchange, queue, routingKey, isProperties); var consumer = new QueueingBasicConsumer(channel); channel.BasicConsume(queue, true, consumer); try { var correlationId = Guid.NewGuid().ToString(); var basicProperties = channel.CreateBasicProperties(); basicProperties.ReplyTo = queue; basicProperties.CorrelationId = correlationId; channel.BasicPublish(exchange, routingKey, basicProperties, body.SerializeUtf8()); var sw = Stopwatch.StartNew(); while (true) { var ea = consumer.Queue.Dequeue(); if (ea.BasicProperties.CorrelationId == correlationId) { return(SerializeExtension.DeserializeUtf8(ea.Body)); } if (sw.ElapsedMilliseconds > 30000) { throw new Exception("等待响应超时"); } } } catch (Exception ex) { throw ex.GetInnestException(); } }
/// <summary> /// 接收消息(订阅广播) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queue">队列名称</param> /// <param name="isProperties"></param> /// <param name="handler">消费处理</param> /// <param name="isDeadLetter"></param> public void SubscribeBroadcast <T>(string queue, string exchange, bool isProperties, Action <T> handler, bool isDeadLetter) where T : class { //队列声明 var channel = GetModel(queue, exchange, ""); channel.ExchangeDeclare(exchange: exchange, type: ExchangeType.Fanout); //默认队列,用于广播 var queueName = channel.QueueDeclare().QueueName; channel.QueueBind(queue: queueName, exchange: exchange, routingKey: ""); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var msgStr = SerializeExtension.DeserializeUtf8(body); var msg = msgStr.FromJson <T>(); try { handler(msg); } catch (Exception ex) { ex.GetInnestException().WriteToFile("队列接收消息", "RabbitMq"); if (!isDeadLetter) { PublishToDead <DeadLetterQueue>(queue, msgStr, ex); } } finally { channel.BasicAck(ea.DeliveryTag, false); } }; channel.BasicConsume(queueName, false, consumer); }