0

RabbitMQ 入门系列:3、基础编码:官方SDK的引用、链接创建、单例改造、发送消息、接...

 2 years ago
source link: https://www.cnblogs.com/cyq1162/p/16602891.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

RabbitMQ 入门系列:1、MQ的应用场景的选择与RabbitMQ安装。

RabbitMQ 入门系列:2、基础含义:链接、通道、队列、交换机。

RabbitMQ 入门系列:3、基础含义:持久化、排它性、自动删除、强制性、路由键。

RabbitMQ 入门系列:4、基础编码:官方SDK使用:链接创建、单例改造、发送消息、接收消息。

RabbitMQ 入门系列:5、基础编码:交换机的进阶介绍及编码方式。

RabbitMQ 入门系列:6、保障消息:不丢失:发送方、Rabbit存储端、接收方。

RabbitMQ 入门系列:7、保障消息:不重复消费:产生消息的唯一ID。

RabbitMQ 入门系列:8、扩展内容:接收信息时:可否根据RoutingKey过滤监听信息,答案是不能。

RabbitMQ 入门系列:9、扩展内容:死信队列:真不适合当延时队列。

RabbitMQ 入门系列:10、扩展内容:延时队列:延时队列插件及其有限的适用场景。

本篇介绍官方提供的SDK:Rabbit.Client的简单使用,本篇尽量使用最简代码,以便初学者能快速理解与掌握。

1、项目中Nuget引入Rabbit.Client:

17408-20220819173357695-256802302.png

2、创建链接:(用户名密码自行调整)

using RabbitMQ.Client;

var factory = new ConnectionFactory()
{
    HostName = "127.0.0.1",
    UserName = "guest",
    Password = "guest",
    VirtualHost = "/"
};

var connection = factory.CreateConnection();

如果是Web应用中使用,这里需要把它改造成单例使用。

3、单例改造:

class Rabbit
{
    ConnectionFactory factory;
    private Rabbit()
    {
        factory = new ConnectionFactory()
        {
            HostName = "127.0.0.1",
            UserName = "guest",
            Password = "guest",
            VirtualHost = "/"
        };
    }
    private IConnection _Connection;
    public IConnection DefaultConnection
    {
        get
        {
            if (_Connection == null)
            {
                _Connection = factory.CreateConnection();
            }
            return _Connection;
        }
    }
    public static Rabbit Instance = new Rabbit();
}

4、发送消息:(创建FirstQueue队列)

using RabbitMQ.Client;
using System.Text;

using (var channel = Rabbit.Instance.DefaultConnection.CreateModel())
{
    channel.QueueDeclare("FirstQueue", false, false, false);
    channel.BasicPublish("", "FirstQueue", false, null, Encoding.UTF8.GetBytes("这是要发送的内容"));
}
17408-20220822111710811-822164418.png

5、发送消息:批量发送

using (var channel = Rabbit.Instance.DefaultConnection.CreateModel())
{
    channel.QueueDeclare("FirstQueue", false, false, false);
    //channel.BasicPublish("", "FirstQueue", false, null, Encoding.UTF8.GetBytes("这是要发送的内容"));
    var pub= channel.CreateBasicPublishBatch();
    pub.Add("", "FirstQueue", false, null, Encoding.UTF8.GetBytes("这是批量要发送的内容1"));
    pub.Add("", "FirstQueue", false, null, Encoding.UTF8.GetBytes("这是批量要发送的内容2"));
    pub.Add("", "FirstQueue", false, null, Encoding.UTF8.GetBytes("这是批量要发送的内容3"));
    pub.Publish();
}
17408-20220822112739538-1137710004.png

6、接收消息:(接收消息要保持通道一直开,所以不能关闭)

using RabbitMQ.Client;
using System.Text;

var channel = Rabbit.Instance.DefaultConnection.CreateModel();

var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
    var message = Encoding.UTF8.GetString(ea.Body.ToArray());
    Console.WriteLine("收到默认消息 {0}", message);
};
channel.BasicConsume("FirstQueue",true,consumer);
17408-20220822113005795-1964715086.png

本篇介绍RabbitMQ最简代码的使用,方便入门与理解。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK