系列的视频:
https://www.bilibili.com/video/BV1jJ411V7Do
1、需要引用相关组件
Nuget查找RabbitMQ.Client,并引用
2、基础代码
1)生产者
//基础配置 ConnectionFactory factory = new ConnectionFactory() { HostName = "10.123.44.12", UserName = "datamip", Password = "datamip" }; //第一步:创建connection using (var connection = factory.CreateConnection()) { //第二步:创建channel using (var channel = connection.CreateModel()) { //第三步:创建exchange 当前使用默认 //第四步:创建一个队列(queue) channel.QueueDeclare("mytest", true, false, false, null); var msg = Encoding.UTF8.GetBytes("你好"); //第五步:发布消息 channel.BasicPublish(string.Empty, "mytest", basicProperties: null, body: msg); } }
2)消费者
//创建连接工厂 ConnectionFactory factory = new ConnectionFactory { UserName = "datamip",//用户名 Password = "datamip",//密码 HostName = "10.123.44.12"//rabbitmq ip }; //第一步:创建connection using (var connection = factory.CreateConnection()) { //第二步:创建channel using (var channel = connection.CreateModel()) { //第三步:获取信息 var result= channel.BasicGet("mytest",true); //第四步:处理消息 Console.WriteLine(Encoding.UTF8.GetString(result.Body.ToArray())); } }