现在让我们来创建两个项目,一个是发布者,一个是消费者。
dotnet new console --name Send mv Send/Program.cs Send/Send.cs dotnet new console --name Receive mv Receive/Program.cs Receive/Receive.csThis will create two new directories named Send and Receive.
这一步将会创建两个文件夹,一个叫 Send,一个叫 Receive。
Then we add the client dependency.
紧接着我们来添加客户端依赖。
cd Send dotnet add package RabbitMQ.Client dotnet restore cd ../Receive dotnet add package RabbitMQ.Client dotnet restoreNow we have the .NET project set up we can write some code.
好了,现在我们完成了 .NET 项目的安装,这样就可以写一些代码了。
Sending 发送We'll call our message publisher (sender) Send.cs and our message consumer (receiver) Receive.cs. The publisher will connect to RabbitMQ, send a single message, then exit.
随后我们会调用消息发布者(发送)Send.cs,以及消息消费者(接收)Receive.cs。发布者会连接 RabbitMQ,并发送一条消息,然后退出。
In Send.cs, we need to use some namespaces:
在 Send.cs 中,我们需要引入一些命名空间:
using System; using RabbitMQ.Client; using System.Text;Set up the class:
建立类文件:
class Send { public static void Main() { ... } }then we can create a connection to the server:
然后我们就可以创建一个通往服务器的连接。
class Send { public static void Main() { var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { ... } } } }The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a broker on the local machine - hence the localhost. If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.
该连接是抽象自套接字连接,为我们处理协议关于版本在协商与认证等方面的事宜。在这里,我们会连接到本地机器的一个代理,也就是 localhost。如果我们想连接到一台不同机器上的代理,只需简单地指定主机名或者 IP 地址。
Next we create a channel, which is where most of the API for getting things done resides.
接下来我们将创建一个信道,它在众多的 API 中负责着事项的正确处理。
To send, we must declare a queue for us to send to; then we can publish a message to the queue:
为了能顺利的发送,我们需要先定义一个队列,然后我们就可以发布消息了。
using System; using RabbitMQ.Client; using System.Text; class Send { public static void Main() { var factory = new ConnectionFactory() { HostName = "localhost" }; using(var connection = factory.CreateConnection()) using(var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); string message = "Hello World!"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", message); } Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } }Declaring a queue is idempotent - it will only be created if it doesn't exist already. The message content is a byte array, so you can encode whatever you like there.
声明队列的行为是幂等性的 - 即只有当一个队列不存在时才能被创建。
When the code above finishes running, the channel and the connection will be disposed.
当上述代码完成时,信道和连接将会被释放。
Here's the whole Send.cs class.
这里是完整的 Send.cs 类文件。
Sending doesn't work! 发送运行失败If this is your first time using RabbitMQ and you don't see the "Sent" message then you may be left scratching your head wondering what could be wrong. Maybe the broker was started without enough free disk space (by default it needs at least 50 MB free) and is therefore refusing to accept messages. Check the broker logfile to confirm and reduce the limit if necessary. The configuration file documentation will show you how to set disk_free_limit.