RabbitMQ消息队列、路由器、消息的持久化
如有错误还请斧正,谢谢
```C#
//入列
ConnectionFactory factory = new ConnectionFactory { HostName = "localhost", UserName = "You`rName", Password = "You`rPassword" };//实例化连接工厂
using (IConnection conn = factory.CreateConnection())//建立连接
{
//创建信道
using (IModel im = conn.CreateModel())
{
//order为交换器名称,ExchangeType为交换机类型(Direct,Fanout,Topic,Headers)具体使用场景在印象笔记中有记录
im.ExchangeDeclare("order", ExchangeType.Direct,durable:true,autoDelete:false,arguments:null);
//creatorder为队列名,第一个false是durable(持久化)第二个false是exclusive(是否排他)第三个false为autodelete(是否自动删除)null为arguments(其他参数)
im.QueueDeclare("creatorder", durable:true,exclusive:false,autoDelete:false,arguments:null);
//creatorder为队列名,order为交换器名,ExchangeType.Direct是路由key,null为arguments(其他参数)
im.QueueBind("creatorder", "order", ExchangeType.Direct, null);
byte[] message = ConvertToByteHelp.ObjectToBytes(order);//把对象转换成bytes[]
IBasicProperties properties = im.CreateBasicProperties();
//properties.SetPersistent(true);队列设置为持久化之后,需要将消息也设置为持久化这是老方法已过时下面为先用新方法
properties.DeliveryMode = 2;//将消息持久化的新方法
im.BasicPublish("order", ExchangeType.Direct, properties, message);//推送消息
//im.BasicPublish("order", routingKey: "Direct", basicProperties: properties, body: message);
}
}
```