65.9K
CodeProject 正在变化。 阅读更多。
Home

运行第一个 Azure Queue 程序的 7 个简单步骤

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (7投票s)

2010年2月11日

CPOL

6分钟阅读

viewsIcon

53378

downloadIcon

612

运行第一个 Azure Queue 程序的 7 个简单步骤。

运行第一个 Azure Queue 程序的 7 个简单步骤

引言

Azure 队列有助于 Web 角色和工作角色之间进行数据通信。Web 角色就是可以由最终浏览器访问的 Web 应用程序。工作角色是在 Azure 系统中运行的后台进程,它们不直接接收来自 Web 的请求。

因此,如果我们想将数据传递给工作角色,就需要使用队列。队列是临时存储,充当 Web 角色和工作角色之间的桥梁。因此,您可以将数据发布到 Web 角色,Web 角色将数据发布到队列,然后工作角色从队列中获取数据进行进一步处理。

您还可以观看这些实际操作的循序渐进视频:学习 Azure学习 Azure DevOps

本文将做什么?

在本文中,我们将创建一个简单的应用程序,允许我们输入客户反馈。这些客户反馈消息将经过质量检查,以确定消息是否包含任何垃圾邮件关键词。目前,我们只检查“XXX”。

换句话说,上述 Web 角色会将反馈消息提交到队列,工作角色会获取消息,进行质量检查,如果质量检查通过,就会将其添加到 Azure 表中。如果您不熟悉如何创建 Azure 表,请在此处阅读。

步骤 1:确保一切就绪

如果您是 Azure 的完全新手,请确保您已具备所有先决条件。您可以阅读这篇文章以获取基本先决条件。

步骤 2:创建 Web 角色和工作角色项目

下一步是选择云服务模板,添加 Web 角色项目并创建您的解决方案。

步骤 3:配置服务定义和配置文件

下一步是在服务定义和配置中定义表存储和队列存储位置。为了复习,服务定义文件帮助您定义配置名称,而服务配置实际上设置了值。

以下是服务定义文件的样子,我们需要帐户名称、共享密钥、队列存储和表存储终结点。我们需要为工作角色和 Web 角色都定义这两个定义。

我们不确定是否有办法避免这种冗余。我们搜索了一下,发现即使名称相同,我们也需要定义不同的定义。此链接讨论了同样的问题。

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="QueueStorage"
	xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole1" enableNativeCodeExecution="false">
<InputEndpoints>
<!-- Must use port 80 for http and port 443 for https when running in the cloud -->
<InputEndpoint name="HttpIn" protocol="http" port="80" />
</InputEndpoints>
<ConfigurationSettings>
<Setting name="AccountName" />
<Setting name="AccountSharedKey" />
<Setting name="QueueStorageEndpoint" />
<Setting name="TableStorageEndpoint" />
</ConfigurationSettings>
</WebRole>
<WorkerRole name="WorkerRole1" enableNativeCodeExecution="false">
<ConfigurationSettings>
<Setting name="AccountName" />
<Setting name="AccountSharedKey" />
<Setting name="QueueStorageEndpoint" />
<Setting name="TableStorageEndpoint" />
</ConfigurationSettings>
</WorkerRole>
</ServiceDefinition>

定义完成后,就可以为这些定义设置配置值了。以下是服务配置文件,其中包含定义文件中定义的所有 4 个定义的设置。您可以看到,存储和队列的终结点都是使用 URL 定义的,换句话说,这些存储是通过 REST 服务公开的。

目前所有 4 个配置都指向本地开发存储。当您想要在实际 Azure 平台中在线托管时,这些值将发生变化。

<?xml version="1.0"?>
<ServiceConfiguration serviceName="QueueStorage"
    xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="WebRole1">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="AccountName" value="devstoreaccount1" />
<Setting name="AccountSharedKey"
value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/
	K1SZFPTOtr/KBHBeksoGMGw==" />
<Setting name="QueueStorageEndpoint" value="http://127.0.0.1:10001" />
<Setting name="TableStorageEndpoint" value="http://127.0.0.1:10002" />
</ConfigurationSettings>
</Role>
<Role name="WorkerRole1">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="AccountName" value="devstoreaccount1" />
<Setting name="AccountSharedKey"
value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/
	K1SZFPTOtr/KBHBeksoGMGw==" />
<Setting name="QueueStorageEndpoint" value="http://127.0.0.1:10001" />
<Setting name="TableStorageEndpoint" value="http://127.0.0.1:10002" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>

步骤 4:添加 CustomerInfo 实体类

下一步是创建包含客户姓名和消息的实体类。因此,下面是一个名为 `CustomerInfo` 的简单实体类,它继承自 `TableStorageEntity`。此类具有两个属性 - `CustomerName` 和 `Message`。

public class CustomerInfo :
Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
{
public CustomerInfo()
{
this.PartitionKey = "CustomerInfos";
this.RowKey = DateTime.Now.Ticks.ToString();
}
public string CustomerName { get; set; }
public string Message { get; set; }
}

步骤 5:添加 CustomerInfoDataContext 类

我们需要创建另一个类,即数据上下文类。此类负责添加上述定义的客户实体类。因此,第一步是从 `TableStorageDataServiceContext` 类继承,如下面的代码片段所示。

public class CustomerInfoDataContext : TableStorageDataServiceContext
{}

在同一个类中,我们创建了一个 `initializetable` 方法,它将创建由 `CustomerInfo` 类定义的表结构。以下是带有注释的代码片段说明。

public void InitializeTable()
{
// Get access to the account of table storage end point
StorageAccountInfo accountInfo =
    StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint",true);

// Create the table using the ‘TableStorage’ class.
// TableStorage.CreateTablesFromModel(typeof(CustomerInfoDataContext), accountInfo);
}

我们还需要公开 `IQueryable` 接口,该接口公开 `CustomerInfo` 类,Azure 系统可以使用它来创建 Azure 表结构。

public IQueryable<CustomerInfo> CustomerInfos
{
get
{
return this.CreateQuery<CustomerInfo>("CustomerInfos");
}
}

我们还公开了一个 `AddCustomer` 方法,该方法接受 `CustomerInfo` 对象。然后使用 `TableStorageDataServiceContext` 的 `AddObject` 方法将此对象添加到 Azure 表中,最后,为了将其提交到表,我们调用 `SaveChanges` 方法。

public void AddCustomer(CustomerInfo _objCustomer)
{
this.AddObject("CustomerInfos", _objCustomer);
this.SaveChanges();
}

将客户实体添加到表后,我们希望显示它。因此,我们公开了一个 `GetCustomerInfo` 函数,该函数被类型化为列表,并且可以绑定到 `gridview`。

public List<CustomerInfo> GetCustomerInfo()
{
return this.CustomerInfos.ToList();
}

步骤 6:创建 Web 角色以添加客户消息

下一步是创建一个 Web 角色项目,其中包含一个 ASPX UI 页面,如下所示。您可以看到 UI 中有一个 `gridview` 源,可用于显示消息,以及两个用于客户姓名和反馈的文本框。

提交消息按钮将消息添加到队列,而获取旧反馈消息则从表中显示消息。

为了将客户和消息添加到队列,第一步是使用队列的配置来获取 `StorageAccountInfo` 对象。

StorageAccountInfo accountQueueInfo =
	StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();

使用此帐户队列信息来创建存储。

QueueStorage qs = QueueStorage.Create(accountQueueInfo);

使用 `Queuestorage` 类获取客户队列。

MessageQueue queue = qs.GetQueue("customer");

如果队列不存在,则使用 `DoesQueueExist` 方法创建客户队列。

if (!queue.DoesQueueExist())
queue.CreateQueue();

通过连接两个文本框来创建消息对象,并将其放入队列。

Message msg = new Message(TextBox1.Text+"#"+TextBox3.Text);
queue.PutMessage(msg);

我们在 Web 角色屏幕上还有第二个按钮,用于显示添加到客户表中的数据。因此,以下代码将帮助您从 Azure 表中显示数据。
获取 `CustomerInfoDataContext` 类。

CustomerInfoDataContext objCustomerDataContext = new CustomerInfoDataContext();

使用先前步骤中讨论的 `GetCustomerInfo` 函数来获取 `CustomerInfo` 对象列表。

List<CustomerInfo> _objCustomer = objCustomerDataContext.GetCustomerInfo();

最后将客户对象与 `gridview` 绑定。

if (_objCustomer.Count > 0)
{
GridView1.DataSource = _objCustomer;
GridView1.DataBind();
}

步骤 7:创建工作角色以读取消息

在第 7 步中,我们看到了 Web 角色如何将数据插入队列。现在队列由工作角色读取,消息被解析并搜索“xxx”。如果未找到,它将将其添加到表中。

因此,第一步是获取如下面的代码片段所示的队列对象。

StorageAccountInfo accountQueueInfo =
	StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();
QueueStorage qs = QueueStorage.Create(accountQueueInfo);
MessageQueue queue = qs.GetQueue("customer");

在工作角色中,将有一个无限循环,它将不断运行并使用我们从上一步获得的队列对象轮询消息。

while (true)
{
Message msg = queue.GetMessage();
if (msg != null)
{
// Parse XXX data here
}
}

在 `while` 循环中,我们获取消息并检查“XXX”,如下面的代码片段所示。如果我们找不到“XXX”,我们将其添加为实体到表中,使用下面的代码片段所示的数据上下文对象。

string message = msg.ContentAsString();
string name = message.Substring(0, message.IndexOf("#"));
string msgUser = message.Substring(message.IndexOf("#") + 1,
		message.Length - message.IndexOf("#") - 1);
if (!msgUser.Contains("xxx"))
{
CustomerInfo _obj = new CustomerInfo();
CustomerInfoDataContext objCustomerDataContext = new CustomerInfoDataContext();
_obj.CustomerName = name; _obj.Message = msgUser;
objCustomerDataContext.InitializeTable();
objCustomerDataContext.AddCustomer(_obj);
}

queue.DeleteMessage(msg);

现在我们可以运行程序了。为了确保所有表都已创建,请右键单击 Visual Studio 项目并选择“创建测试存储表”,如下所示。

享受您的辛勤工作。如果您现在在反馈消息中添加“XXX”,它不会被添加到表中,而是被工作角色拒绝。如果您添加普通消息,它将显示在 Web 角色项目的网格视图源中。

历史

  • 2010 年 2 月 11 日:初始发布

如需进一步阅读,请观看以下面试准备和分步视频系列。

© . All rights reserved.