其他分享
首页 > 其他分享> > RabbitMQ 生产者与消费者Code

RabbitMQ 生产者与消费者Code

作者:互联网

Nuget引入RabbitMQ Client

 

消费者:

// See https://aka.ms/new-console-template for more information
using RabbitMQ.Client;
using System.Text;

Console.WriteLine("Hello, World!");

string dir = AppDomain.CurrentDomain.BaseDirectory;
string? consumerName = Path.GetFileName(Path.GetDirectoryName(dir));

ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.8.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5677; //默认端口号是:5672
factory.AutomaticRecoveryEnabled = true;

using (IConnection connection = factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        //创建队列
        channel.QueueDeclare(queue: "OnlyProducerMessageYangpeng", durable: true, exclusive: false, autoDelete: false, arguments: null);
        //创建交换机
        channel.ExchangeDeclare(exchange: "OnlyProducerMessageExChangeYangpeng", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
        //将队列和交换机绑定
        channel.QueueBind(queue: "OnlyProducerMessageYangpeng", exchange: "OnlyProducerMessageExChangeYangpeng", routingKey: String.Empty, arguments: null);
    
        Console.ForegroundColor=ConsoleColor.Red;
        Console.WriteLine($"生产者{consumerName}已经准备就绪");

        int i = 1;

        while (i <= 1000)
        {
            string message = $"{consumerName}生成者发送第{i}条消息;";
            byte[] body=Encoding.UTF8.GetBytes(message);
            channel.BasicPublish(exchange: "OnlyProducerMessageExChangeYangpeng", routingKey: string.Empty, basicProperties: null, body: body);
            Console.WriteLine($"消息已发送{i}条");
            i++;
            Thread.Sleep(500);
        }
    }
}

  

生产者:

// See https://aka.ms/new-console-template for more information
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

Console.WriteLine("Hello, World!");

string dir = AppDomain.CurrentDomain.BaseDirectory;
string? consumerName = Path.GetFileName(Path.GetDirectoryName(dir));

ConnectionFactory factory = new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.8.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5677; //默认端口号是:5672

using (IConnection connection = factory.CreateConnection())  //基于链接工厂创建练级
{
    using (var channel = connection.CreateModel()) //基于链接创建信道
    {
        Console.ForegroundColor = ConsoleColor.Green;
        try
        {
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body.ToArray());
                Console.WriteLine($"{consumerName}成功接受到消息,消息内容为:{message}");
            };
            channel.BasicConsume(queue: "OnlyProducerMessageYangpeng", autoAck: true, consumer: consumer);
            Console.WriteLine("Press[enter] to exit.");
            Console.ReadLine();

        }
        catch (Exception ex) {

            Console.WriteLine(ex.Message);
        }
    }
}

  

标签:Code,Console,生产者,factory,RabbitMQ,WriteLine,using,channel
来源: https://www.cnblogs.com/yangpengNetCore/p/16667960.html