Azure Function as a Web API 性能和定价

jon*_*hin 9 azure azure-active-directory azure-functions

我们正在计划构建一个 Web 应用程序,我希望有人可以帮助我们决定是使用 Azure App Service 还是 Azure Function 来为客户端提供 rest API。我们的要求如下。

  1. 认证和授权
  2. Azure SQL 和 Cosmos DB 上的 CRUD
  3. 多区域
  4. 每月 100,000,000 次 API 调用

起初,我们打算使用 Azure 应用服务构建后端。但是在研究了 Azure Functions 的优缺点后,Azure Functions 对我们变得更具吸引力。

那么,构建一个依赖 Azure Functions 作为 REST API 提供者的 Web 应用程序是否是一个好主意?

有没有人有作为 REST API 提供商构建、管理和扩展和扩展 Azure Functions 的经验?

Md *_*ron 5

构建一个依赖 Azure Functions 作为 REST API 提供程序的 Web 应用程序是否是个好主意?

您似乎计划使用 Web 服务或 Azure 函数来拥有 REST API。我会说你的决定是完美的。对于 Azure 函数,并不强制要求为此提供 Web 服务。Azure 函数将是您的最佳选择。您可以实现 Web API 提供的所有功能。因此,如果您的目标只是开发 API,那么您可以从 Azure Function 开始,别无选择。它的杰出实际上!

有没有人有作为 REST API 提供商构建、管理和扩展和扩展 Azure Functions 的经验?

我正在为我们的 AI Base Bot 与 LUIS 集成使用 Azure Function。据我所知,它非常易于维护,响应时间最快,您可以从任何地方构建它。因此,您无疑可以使用 Azure 功能。

为什么选择 Azure 函数:

  1. 它是无状态的,不需要任何服务器来运行
  2. 完整的 REST,可以从任何地方任何区域调用
  3. 可以同时开发 Azure Portal 和本地 Visual Studio
  4. 具有成本效益,您只需支付使用量即可。
  5. 多语言支持
  6. 简单的授权和认证功能
  7. 根据您的消费计划没有通话限制

使用 Azure 函数做很多事情:

可以使用 Azure 函数开发强大的 API 服务。它有许多突出的特点。请检查检查这里

授权和认证:

您可以简单地将您的授权和身份验证集成到您的功能应用程序中。甚至您可以在每个功能上单独或在完整应用程序上实现它。它支持大多数流行的身份验证提供程序,例如:

  1. Azure 活动目录
  2. 微软身份
  3. 护目镜
  4. Facebook
  5. 推特认证

了解如何实施身份验证:

第1步

在此处输入图片说明

第2步

在此处输入图片说明

休息功能代码示例:

在这里我给你一个简单的代码片段开始:虽然它是在 Azure 表存储上,但帮助你开发 azure 函数和 CRUD 概念。

你的样本类:

  public class YourSampleClass
    {

        public string PartitionKey { get; set; }
        public string RowKey { get; set; }

    }
Run Code Online (Sandbox Code Playgroud)

表存储类:

 public class TableStorageClass
    {
        public TableStorageClass()
        {

        }
        public TableStorageClass(DynamicTableEntity entity)
        {
            PartitionKey = entity.PartitionKey;
            RowKey = entity.RowKey;

        }

        public string PartitionKey { get; set; }
        public string RowKey { get; set; }


    }
Run Code Online (Sandbox Code Playgroud)

Azure 函数 V2 示例:

public static class FunctionReadFromTableStorage
    {
        [FunctionName("FunctionReadFromTableStorage")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //Read Request Body
            var content = await new StreamReader(req.Body).ReadToEndAsync();

            //Extract Request Body and Parse To Class
            YourSampleClass objYourSampleClass = JsonConvert.DeserializeObject<YourSampleClass>(content);

            // Validate param because PartitionKey and RowKey is required to read from Table storage In this case , so I am checking here.
            dynamic validationMessage;

            if (string.IsNullOrEmpty(objYourSampleClass.PartitionKey))
            {
                validationMessage = new OkObjectResult("PartitionKey is required!");
                return (IActionResult)validationMessage;
            }
            if (string.IsNullOrEmpty(objYourSampleClass.RowKey))
            {
                validationMessage = new OkObjectResult("RowKey is required!");
                return (IActionResult)validationMessage;
            }


            // Table Storage operation  with credentials
            var client = new CloudTableClient(new Uri("https://YourStorageURL.table.core.windows.net/"),
                      new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("YourStorageName", "xtaguZokAWbfYG4QDkBjT+YourStorageKey+T/kId/Ng+cl3TfYHtg=="));
            var table = client.GetTableReference("YourTableName");

            //Query filter
            var query = new TableQuery()
            {
                FilterString = string.Format("PartitionKey eq '{0}' and RowKey eq '{1}'", objYourSampleClass.PartitionKey, objYourSampleClass.RowKey)
            };


            //Request for storage query with query filter
            var continuationToken = new TableContinuationToken();
            var storageTableQueryResults = new List<TableStorageClass>();
            foreach (var entity in table.ExecuteQuerySegmentedAsync(query, continuationToken).GetAwaiter().GetResult().Results)
            {
                var request = new TableStorageClass(entity);
                storageTableQueryResults.Add(request);
            }

            //As we have to return IAction Type So converting to IAction Class Using OkObjectResult We Even Can Use OkResult
            var result = new OkObjectResult(storageTableQueryResults);
            return (IActionResult)result;
        }
    }
Run Code Online (Sandbox Code Playgroud)

要记住的要点:

  1. Azure Portal执行的情况下,只需摆脱FunctionReadFromTableStorage课堂
  2. 您需要以下参考来执行上面的代码
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
Run Code Online (Sandbox Code Playgroud)

邮递员请求模式:

函数调用示例:

{
   "PartitionKey": "Your Param According to Table Storage Design" ,
   "RowKey": "Your Param According to Table Storage Design",
   "Directory": "Your Param According to Table Storage Design"
}
Run Code Online (Sandbox Code Playgroud)

请参阅屏幕截图:

在此处输入图片说明

邮递员回复:

响应以我自己的表设计为准

[
    {
        "partitionKey": "Microsoft SharePoint Server",
        "rowKey": "2016"
    }
]
Run Code Online (Sandbox Code Playgroud)

请参阅下面的屏幕截图:

在此处输入图片说明

注意:对于CosmosDb 集成,您可以在此处查看带函数的 Azure SQL这里