将数据存储在 Azure WebJob 中

问题描述 投票:0回答:1

我创建了一个从服务器接收数据的 gRPC 客户端。 客户端作为 Azure WebJob 运行。

收到的数据看起来每条消息都有一个重播id。 我目前将此重播 ID 作为文件存储在本地。收到的重播 ID 会附加到文件中。 需要重播 ID,以便在出现问题时可以从该 ID 继续。

如果我在本地运行客户端,这不是问题,但我认为作为 WebJob 并不理想。 我目前的问题是,在运行时创建/修改的文件存储在这里:

C:\local\Temp\jobs\continuous\SalesforceIngestor.App\ev4ygzfw.ezh

如果WebJob因任何原因重新启动,replayIdStorage.txt文件中的数据将会丢失。

有什么方法可以安全地将数据存储在 WebJob 中,以便重启后可以检索数据吗?

或者 Azure Blob 存储是更好的选择吗?

c# .net azure azure-blob-storage azure-webjobs
1个回答
0
投票

我尝试了下面的.Net Web Job代码,并且能够将Web应用程序的Web Jobs数据存储在我的存储帐户容器中。

代码:

程序.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace WebJob1
{
    internal class Program
    {
        static void Main()
        {
            var config = new JobHostConfiguration();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            host.RunAndBlock();
        }
    }
}

函数.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;

namespace WebJob1
{
    public class Functions
    {
        private const string ConnectionStringName = "AzureWebJobsStorage"; 

        public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
        {
            log.WriteLine(message);
            string replayId = GenerateReplayIdFromMessage(message);
            StoreReplayIdInBlobStorage(replayId);
        }

        private static string GenerateReplayIdFromMessage(string message)
        {
            return Guid.NewGuid().ToString();
        }

        private static void StoreReplayIdInBlobStorage(string replayId)
        {
            var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable(ConnectionStringName));
            var blobClient = storageAccount.CreateCloudBlobClient();
            var containerName = "replayids"; 
            var container = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists();

            var blobName = "replayIdStorage.txt"; 
            var blob = container.GetBlockBlobReference(blobName);
            blob.UploadText(replayId);
        }
    }
}

应用程序配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <!-- The format of the connection string is "DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY" -->
        <!-- For local execution, the value can be set either in this config file or through environment variables -->
        <add name="AzureWebJobsDashboard" connectionString="<storage_connec_string>" />
        <add name="AzureWebJobsStorage" connectionString="<storage_connec_string>" />
    </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

输出:

运行成功如下,

enter image description here

我在 Azure 门户的存储帐户中创建了两个容器,如下所示,

enter image description here

我在 azure-webjobs-hosts 容器中获得了 Id,如下所示,

enter image description here

并在 azure-jobs-host-output 容器中,

enter image description here

enter image description here

然后,我发布我的代码到网络应用程序,如下,

enter image description here

它已成功发布Azure Portalweb应用程序中的web作业。然后,我单击 Run 开始运行 Web 作业,如下所示,

enter image description here

Web Job running成功启动如下,

enter image description here

enter image description here

然后,我在我的存储帐户azure-jobs-host-output容器中获得了web应用程序详细信息存储,如下所示,

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.