文中使用amiquip这个包来使用rabbitmq 我相当于就是把那个英文文档看了一遍 但是还没看全 因为rust得线程这个东西我还没学到(别的语言的线程也是学的不是很精通)
首先放上官方文档的地址https://docs.rs/amiquip/latest/amiquip/
如果有不全的大家可以看文档
首先第一步 在cargo.toml中引入依赖
点击查看代码
``` [dependencies] amiquip = { version = "0.4", default-features = false } ```接下来是惯例得hello world 例子
首先是发布者
点击查看代码
use amiquip::{Connection, Exchange, Publish, Result};
fn main() -> Result<()> {
// Open connection.
let mut connection = Connection::insecure_open("amqp://guest:guest@localhost:5672")?;
// Open a channel - None says let the library choose the channel ID.
let channel = connection.open_channel(None)?;
// Get a handle to the direct exchange on our channel.
let exchange = Exchange::direct(&channel);
// Publish a message to the "hello" queue.
exchange.publish(Publish::new("hello there".as_bytes(), "hello"))?;
connection.close()
}
然后是消费者
点击查看代码
// Port of https://www.rabbitmq.com/tutorials/tutorial-one-python.html. Run this
// in one shell, and run the hello_world_publish example in another.
use amiquip::{Connection, ConsumerMessage, ConsumerOptions, QueueDeclareOptions, Result};
fn main() -> Result<()> {
// Open connection.
let mut connection = Connection::insecure_open("amqp://guest:guest@localhost:5672")?;
// Open a channel - None says let the library choose the channel ID.
let channel = connection.open_channel(None)?;
// Declare the "hello" queue.
let queue = channel.queue_declare("hello", QueueDeclareOptions::default())?;
// Start a consumer.
let consumer = queue.consume(ConsumerOptions::default())?;
println!("Waiting for messages. Press Ctrl-C to exit.");
for (i, message) in consumer.receiver().iter().enumerate() {
match message {
ConsumerMessage::Delivery(delivery) => {
let body = String::from_utf8_lossy(&delivery.body);
println!("({:>3}) Received [{}]", i, body);
consumer.ack(delivery)?;
}
other => {
println!("Consumer ended: {:?}", other);
break;
}
}
}
connection.close()
}
我在学习时 那个消费者得报错 原因是 durable 我的服务器上部署的true 但是他默认配置是false
于是就实例化了一个这个配置结构体
在实例化得时候 又学到了这个rabbitmq得一些参数怎么设置和这个BTreeMap
这个BTreeMap需要引入std::collections::BTreeMap;
点击查看代码
let declartOption = QueueDeclareOptions {
durable:true,
exclusive: false,
auto_delete: false,
arguments:BTreeMap::new(),
};
然后就可以完成简单的发布者和消费者了
之后再完善再写