【问题标题】:Azure Queue to receive http requestsAzure 队列接收 http 请求
【发布时间】:2017-06-19 07:12:10
【问题描述】:

我想知道是否可以创建一个 Azure 队列(服务总线或存储队列),它可以放置在 Web 应用程序的前面,并且可以第一时间接收 http 请求。

更新

感谢cmets和答案。

我想在不增加 IIS 负担的情况下处理请求。我需要在队列中的请求到达 IIS 之前对其进行处理。

【问题讨论】:

  • 按顺序处理每个请求?如果您在 OWIN 之上构建您的 Web 应用程序,您可以注册一个执行您想要的中间件。
  • 您能详细说明您的问题吗?目前尚不清楚您要完成什么。

标签: azure azure-servicebus-queues azure-queues azure-storage-queues


【解决方案1】:

如果有可能创建一个 Azure 队列(服务总线或存储队列),它可以放置在 Web 应用程序的前面并立即接收 http 请求。

我们可以通过添加一些代码在 Azure Web App 中处理请求之前将请求消息保存到队列中。我编写了一个 C# 版本的示例代码,它将请求消息记录到 Azure 存储队列。以下步骤供您参考。

步骤 1. 将 http 模块添加到您的项目中。在这个模块中,我注册了 HttpApplication 的 BeginRequest 事件并进行了消息记录工作。

public class RequestToQueueModeule : IHttpModule
{
    #region IHttpModule Members

    public void Dispose()
    {
        //clean-up code here.
    }

    public void Init(HttpApplication context)
    {
        // Below is an example of how you can handle LogRequest event and provide 
        // custom logging implementation for it
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }

    #endregion

    public void OnBeginRequest(Object source, EventArgs e)
    {
        HttpApplication context = source as HttpApplication;
        AddMessageToQueue(context.Request);
    }


    public void AddMessageToQueue(HttpRequest request)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(request.HttpMethod + " " + request.RawUrl + " " + request.ServerVariables["SERVER_PROTOCOL"]);
        for (int i = 0; i < request.Headers.Count; i++)
        {
            sb.AppendLine(request.Headers.Keys[i] + ":" + request.Headers[i]);
        }
        sb.AppendLine();
        if (request.InputStream != null)
        {
            using (StreamReader sr = new StreamReader(request.InputStream))
            {
                sb.Append(sr.ReadToEnd());
            }
        }

        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string of your azure storage");

        // Create the queue client.
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

        // Retrieve a reference to a queue.
        CloudQueue queue = queueClient.GetQueueReference("queue name which is used to store the request message");

        // Create the queue if it doesn't already exist.
        queue.CreateIfNotExists();

        // Create a message and add it to the queue.
        CloudQueueMessage message = new CloudQueueMessage(sb.ToString());
        queue.AddMessage(message);
    }
}

Step 2. 在web.config的system.webServer节点注册上层模块。请修改您的模块放置的命名空间名称。

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="RequestToQueueModeule" type="[your namespace name].RequestToQueueModeule" />
  </modules>
</system.webServer>

我想在不增加 IIS 负担的情况下处理请求。我需要在队列中的请求到达 IIS 之前对其进行处理。

如果要在请求到达 IIS 之前处理队列中的请求,则需要在 Azure Web App 前面添加代理。 Azure 应用程序网关用作代理,它可以放在 Web 应用程序的前面。如果您只想记录 HTT 请求的主要信息,您可以使用 Azure 应用程序网关并打开访问日志。欲了解更多信息,下面的链接供您参考。

Diagnostic logs of Application Gateway

如果你想保存所有的请求消息,恐怕你需要构建一个自定义代理并自己记录请求。

【讨论】:

  • 感谢您的回答。我想要这样一个解决方案的原因是我们有一个应用程序,我们的客户可以使用它来跟踪他们的物品(数据包)。它多次进行数据挖掘,我们不想使用recaptcha。通过这个解决方案,我想区分 ogranic 和 non-organic 请求(它们是包含正确 ItemId 但来自机器人的合法请求)并将它们驱动到两个不同的队列中请求。
  • 是否在 URL 中传递了 ItemId?如果是,您可以使用支持基于路径的路由的应用程序网关。如果 ItemId 在 HTTP 标头中传递,则没有可以为您路由它的 Azure 服务。也许你可以创建虚拟机并使用一些反向代理来做到这一点。例如,Nginx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多