Docker:检查RabbitMQ是否准备好订阅?

时间:2019-05-27 15:25:58

标签: c# docker .net-core rabbitmq

这个问题类似于the one I posted about Postgre

基本上,我设置了一个利用.NET Docker客户端并运行RabbitMQ映像(包括管理插件)的程序,然后通过EasyNetQ使用RabbitMQ在同一队列上订阅和发送消息。

public static class Program
{
    public static async Task Main(params string[] args)
    {
        var configurationBuilder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");

        var configuration = new Configuration();
        configurationBuilder.Build().Bind(configuration);

        var client = new DockerClientConfiguration(DockerUris.DefaultLocalApi).CreateClient();

        var containers = await client.Containers.SearchByNameAsync(configuration.ContainerName);
        var container = containers.SingleOrDefault();
        if (container != null)
        {
            await client.Containers.StopAndRemoveContainerAsync(container.ID);
            await client.Volumes.RemoveAsync(container.Mounts.Select(x => x.Name));
        }

        var createdContainer = await client.Containers.RunContainerAsync(new CreateContainerParameters
        {
            Image = configuration.ImageName,
            HostConfig = new HostConfig
            {
                PortBindings = new Dictionary<string, IList<PortBinding>>
                {
                    {$"{configuration.QueueContainerPort}/{configuration.QueueContainerPortProtocol}", 
                        new List<PortBinding>
                        {
                            new PortBinding { HostPort = configuration.QueueHostPort }
                        }
                    },
                    {$"{configuration.ManagementContainerPort}/{configuration.ManagementContainerPortProtocol}", 
                        new List<PortBinding>
                        {
                            new PortBinding { HostPort = configuration.ManagementHostPort }
                        }
                    }
                }
            },
            Env = new List<string>
            {
                $"RABBITMQ_DEFAULT_USER={configuration.Username}",
                $"RABBITMQ_DEFAULT_PASS={configuration.Password}"
            },
            Name = configuration.ContainerName
        });

        // Thread.Sleep(2000);

        var bus = RabbitHutch.CreateBus("host=localhost");

        await bus.PubSub.SubscribeAsync<TextMessage>("sub", message =>
        {
            Console.WriteLine(message.Text);
        });

        await bus.PubSub.PublishAsync<TextMessage>(new TextMessage
        {
            Text = "Michelle"
        });

        Console.ReadKey();
    }
}

public class TextMessage
{
    public string Text { get; set; }
}

如果我在上面运行我的程序(也就是说,如果// Thread.Sleep(2000);没有被注释掉),似乎RabbitMQ尚未完全准备好订阅,我得到了以下异常:

System.TimeoutException: The operation requested on PersistentChannel timed out
  at at EasyNetQ.Producer.PersistentChannel.InvokeChannelAction(Action`1 channelAction)
  at at EasyNetQ.Producer.ClientCommandDispatcherSingleton.<>c__DisplayClass4_0`1.<InvokeAsync>b__0()
  at at EasyNetQ.RabbitAdvancedBus.QueueDeclareAsync(String name, Action`1 configure, CancellationToken cancellationToken)
  at at EasyNetQ.Producer.DefaultPubSub.SubscribeAsyncInternal[T](String subscriptionId, Func`3 onMessage, Action`1 configure, CancellationToken cancellationToken)

我想知道我可以在容器中运行哪种命令,以便知道RabbitMQ已完全可以订阅。

0 个答案:

没有答案