重用在Azure中服务总线连接 [英] Reusing Connections in Azure Service Bus

查看:124
本文介绍了重用在Azure中服务总线连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有2个实例,它接受请求的Web角色在Windows Azure上托管的API,对它们进行验证,然后把它们添加到Azure的服务总线队列。

We have an API hosted on Windows Azure in a web role with 2 instances that takes in requests, validates them then adds them to an Azure Service Bus Queue.

最近,我们已经开始负载测试,发现这个,我们目前的code抛出异常如下:

Recently we've started load testing this and found that our current code throws the below exception:

不能添加命令到命令队列
  例外:
  Microsoft.ServiceBus.Messaging.QuotaExceededException:
  maximumallowed连接数量已经达到或超过了队列
  100,最大允许连接数:活动连接数100

Could not add command to the command queue Exception: Microsoft.ServiceBus.Messaging.QuotaExceededException: The number of maximumallowed connections have been reached or exceeded for Queue Number of active connections: 100, Maximum allowed connections: 100.

请问下方的客户code保持一个连接开放假设我们保持了类的一个实例?我想,以确定问题是否与ServiceBusClient code或的依赖注册初始化一个新的客户端为每个请求。

Would the below client code keep a single connection open assuming we maintained a single instance of the class? I am trying to ascertain whether the issue is with ServiceBusClient code or our dependency registration initialising a new client for each request.

public class ServiceBusClient : IDisposable
{
    #region Constants and Fields

    protected readonly NamespaceManager NamespaceManager;

    private const int DEFAULT_LOCK_DURATION_IN_SECONDS = 300; // 5 minutes 

    private const string SERVICE_BUS_CONNECTION_STRING_KEY 
                            = "service.bus.connection.string";

    private readonly IMessageBodySerializer _messageBodySerializer;

    private readonly MessagingFactory _messagingFactory;

    private readonly QueueClient _queueClient;

    private readonly string _queueName;

    private readonly ISettingsManager _settingsManager;

    #endregion

    #region Constructors and Destructors

    public ServiceBusClient(
        ISettingsManager settingsManager, 
        IMessageBodySerializer messageBodySerializer, s
        tring queueName)
    {
        _settingsManager = settingsManager;
        _messageBodySerializer = messageBodySerializer;

        var connectionString = _settingsManager.GetSetting<string>(
            SERVICE_BUS_CONNECTION_STRING_KEY);

        NamespaceManager = 
            NamespaceManager.CreateFromConnectionString(connectionString);

        _messagingFactory = 
            MessagingFactory.CreateFromConnectionString(connectionString);

        _queueName = queueName;
        _queueClient = GetOrCreateQueue();
    }

    #endregion

    #region Public Methods and Operators

    public virtual void SendMessage(object bodyObject)
    {
        var brokeredMessage = 
            _messageBodySerializer.SerializeMessageBody(bodyObject);

        _queueClient.Send(brokeredMessage);
    }

    public void Dispose()
    {
        _messagingFactory.Close();
    }

    #endregion

    #region Methods

    private QueueClient GetOrCreateQueue()
    {
        QueueDescription queue;

        if (!NamespaceManager.QueueExists(_queueName))
        {
            var queueToCreate = new QueueDescription(_queueName)
            {
                LockDuration = TimeSpan.FromSeconds(
                    DEFAULT_LOCK_DURATION_IN_SECONDS)
            };

            queue = NamespaceManager.CreateQueue(queueToCreate);
        }
        else
        {
            queue = NamespaceManager.GetQueue(_queueName);
        }

        return _messagingFactory.CreateQueueClient(
                        queue.Path, 
                        ReceiveMode.PeekLock);
    }

    #endregion
}

作为扩展到这一点;如果上述code确实保持活动连接打开;会使用Singleton模式来存储ServiceBusClient的API的每个实例的实例是危险的?还是在Azure SDK手柄关闭的连接内部?

As an extension to this; if the above code does keep an active connection open; would using the singleton pattern to store an instance of the ServiceBusClient for each instance of the API be dangerous? Or does the Azure SDK handle closed connections internally?

推荐答案

从连接管理的角度来看,下面可能会有所帮助:

From a connection management perspective, following may be helpful:

1)对MessagingFactory实例创建一个单独的连接

1) A single connection is created for a MessagingFactory instance

2)你可以从一个单一的MessagingFactory创造尽可能多QueueClient实例,但它们都将使用相同​​的实际TCP连接,所以考虑吞吐量和可用性在这里,因为更多的连接可以帮助

2) You can create as many QueueClient instances from a single MessagingFactory but they will all use the same actual underlying TCP connection, so consider throughput and availability here because more connections can help

3)我们将重新连接的情况下,一个MessagingFactory被丢弃底层连接

3) We will reconnect the underlying connection for a MessagingFactory in case it is dropped

4)从正在创建到特定队列连接的QueueClient的角度来看,认为这些链接(其中的几个可以存在通过单个MessagingFactory连接)。同样,如果连接中断,将被重新创建,因此将成为队列中的链接,你并不需要重新创建的对象。

4) From a QueueClient perspective you are creating connections to a particular Queue, consider these links (several of these can exist over a single MessagingFactory connection). Again if the connection is dropped it will be recreated and so will be the links for the Queues, you do not need to recreate the objects

5),100个并发连接的配额是强加在每个队列的基础所以基本上你可以有多达100个链接到一个队列

5) The Quota of 100 concurrent connections is imposed on a per Queue basis so essentially you can have up to 100 links to a single Queue

6)这些是不是你在创建

6) These are not impacted by how many MessagingFactory instances you create these links over

7)如果你调用close /配置上MessagingFactory则所有经该连接的链接将被关闭过,也不会重新连接

7) If you call close/dispose on the MessagingFactory then all the links over that connection will be closed too and will not reconnect

这篇关于重用在Azure中服务总线连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆