是否可以通过编程方式扩展Azure应用服务 [英] Is it possible to scale Azure app services programmatically

查看:72
本文介绍了是否可以通过编程方式扩展Azure应用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种以编程方式放大/缩小Azure应用服务实例的方法.几个月前曾有人问过这个问题(例如,在下面的链接中),当时无法给出答案,所以我只是想知道事情是否有所改变和/或现在有一些新功能可用.

I am looking for a way to programmatically scale up/down an Azure app service instance. This question has been asked before (e.g. in the link below) several months back and the answer was not possible at the time, so I am just wondering if something has changed and/or some new feature is available now.

可以通过编程方式扩展Azure实例吗?

请注意,我们希望在"Azure应用程序服务"上执行此操作,而不是在旧风格的"Azure云服务"上执行此操作.

Please note that we would like to do this on "Azure app services", not the old style "Azure cloud services".

我们希望通过编程方式进行缩放的原因是,我们可以使用自定义指标对其进行控制.我们没有找到将自定义指标发布到Azure的方法,然后Azure可以使用它来进行自动缩放.

The reason that we would like to do the scaling programmatically is so that we can control it using on our custom metrics. We did not find a way to publish our custom metrics to Azure and it can then be used by Azure to do the autoscaling either.

我们确实发现Azure自动缩放规则可以接受Azure存储队列长度,因此从理论上讲,我们可以通过在队列中添加/删除消息来控制队列长度,但这是一种破解,并且仅当队列是在传统的Azure Web门户中创建的,而不是在新的Azure门户中创建的.

We did find that the Azure autoscale rule can accept an Azure storage queue length, so theoretically we can control the queue length by adding/removing messages to the queue, but it is kind of a hack and also it only works if the queue is created in the classic Azure web portal, not the new Azure portal.

推荐答案

是否可以通过编程方式扩展Azure应用服务

Is it possible to scale Azure app services programmatically

是的,我们可以使用 REST API 或SDK. 我使用提琴手测试REST API,详细信息请参考快照,有关如何获得授权的信息,请参考

Yes, we can do that use REST API or SDK. I test the REST API using the fiddler, details please refer to the snapshot, for how to get the authorization, please refer to the document.

标题信息:

身体信息:

如果可以使用C#代码,请尝试使用
Microsoft.Azure.Management.WebSites 来扩展Azure应用程序服务.有关SDK的更多详细信息,请参阅packages.config文件. 如何注册Azure AD应用程序以及如何获取应用程序ID,secretKey和tenantId,请参考

If C# code is possible, please have a try to use
Microsoft.Azure.Management.WebSites to scale Azure app services. More detail info about SDK please refer to the packages.config file. How to registry Azure AD App and how to get Application ID, secretKey and tenantId please refer to the document. The following is the demo code.

  var subscriptionId = "Your subscrption";
  var appId = "Registried Azure Application Id";
  var secretKey = "Secret Key";
  var tenantId = "tenant Id";
  var resourceGroup = "resource group name";
  var servicePlanName = "service plan name";
  var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
  ClientCredential clientCredential = new ClientCredential(appId, secretKey);
  var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
  var accessToken = tokenResponse.AccessToken;
  TokenCredentials credential = new TokenCredentials(accessToken);
  var webSiteManagementClient = new Microsoft.Azure.Management.WebSites.WebSiteManagementClient(credential);
  webSiteManagementClient.SubscriptionId = subscriptionId;
  var servicePlan = webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup).Result.Body.Where(x=>x.Name.Equals(servicePlanName)).FirstOrDefault();
   //scale up/down
    servicePlan.Sku.Family = "P"; 
    servicePlan.Sku.Name = "P1";
    servicePlan.Sku.Size = "P1";
    servicePlan.Sku.Tier = "Premium";
    servicePlan.Sku.Capacity = 2; // scale out: number of instances 
   var updateResult = webSiteManagementClient.AppServicePlans.CreateOrUpdateWithHttpMessagesAsync(resourceGroup, servicePlanName, servicePlan).Result;

packages.config文件:

packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.Management.Websites" version="1.6.0-preview" targetFramework="net462" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net462" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net462" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.5" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net462" />
</packages>

从门户检查结果.

注意:如果更新了Azure服务计划,它将应用于服务计划中的所有WebApp.

Note: If the Azure Service plan is updated, it will apply to all of WebApps in the Service plan.

这篇关于是否可以通过编程方式扩展Azure应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆