استخدم مخطط CloudEvents v1.0 مع شبكة الأحداث

بالإضافة إلى مخطط الحدث الافتراضيالخاص به، تدعم Azure Event Grid الأحداث في تنفيذ JSON لـ CloudEvents v1.0 وHTTP ربط البروتوكول. CloudEventsمواصفة مفتوحة لوصف بيانات الحدث.

تعمل CloudEvents على تبسيط إمكانية التشغيل المتداخل من خلال توفير مخطط حدث شائع لنشر الأحداث المستندة إلى مجموعة النظراء واستهلاكها. يسمح هذا المخطط لأدوات موحدة، وطرق قياسية لتوجيه ومعالجة الأحداث وطرق عالمية من deserializing مخطط الحدث الخارجي. مع مخطط مشترك، يمكنك دمج العمل بسهولة أكبر عبر الأنظمة الأساسية.

يتم بناء CloudEvents من قبل العديد من المتعاونين، بما في ذلك Microsoft، من خلال Cloud Native Computing Foundation. وهو متوفرة حالياً بإصدار 1.0.

توضح هذه المقالة كيفية استخدام مخطط CloudEvents مع شبكة الأحداث.

مخطط CloudEvent

فيما يلي مثال لحدث تخزين Azure Blob بتنسيق CloudEvents:

{
    "specversion": "1.0",
    "type": "Microsoft.Storage.BlobCreated",  
    "source": "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}",
    "id": "9aeb0fdf-c01e-0131-0922-9eb54906e209",
    "time": "2019-11-18T15:13:39.4589254Z",
    "subject": "blobServices/default/containers/{storage-container}/blobs/{new-file}",
    "dataschema": "#",
    "data": {
        "api": "PutBlockList",
        "clientRequestId": "4c5dd7fb-2c48-4a27-bb30-5361b5de920a",
        "requestId": "9aeb0fdf-c01e-0131-0922-9eb549000000",
        "eTag": "0x8D76C39E4407333",
        "contentType": "image/png",
        "contentLength": 30699,
        "blobType": "BlockBlob",
        "url": "https://gridtesting.blob.core.windows.net/testcontainer/{new-file}",
        "sequencer": "000000000000000000000000000099240000000000c41c18",
        "storageDiagnostics": {
            "batchId": "681fe319-3006-00a8-0022-9e7cde000000"
        }
    }
}

للحصول على وصف مفصل للحقول المتوفرة وأنواعها وتعريفاتها، راجع CloudEvents v1.0.

قيم العناوين للأحداث تسليمها في مخطط CloudEvents ومخطط شبكة الأحداث هي نفسها باستثناء content-type. بالنسبة إلى مخطط CloudEvents، تكون "content-type":"application/cloudevents+json; charset=utf-8" قيمة الرأس هذه. بالنسبة إلى مخطط شبكة الأحداث، تكون "content-type":"application/json; charset=utf-8" قيمة الرأس هذه.

تكوين CloudEvents

يمكنك استخدام "شبكة الأحداث" لإدخال الأحداث وإخراجها في مخطط CloudEvents. يصف الجدول التالي التحويلات المحتملة:

مورد شبكة الأحداث مخطط الإدخال مخطط التسليم
مواضيع النظام مخطط شبكة الأحداث مخطط شبكة الأحداث أو مخطط CloudEvents
المواضيع/المجالات المخصصة مخطط شبكة الأحداث مخطط شبكة الأحداث أو مخطط CloudEvents
المواضيع/المجالات المخصصة مخطط CloudEvents مخطط CloudEvents
المواضيع/المجالات المخصصة مخطط مخصص مخطط مخصص أو مخطط شبكة الأحداث أو مخطط CloudEvents
مواضيع الشركاء مخطط CloudEvents مخطط CloudEvents

بالنسبة إلى كافة مخططات الأحداث، تتطلب شبكة الأحداث التحقق من الصحة عندما تقوم بالنشر لموضوع "شبكة الأحداث" وعند إنشاء اشتراك حدث.

لمزيد من المعلومات، راجع أمان Event Grid والمصادقة عليها.

مخطط الإدخال

تعيين مخطط الإدخال لموضوع مخصص عند إنشاء الموضوع المخصص.

بالنسبة إلى Azure CLI، استخدم:

az eventgrid topic create --name demotopic -l westcentralus -g gridResourceGroup --input-schema cloudeventschemav1_0

بالنسبة إلى PowerShell، استخدم:

New-AzEventGridTopic -ResourceGroupName gridResourceGroup -Location westcentralus -Name demotopic -InputSchema CloudEventSchemaV1_0

مخطط الإخراج

تعيين مخطط الإخراج عند إنشاء اشتراك الحدث.

بالنسبة إلى Azure CLI، استخدم:

topicID=$(az eventgrid topic show --name demotopic -g gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create --name demotopicsub --source-resource-id $topicID --endpoint <endpoint_URL> --event-delivery-schema cloudeventschemav1_0

بالنسبة إلى PowerShell، استخدم:

$topicid = (Get-AzEventGridTopic -ResourceGroupName gridResourceGroup -Name <topic-name>).Id

New-AzEventGridSubscription -ResourceId $topicid -EventSubscriptionName <event_subscription_name> -Endpoint <endpoint_URL> -DeliverySchema CloudEventSchemaV1_0

التحقق من صحة نقطة النهاية باستخدام CloudEvents الإصدار رقم 1.0

إذا كنت معتاداً على Event Grid بالفعل، فقد تكون على دراية بتأكيد الاتصال للتحقق من صحة نقطة النهاية لمنع إساءة الاستخدام. تطبق CloudEvents v1.0 الخاصة به استخدام دلالات الحماية باستخدام أسلوب خيارات HTTP. لقراءة المزيد حول هذا الموضوع، راجع HTTP 1.1 Hooks ويب لتسليم الحدث - الإصدار 1.0. عند استخدام مخطط CloudEvents للإخراج، يستخدم شبكة الأحداث حماية إساءة استخدام CloudEvents v1.0 بدلاً من آلية الحدث التحقق من صحة شبكة الأحداث.

استخدام مع وظائف Azure

Visual Studio أو Visual Studio Code

إذا كنت تستخدم Visual Studio أو Visual Studio Code ولغة برمجة C# لتطوير الوظائف، فتأكد من استخدام أحدث حزمة Microsoft.Azure.WebJobs.Extensions.EventGrid NuGet (الإصدار 3.3.1 أو أعلى).

في Visual Studio، استخدم Tools ->NuGet مدير الحِزَم ->مدير الحِزَم Console، وقم بتشغيل Install-Package الأمر (Install-Package Microsoft.Azure.WebJobs.Extensions.EventGrid -Version 3.3.1). بدلا من ذلك، انقر بزر الماوس الأيمن فوق المشروع في نافذة مستكشف الحلول، وحدد إدارة قائمة حزم NuGet للاستعراض بحثا عن حزمة NuGet، وتثبيتها أو تحديثها إلى أحدث إصدار.

في VS Code، قم بتحديث رقم الإصدار لحزمة Microsoft.Azure.WebJobs.Extensions.EventGrid في ملف csproj لمشروع Azure Functions.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventGrid" Version="3.3.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

يظهر المثال التالي دالة Azure Functions الإصدار 3.x التي تم تطويرها إما في Visual Studio أو Visual Studio Code. يستخدم معلمة CloudEvent ربط و EventGridTrigger.

using Azure.Messaging;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public static class CloudEventTriggerFunction
    {
        [FunctionName("CloudEventTriggerFunction")]
        public static void Run(ILogger logger, [EventGridTrigger] CloudEvent e)
        {
            logger.LogInformation("Event received {type} {subject}", e.Type, e.Subject);
        }
    }
}

تجربة تطوير مدخل Azure

إذا كنت تستخدم مدخل Microsoft Azure لتطوير دالة Azure، فاتبع الخطوات التالية:

  1. قم بتحديث اسم المعلمة في function.json الملف إلى cloudEvent.

    {
      "bindings": [
        {
          "type": "eventGridTrigger",
          "name": "cloudEvent",
          "direction": "in"
        }
      ]
    }    
    
  2. run.csx تحديث الملف كما هو موضح في نموذج التعليمات البرمجية التالي.

    #r "Azure.Core"
    
    using Azure.Messaging;
    
    public static void Run(CloudEvent cloudEvent, ILogger logger)
    {
        logger.LogInformation("Event received {type} {subject}", cloudEvent.Type, cloudEvent.Subject);
    }
    

إشعار

لمزيد من المعلومات، راجع مشغل شبكة حدث Azure لدوال Azure.

الخطوات التالية