简体   繁体   English

Azure,云服务,可通过.NET进行扩展

[英]Azure, Cloud Service, scale with .NET

How can I use the Microsoft.Azure.Management.Compute to change instances number on Cloud Service ? 如何使用Microsoft.Azure.Management.Compute更改Cloud Service上的实例号? Or any other .NET way to do it ? 还是其他任何.NET方式?

Not really an answer to your question (as I have not used this library) but I will try to give you a general idea (in the hope that it helps you in finding the right methods in the library). 并不是您问题的真正答案(因为我没有使用过该库),但我会尽力为您提供一个总体思路(希望它可以帮助您在库中找到正确的方法)。

As you may already know that the number of instances for a Cloud Service Role is stored in the configuration file (*.cscfg) which is an XML file. 您可能已经知道,云服务角色的实例数存储在配置文件(* .cscfg)中,该文件是XML文件。 Scaling a Cloud Service involves changing this configuration file. 扩展Cloud Service涉及更改此配置文件。

What you would need to do is first get this configuration file. 您需要做的是首先获取此配置文件。 Once you get this configuration file, you would create an XML document out of it by reading the contents of the file. 获得此配置文件后,您可以通过读取文件内容来从中创建XML文档。

Next you need to find <Instances> node in this XML document for the desired role that you wish to scale. 接下来,您需要在该XML文档中找到您想要扩展的所需角色的<Instances>节点。 You would update the value of count attribute of this node to the desired instance count. 您可以将此节点的count属性的值更新为所需的实例计数。

Once you do that, you would need to apply this changed configuration. 完成此操作后,您将需要应用此更改的配置。 REST API operation that gets called is Change Deployment Configuration . 调用的REST API操作是“ Change Deployment Configuration This will initiate scaling of your Cloud Service. 这将启动您的Cloud Service的扩展。

Microsoft.Azure.Management.Compute client library is for Virtual Machines under Azure Resource Manager. Microsoft.Azure.Management.Compute客户端库适用于Azure资源管理器下的虚拟机 For managing Azure Cloud Services, we could use this Management Libraries for .NET(Microsoft.WindowsAzure.Management.Compute) . 为了管理Azure云服务,我们可以使用此.NET(Microsoft.WindowsAzure.Management.Compute)管理库

The following sample code works fine on my side, please refer to it. 以下示例代码对我而言效果很好,请参考它。

    static void Main(string[] args)
    {
        var managementCertThumbprint = "{CertThumbprint}";
        var subscriptionid = "{subscriptionid}";
        var servicename = "fehancloud";

        X509Certificate2 managementCert = FindX509Certificate(managementCertThumbprint);

        SubscriptionCloudCredentials creds = new CertificateCloudCredentials(subscriptionid, managementCert);
        ComputeManagementClient cmclient = new ComputeManagementClient(creds);

        var detailed = cmclient.HostedServices.GetDetailed(servicename);

        var deployment = detailed.Deployments
            .First(_ => _.DeploymentSlot == DeploymentSlot.Production);

        var xConfig = XDocument.Parse(deployment.Configuration);

        Func<string, XName> n = (name) => XName.Get(name,
            "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration");

        Func<XDocument, string, int> getInstanceCount = (doc, rolename) =>
        {
            var role = doc.Root.Elements(n("Role")).FirstOrDefault(_ => _.Attribute("name").Value == rolename);
            if (role == null) return -1;
            var v = role.Element(n("Instances")).Attribute("count").Value;
            Console.WriteLine(int.Parse(v));
            return int.Parse(v);
        };

        Action<XDocument, string, int> setInstanceCount = (doc, rolename, newInstanceCount) =>
        {
            if (newInstanceCount < 1)
            {
                newInstanceCount = 1;
            }

            var role = doc.Root.Elements(n("Role")).FirstOrDefault(_ => _.Attribute("name").Value == rolename);
            role.Element(n("Instances")).Attribute("count").Value = newInstanceCount.ToString();
        };

        Action<XDocument, string, int> changeInstanceCount = (doc, rolename, deltaCount) =>
        {
            int oldCount = getInstanceCount(doc, rolename);
            var newCount = oldCount + deltaCount;
            setInstanceCount(doc, rolename, newCount);
        };

        changeInstanceCount(xConfig, "WorkerRole1", 2);

        var response = cmclient.Deployments.ChangeConfigurationBySlot(
            serviceName: detailed.ServiceName,
            deploymentSlot: deployment.DeploymentSlot,
            parameters: new DeploymentChangeConfigurationParameters()
            {
                Configuration = xConfig.ToString()
            });
    }

    private static X509Certificate2 FindX509Certificate(string managementCertThumbprint)
    {
        X509Store store = null;

        try
        {
            store = new X509Store(StoreName.My, StoreLocation.CurrentUser); //set StoreLocation based on your specified value 
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            X509Certificate2Collection certs = store.Certificates.Find(
                findType: X509FindType.FindByThumbprint,
                findValue: managementCertThumbprint,
                validOnly: false);
            if (certs.Count == 0)
            {
                return null;
            }

            return certs[0];
        }
        finally
        {
            if (store != null) store.Close();
        }
    }

Note : I uploaded a management certificate. 注意 :我上传了管理证书。

packages.config packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
  <package id="Microsoft.Azure.Common" version="2.0.4" targetFramework="net45" />
  <package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
  <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
  <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net45" />
  <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
  <package id="Microsoft.WindowsAzure.Management.Compute" version="13.0.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>

After it executed changeInstanceCount method , you could see new instances are starting. 在执行changeInstanceCount方法后 ,您可以看到新实例正在启动。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM