Docker RabbitMQ 消息在重启时消失

Docker RabbitMQ Message disappear on restart

我用这个命令行新建RabbitMQ容器

docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq --hostname rabbitmq rabbitmq:management

代码设置durable:true,然后重启容器队列存在,消息消失

channel.QueueDeclare(
    queue: name,        
    durable: true,      
    exclusive: false,   
    autoDelete: false,  
    arguments: null     
);

请问什么问题?谢谢

有发布吗?

channel.BasicPublish(exchange: "",
                             routingKey: "hello",
                             basicProperties: null,
                             body: body);

参考:https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

来自official documentation

When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.

所以你需要这样的东西:

var properties = channel.CreateBasicProperties();
properties.Persistent = true;

channel.BasicPublish(exchange: "",
                     routingKey: "task_queue",
                     basicProperties: properties,
                     body: body);