简体   繁体   English

Azure Service Bus广播到所有工作者角色实例

[英]Azure Service Bus broadcast to all worker role instances

I am trying to use to Azure Service Bus to broadcast a message from a Web Role to all the instances of a single worker role. 我正在尝试使用Azure Service Bus将消息从Web角色广播到单个辅助角色的所有实例。 This is the code I use to receive messages: 这是我用来接收消息的代码:

// Create the topic if it does not exist already
            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
        var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

        // Configure Topic Settings
        TopicDescription td = new TopicDescription("CommandTopic");
        td.MaxSizeInMegabytes = 5120;
        td.DefaultMessageTimeToLive = new TimeSpan(0, 0, 1);

        if (!namespaceManager.TopicExists("CommandTopic"))
        {
            namespaceManager.CreateTopic(td);
        }

        Random rand = new Random();
        double randNum = rand.Next();

        if (!namespaceManager.SubscriptionExists("CommandTopic", "CommandSubscription"+randNum))
        {
            namespaceManager.CreateSubscription("CommandTopic", "CommandSubscription" + randNum);
        }

        Client = SubscriptionClient.CreateFromConnectionString(connectionString, "CommandTopic", "CommandSubscription" + randNum, ReceiveMode.ReceiveAndDelete);
        Trace.WriteLine("SUBSCRIPTION: COMMANDSUBSCRIPTION"+randNum);

In order to create a separate subscription for each worker role instance (so that all instances receive the message in the topic) I had to use a random number. 为了为每个辅助角色实例创建单独的订阅(以便所有实例都在主题中接收消息),我必须使用随机数。 Is there a way of using some Id of the instance instead of the random number. 有没有办法使用实例的一些Id而不是随机数。 There is Instance.Id however it is too long to be used as a parameter for the subscription name. 有Instance.Id但是它太长,不能用作订阅名称的参数。 Is there a shorter version without using substring? 没有使用子字符串是否有更短的版本? Also, is creating a separate subscription for each instance the proper approach? 另外,是为每个实例创建一个单独的订阅方法吗? Previously all instances subscribed to the same subscription and so only 1 instance was getting the message and deleting it from the subscription. 以前所有实例都订阅了相同的订阅,因此只有1个实例获取消息并从订阅中删除它。

Try adding a bogus InternalEndpoint for the worker role to your configuration. 尝试为您的配置添加一个伪工作者角色的InternalEndpoint。 This ensures the list of instances of a role gets populated. 这可确保填充角色实例列表。

please find the following link, I hope it can help you make the intercommunication between your roles: 请找到以下链接,我希望它可以帮助您实现角色之间的相互通信:

http://msdn.microsoft.com/en-us/library/windowsazure/hh180158.aspx http://msdn.microsoft.com/en-us/library/windowsazure/hh180158.aspx

and also please have a look on the following link, I think it has exactly what you are looking for: http://windowsazurecat.com/2011/08/how-to-simplify-scale-inter-role-communication-using-windows-azure-service-bus/ 并且请看看以下链接,我认为它正是您正在寻找的: http//windowsazurecat.com/2011/08/how-to-simplify-scale-inter-role-communication-using- Windows的Azure的服务总线/

I think what you are talking about would be like the Scenario 4 in the following link where a role can communicate to several other roles. 我认为您所谈论的内容将类似于以下链接中的方案4,其中角色可以与其他几个角色进行通信。 I am not sure if what you asked for is possible, but try working with the Windows Azure Worker Role with Service Bus Queue, I think this might help you a lot and might also be a better solution than the topic and subscription in this case. 我不确定您要求的是否可行,但尝试使用服务总线队列的Windows Azure辅助角色,我认为这可能对您有很大帮助,并且在这种情况下也可能是比主题和订阅更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM