Masstransit - Rabbit MQ虚拟主机

时间:2013-09-15 11:26:58

标签: c# rabbitmq masstransit

我正在使用公共交通工具并连接到我的兔子经纪人。

 string uri1 = @"rabbitmq://myusername:mypassword@myip/myvirtualhost/myqueue";

我一直没有得到ConfigurationException。

  

在服务总线创建期间抛出异常   {} System.Collections.ListDictionaryInternal.NodeKeyValueCollection   {“无法启动公交服务”}   MassTransit.ServiceContainer.Start()in   d:\ BuildAgent-03 \工作\ aa063b4295dfc097的\ src \ MassTransit \ ServiceContainer.cs:行   83在MassTransit.ServiceBus.Start()中   d:\ BuildAgent-03 \工作\ aa063b4295dfc097的\ src \ MassTransit \ ServiceBus.cs:行   369在MassTransit.Builders.ServiceBusBuilderImpl.Build()中   d:\ BuildAgent-03 \工作\ aa063b4295dfc097的\ src \ MassTransit \配置\建设者\ ServiceBusBuilderImpl.cs:行   84点   MassTransit.BusConfigurators.ServiceBusConfiguratorImpl.CreateServiceBus()   在   d:\ BuildAgent-03 \工作\ aa063b4295dfc097的\ src \ MassTransit \配置\ BusConfigurators \ ServiceBusConfiguratorImpl.cs:行   在MassTransit.ServiceBusFactory.New(Action`1配置)中的171   d:\ BuildAgent-03 \工作\ aa063b4295dfc097的\ src \ MassTransit \配置\ ServiceBusFactory.cs:行   44

真正奇怪的是,它可以在不指定虚拟主机的情况下工作,但是他们正在进入错误的虚拟主机。

然后我下载了rabbitMQ库,并使用我的凭据将其连接到正确的虚拟主机。这就是我知道我的凭据的方式,虚拟主机设置得很好。我甚至添加了一个队列,以防出现问题。 Masstransit有一些错误吗?我真的不明白我做错了什么。我正在考虑不使用masstransit并编写我自己的轻量级版本。

我甚至将其粘贴到https://github.com/MassTransit/MassTransit/blob/v2.7.2/src/Transports/MassTransit.Transports.RabbitMq/RabbitMqEndpointAddress.cs#L167我的解决方案中,以检查它是否正确修剪虚拟主机。我真的很困惑。 `static readonly Regex regex = new Regex(@“^ [A-Za-z0-9 - 。:] + $”);

    public static RabbitMqEndpointAddress Parse(Uri address)
    {
        Guard.AgainstNull(address, "address");

        if (string.Compare("rabbitmq", address.Scheme, true) != 0)
            throw new RabbitMqAddressException("The invalid scheme was specified: " + address.Scheme ?? "(null)");

        var connectionFactory = new ConnectionFactory
        {
            HostName = address.Host,
            UserName = "",
            Password = "",
        };

        if (address.IsDefaultPort)
            connectionFactory.Port = 5672;
        else if (!address.IsDefaultPort)
            connectionFactory.Port = address.Port;

        if (!address.UserInfo.IsEmpty())
        {
            if (address.UserInfo.Contains(":"))
            {
                string[] parts = address.UserInfo.Split(':');
                connectionFactory.UserName = parts[0];
                connectionFactory.Password = parts[1];
            }
            else
                connectionFactory.UserName = address.UserInfo;
        }

        string name = address.AbsolutePath.Substring(1);
        string[] pathSegments = name.Split('/');
        if (pathSegments.Length == 2)
        {
            connectionFactory.VirtualHost = pathSegments[0];
            name = pathSegments[1];
        }

        ushort heartbeat = address.Query.GetValueFromQueryString("heartbeat", connectionFactory.RequestedHeartbeat);
        connectionFactory.RequestedHeartbeat = heartbeat;

       VerifyQueueOrExchangeNameIsLegal(name);

        return new RabbitMqEndpointAddress(address, connectionFactory, name);
    }
    static void VerifyQueueOrExchangeNameIsLegal(string path)
    {
        Match match = _regex.Match(path);

        if (!match.Success)
            throw new RabbitMqAddressException(FormatErrorMsg);
    }

    const string FormatErrorMsg =
      "The path can be empty, or a sequence of these characters: letters, digits, hyphen, underscore, period, or colon.";


    private static void Main(string[] args)
    {

        string uri1 = @"rabbitmq://username:password@mydomain.co.za/vhost/queue";
        IServiceBus serviceBus;

        var result =  Parse(new Uri(uri1));

        serviceBus = ServiceBusFactory.New(sbc =>
        {
            sbc.UseRabbitMq();
            sbc.ReceiveFrom(uri1);
            sbc.Subscribe(c => c.Consumer<SendEmailConsumer>());
        });

        IPublish publishMessage = new MassTransitPublisher(uri1);

        publishMessage.Publish(new SendEmail
            {
                EmailFrom = "*@8.co.za",
                EmailTo = "*@*.co.za",
                Subject = "hello",
                Body = "hello"
            });

        Console.ReadKey();


        JobRunner jobRunner = new JobRunner();

        jobRunner.Start();

        Console.ReadKey();
    }`

任何帮助都将受到赞赏或其他任何我可以尝试使其工作。

1 个答案:

答案 0 :(得分:1)

您使用的是什么版本的MassTransit?我们删除了从Uri声明用户名和密码的功能(因此它不会出现在日志中),您必须通过API执行此操作。见https://groups.google.com/d/msg/masstransit-discuss/4m5Vf04oRWM/hujvVh1HSdwJ

sbc.UseRabbitMq(x => x.ConfigureHost("rabbitmq://yourhost/yourvhost/yourqueue", x=> x.UserName, x.Password)).