首页 文章

Azure Service Bus队列发送消息的速度很慢

提问于
浏览
1

所以我正在尝试从node.js向Azure服务总线队列发送消息我正在使用Azure文档中描述的样板代码 .

var message = {
body: 'Test message',
customProperties: {
    testproperty: 'TestValue'
}};
serviceBusService.sendQueueMessage('myqueue', message, function(error){
if(!error){
    // message sent
}});

当我每隔几秒钟发送一条消息时,它工作正常,我能看到我的工作人员在一秒左右后收到消息,但是一旦我开始发送更多消息(每秒5-10个),它看起来像它开始窒息,我看到性能下降,每隔5-8秒发送一条消息 . 有没有办法提高性能并保持FIFO排序?这可能是一个延迟问题,因为我在本地运行我的节点代码和我的工作人员?

注意:我正在使用基本的非分区队列 .

1 回答

  • 1

    为了优化Service Bus的发送性能,如果您的应用程序支持异步发送和完成操作,我们可以利用客户端批处理 . 您可以获得官方文章https://azure.microsoft.com/en-us/documentation/articles/service-bus-performance-improvements/#client-side-batching的详细信息 .

    但是,使用批量发送消息的客户端仅集成在.Net SDK中,在Node.js中我们应该通过REST API实现自定义请求https://msdn.microsoft.com/en-us/library/azure/dn798894.aspx .

    要通过REST API实现自定义请求,我们需要首先在Azure门户上创建身份验证凭据:

    • 转到Azure Managment Portal,在左侧菜单中选择"SERVICE BUS" .

    • 选择您创建的命名空间 .

    • 在顶部菜单上选择QUEUES .

    • 选择队列以转到详细信息屏幕

    • 转到配置选项卡,然后转到"Shared access policies"部分

    • 创建新策略,输入名称并选择"Send"发送消息的权限

    • 按页面底部的SAVE按钮保存

    • 转到"Shared access key generator"部分

    • 选择您的策略并复制PRIMARY KEY

    • 保存此密钥,稍后您将使用它

    然后,您可以利用此代码示例生成用于发送批处理消息的SAS令牌,https://github.com/dx-ted-emea/azure-tessel/blob/master/labs/service-bus-queues/CreateSASToken.js

    这是代码片段:

    var https = require('https');
    var crypto = require('crypto');
    // ServiceBus parameters
    var namespace = '<Your-ServiceBus-Namespace>';
    var queue ='<Your-Queue>';
    var AccessKeyName = '<Your-AccessKey-Name>';
    var AccessKey = '<Your-AccessKey>';
    // Full ServiceBus Queue publisher URI
    
    var ServiceBusUri = 'https://' + namespace + '.servicebus.windows.net' + '/' + queue;
    function createSASToken(uri, keyName, key) {
                    var expiry = parseInt(Date.now()/1000)+3600;
                    var signedString = encodeURIComponent(uri) + '\n' + expiry;
                    var hmac = crypto.createHmac('sha256', key);
                    hmac.update(signedString);
                    var signature = hmac.digest('base64');
                    var token = 'SharedAccessSignature sr=' + encodeURIComponent(uri) + '&sig=' + encodeURIComponent(signature) + '&se=' + expiry + '&skn=' + keyName;
    
                    return token;
    }
    
    var createdSASToken = createSASToken(ServiceBusUri, AccessKeyName, AccessKey);
    var options = {
                    hostname: namespace + '.' + 'servicebus.Windows.net',
                    port: 443,
                    path: '/' + queue + '/messages',
                    method: 'POST',
                    headers: {
                                    'Authorization': createdSASToken,
                                    'Content-Type': 'application/vnd.microsoft.servicebus.json',
                    }
    };
    var req = https.request(options, function(res) {
                    console.log("SendMessageInQueue:statusCode: ", res.statusCode);
                    res.setEncoding('utf8');
                    res.on('data', function(d) {
                    });
    });
    req.on('error', function(e) {
                    console.error(e);
    });
    var messages = [{
                    "Body": "This is the first message"
    }, {
                    "Body": "This is the second message"
    }, {
                    "Body": "This is the third message"
    }];
    req.write(JSON.stringify(messages));
    req.end();
    

    此解决方案基于https://github.com/dx-ted-emea/azure-tessel/tree/master/labs/service-bus-queues .

相关问题