.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));
        
    }
   
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章