服务运行时是否有任何地方可以告诉我我当前在“登台”或“生产”上运行?手动修改与生产的配置似乎有些麻烦。

有帮助吗?

解决方案

如果您在产品中或舞台上,则实际上不应更改配置。分期区域并非设计为“ QA”环境,而是在部署生产之前的持有区域。

当您上传新的部署时,将包裹上传到的当前部署插槽将被销毁,并在上传和开始VM的情况下将其下载10-15分钟。如果您直接将其上传到生产中,那就是生产停机时间15分钟。因此,发明了分期区域:您上传到分期,测试内容,然后单击“交换”按钮,而您的分期环境神奇地变成生产(虚拟IP交换)。因此,您的分期应该与您的生产相同。

我认为您正在寻找质量检查/测试环境?您应该为自己的产品/分期开放一项新服务,以测试环境。在这种情况下,您将需要维护多个配置文件集,每个部署环境(生产,测试等)一组

有很多方法可以管理发生的配置 - 孔,尤其是在.config文件(其自己的 *.cscfg文件)上具有的Azure。我更喜欢使用Azure项目进行的方式如下:设置一个小型配置项目,在那里创建匹配部署类型的文件夹。在每个文件夹设置集中, *.config& *.cscfg文件与特定部署环境匹配:调试,测试,发布...这些也是Visual Studio中的设置,也是构建目标类型。我有一个小的Xcopy命令,该命令在配置项目的每个编译中都会发生,该命令将配置项目的构建目标文件夹复制到配置项目的根文件夹中。

然后,解决方案中的每个其他项目,从配置项目的根文件夹链接到.config或.cscfg文件。

瞧,我的配置会神奇地自动适应每个构建配置。我还使用.config转换来管理发行版与非释放构建目标的调试信息。

如果您阅读了所有这些,仍然想了解 生产与运行时的分期状态, ,然后:获取 deploymentIdRoleEnvironment.DeploymentId然后使用适当的管理API X509 certificate 到达 Azure structure of your Service 并致电 GetDeployments 方法(是REST API,但有一个抽象库)。

希望这可以帮助

编辑:根据要求的博客文章有关配置字符串的设置以及在环境之间切换 @ http://blog.paraleap.com/blog/post/managing-environments-in-a-a-distributed-azure-ozure-or-other-cloud-net-net-solution

其他提示

有时我希望人们只能回答这个问题..不要解释道德或最佳实践...

Microsoft发布了一个代码示例,在此处进行此操作: https://code.msdn.microsoft.com/windowsazure/csazuredeploymentslot-1ce0e3b5

image showing Staging instance

image showing Production instance

protected void Page_Load(object sender, EventArgs e) 
{ 
    // You basic information of the Deployment of Azure application. 
    string deploymentId = RoleEnvironment.DeploymentId; 
    string subscriptionID = "<Your subscription ID>"; 
    string thrumbnail = "<Your certificate thumbnail print>"; 
    string hostedServiceName = "<Your hosted service name>"; 
    string productionString = string.Format(
        "https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}",
        subscriptionID, hostedServiceName, "Production"); 
    Uri requestUri = new Uri(productionString); 

    // Add client certificate. 
    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
    store.Open(OpenFlags.OpenExistingOnly); 
    X509Certificate2Collection collection = store.Certificates.Find(
        X509FindType.FindByThumbprint, thrumbnail, false); 
    store.Close(); 

    if (collection.Count != 0) 
    { 
        X509Certificate2 certificate = collection[0]; 
        HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(requestUri); 
        httpRequest.ClientCertificates.Add(certificate); 
        httpRequest.Headers.Add("x-ms-version", "2011-10-01"); 
        httpRequest.KeepAlive = false; 
        HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;

        // Get response stream from Management API. 
        Stream stream = httpResponse.GetResponseStream(); 
        string result = string.Empty; 
        using (StreamReader reader = new StreamReader(stream)) 
        { 
            result = reader.ReadToEnd();
        } 
        if (result == null || result.Trim() == string.Empty) 
        {
            return;
        }
        XDocument document = XDocument.Parse(result); 
        string serverID = string.Empty; 
        var list = from item
                   in document.Descendants(XName.Get("PrivateID",
                       "http://schemas.microsoft.com/windowsazure")) 
                   select item; 

        serverID = list.First().Value; 
        Response.Write("Check Production: "); 
        Response.Write("DeploymentID : " + deploymentId
            + " ServerID :" + serverID); 
        if (deploymentId.Equals(serverID)) 
            lbStatus.Text = "Production"; 
        else 
        { 
            // If the application not in Production slot, try to check Staging slot. 
            string stagingString = string.Format(
                "https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}",
                subscriptionID, hostedServiceName, "Staging"); 
            Uri stagingUri = new Uri(stagingString); 
            httpRequest = (HttpWebRequest)HttpWebRequest.Create(stagingUri); 
            httpRequest.ClientCertificates.Add(certificate); 
            httpRequest.Headers.Add("x-ms-version", "2011-10-01"); 
            httpRequest.KeepAlive = false; 
            httpResponse = httpRequest.GetResponse() as HttpWebResponse; 
            stream = httpResponse.GetResponseStream(); 
            result = string.Empty; 
            using (StreamReader reader = new StreamReader(stream)) 
            { 
                result = reader.ReadToEnd();
            } 
            if (result == null || result.Trim() == string.Empty) 
            {
                return;
            }
            document = XDocument.Parse(result); 
            serverID = string.Empty; 
            list = from item
                   in document.Descendants(XName.Get("PrivateID",
                       "http://schemas.microsoft.com/windowsazure")) 
                   select item; 

            serverID = list.First().Value; 
            Response.Write(" Check Staging:"); 
            Response.Write(" DeploymentID : " + deploymentId
                + " ServerID :" + serverID); 
            if (deploymentId.Equals(serverID)) 
            {
                lbStatus.Text = "Staging";
            }
            else 
            {
                lbStatus.Text = "Do not find this id";
            }
        } 
        httpResponse.Close(); 
        stream.Close(); 
    } 
}

分期是一个临时部署插槽,主要用于无需升级和回滚升级的能力。

建议不要将您的系统(在代码或配置中)与此类Azure细节相结合。

自从 Windows Azure管理库 感谢@guaravmantri 回答 对于另一个问题,您可以这样做:

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Management.Compute;
using Microsoft.WindowsAzure.Management.Compute.Models;

namespace Configuration
{
    public class DeploymentSlotTypeHelper
    {
        static string subscriptionId = "<subscription-id>";
        static string managementCertContents = "<Base64 Encoded Management Certificate String from Publish Setting File>";// copy-paste it
        static string cloudServiceName = "<your cloud service name>"; // lowercase
        static string ns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration";

        public DeploymentSlot GetSlotType()
        {
            var managementCertificate = new X509Certificate2(Convert.FromBase64String(managementCertContents));
            var credentials = new CertificateCloudCredentials(subscriptionId, managementCertificate);

            var computeManagementClient = new ComputeManagementClient(credentials);
            var response = computeManagementClient.HostedServices.GetDetailed(cloudServiceName);
            return response.Deployments.FirstOrDefault(d => d.DeploymentSlot == DeploymentSlot.Production) == null ? DeploymentSlot.Staging : DeploymentSlot.Production;
        }
    }
}

解决此问题的一种简单方法是在您的实例上设置一个关键,以识别它正在运行的环境。

1)在您的生产插槽中设置:设置IT设置>>应用程序设置>>应用程序设置,并创建一个名为slot_name和值“生产”的键。重要:检查插槽设置。

2)在您的登台插槽中设置:设置IT设置>>应用程序设置>>应用程序设置,并创建一个名为slot_name和值“ staging”的键。重要:检查插槽设置。

从您的应用程序访问变量并确定应用程序正在运行的环境。在Java中,您可以访问:

String slotName = System.getenv("APPSETTING_SLOT_NAME");

这里有4分要考虑

  1. VIP交换只有在您的服务面对外界时才有意义。又名,当它暴露API并对请求做出反应时。
  2. 如果您的服务所做的就是从队列中拉消息并处理它们,那么您的服务是主动的,而VIP交换对您来说不是一个好的解决方案。
  3. 如果您的服务既具有反应性又积极主动,则可能需要重新考虑您的设计。也许将服务分为两种不同的服务。
  4. 如果您的服务的主动部分可能需要短暂的时间(因为您首先配置分期和生产以不拉消息,然后执行VIP交换,然后执行VIP交换,则ERIC修改CSCFG文件前和VIP交换的建议是好的,如果您首先配置分期和生产,然后更新生产的配置以开始拉消息)。
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top