.net fromwork连接rabbitmq发布消息

1、创建连接工厂类

var factory = new RabbitMQ.Client.ConnectionFactory()
{
    HostName = "120.237.72.46",
    UserName = "admin",
    Password = "admin",
    Port = 5672,
    VirtualHost = "/"
};

2、创建连接并发送数据

using (var connection = factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        channel.QueueDeclare("xh-queue", true, false, false, null);
        IBasicProperties properties = channel.CreateBasicProperties();
        properties.Persistent = true;
        properties.DeliveryMode = 2;
        properties.Expiration = "172800000";//48小时过期

        string da = Newtonsoft.Json.JsonConvert.SerializeObject(data).Replace("\r\n", "");


        channel.BasicPublish(
            exchange: "xh-exchange",
            routingKey: "Order.CreateBuyOrderEto",
            mandatory: true,
            basicProperties: properties,
            body: Encoding.UTF8.GetBytes(da));
        
    }
   
}

 

posted @ 2023-10-11 11:14  爱生活,爱代码  阅读(16)  评论(0编辑  收藏  举报