Implementing an Event Driven Architecture using Azure Event Grid
If we abstract the requirements of an event driven architecture, we can distill it down to five basic concepts:
- the “Topic”, or data entity, about which an event is concerned and its associated endpoint
- the “Event” itself, i.e. what has happened to that Topic
- the “Event Source” – the service where the event happened
- the “Event Subscriptions” – the registration of an endpoint to which to route a specific event
- the “Event Handlers” – the services that have registered an interest in this specific event via a subscription and carry out some business function when they receive the event.
Abstracting the previous example of an Order service shows us how the concrete objects fit into these categories:
Azure provides us with Out of the Box services to implement this event based architecture. The Event Grid Topic and the Event Grid Topic Subscription.
Let’s use these services to build an example Event Broker:
In the Azure Portal, first create a new resource group to hold the example objects. Let’s call it EventBroker_RG.
Now add a new Event Grid Topic named Order as shown
Now we need to create a service with an endpoint to subscribe to this topic. There is an example service on Github that we can use for this purpose. It’s at:
https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json
We can deploy this using Azure CLI.
resourcegroupname=EventBroker_RG
sitename=<YourSiteName>
az deployment group create \
–resource-group $resourcegroupname \
–template-uri “https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json” \
–parameters siteName=$sitename hostingPlanName=viewerhost
When the deployment completes, if you now browse to the newly created site at https://<YourSiteName>.azurewebsites.net/ , you can see there are no events in the queue.
Now we can add a subscription for the Order Topic to this site. We can do this from the portal:
Or do the same thing via the Azure CLI:
endpoint=https://<YourSiteName>.azurewebsites.net/api/updates
subscription=358cdd05-be24-4346-9c33-ee89bb2eba46
az eventgrid event-subscription create \
–source-resource-id “/subscriptions/$subscription/resourceGroups/$resourcegroupname/providers/Microsoft.EventGrid/topics/$topicname” –name demoViewerSub –endpoint $endpoint
In either case, if we now browse back to the endpoint site we will see a subscription validation event
We now have a valid Topic and a service which is subscribing to it. To test it let’s trigger an event to see the Event Grid send the event notifcation to our endpoint. First, let’s get the URL and key for the custom topic using the Azure CLI.
endpoint=$(az eventgrid topic show –name $topicname -g $resourcegroupname –query “endpoint” –output tsv)
key=$(az eventgrid topic key list –name $topicname -g $resourcegroupname –query “key1” –output tsv)
Now let’s create a fake data packet for the message. Ideally this would be provided by an actual Order service, but for now, we’ll fake it using the CLI:
event='[ {“id”: “‘”$RANDOM”‘”, “eventType”: “recordInserted”, “subject”: “fakesourceapp/orders/plants”, “eventTime”: “‘date +%Y-%m-%dT%H:%M:%S%z
‘”, “data”:{ “genus”: “Rosa”, “species”: “Floribunda”},”dataVersion”: “1.0”} ]’
And finally let’s use CURL in the CLI to send it to our event grid:
curl -X POST -H “aeg-sas-key: $key” -d “$event” $endpoint
Now browse back to our endpoint app and see the newly raised event:
So, we have successfully created and Event grid topic, created and Event Handler, Added a subcription between the two and generated an event. That’s four of the five main concepts demonstrated. We still need an event source, but that can be picked up in the next post which will cover multiple sources and handlers as would be the case in a real production system.
All examples and code by Roland Kamsika
0 Comments