Wednesday, December 8, 2021

Creating Dapr Enabled Azure Container Apps

Azure Container Apps has native support for Dapr. That means we don't have to deploy Dapr runtime along with our images and have very straightforward integration.

In this post, let's see how we can create Dapr enabled services in Azure Container Apps and do service invocations. If you are new to Azure Container Apps, make sure to read this post: Public Preview: Azure Container Apps, which will help you get up to speed.

I have created 3 Azure Container Apps for the following components.

  • A Web App
  • Customers API
  • Orders API

Web App is public-facing (ingress mode is external) and the two APIs are internal (ingress mode is internal). Web App is consuming Customers API and Orders API.

I can create Dapr enabled Container Apps by running the following script (replacing variables and for APIs ingress mode is internal).

az containerapp create `
   --name $WEB_APP_NAME `
   --resource-group $RESOURCE_GROUP `
   --image `
   --target-port 80 `
   --ingress 'external' `
   --min-replicas 1 `
   --max-replicas 1 `
   --enable-dapr `
   --dapr-app-port 80 `
   --dapr-app-id $WEB_APP_SERVICE_ID `
   --dapr-components ./dapr/components.yaml

And then, in the Web App, appsettings.json, I have the Service App IDs defined. These are what you use for --dapr-app-id argument.

  "Services": {
    "Customers": {
}, "Orders": { "ServiceId""orders-api"
} } }

And finally I can easily consume APIs/Services from WebApp, something like below for an example.

using ContainerAppsDemo.Web.Blazor.Data;
using Dapr.Client;
namespace ContainerAppsDemo.Web.Blazor.Services;

public class CustomerService : ICustomerService
    private readonly DaprClient _daprClient;
    private readonly string _serviceId;
    public CustomerService(DaprClient daprClient, IConfiguration configuration)
        _daprClient = daprClient;
        _serviceId = configuration.GetValue<string>("Services:Customers:ServiceId");     }     public async Task<IEnumerable<Customer>> GetCustomers(CancellationToken cancellationToken = default)     {         return await _daprClient.InvokeMethodAsync<List<Customer>>(HttpMethod.Get,
            cancellationToken);     } }
Dapr Enabled Container Apps

Isn't that easy?

You can find the source code for this post here,

Hope this helps.

Happy Coding.


No comments:

Post a Comment