zoukankan      html  css  js  c++  java
  • Windows Azure Storage (10) Windows Azure 上的托管服务CDN (下) Hosted Service

      《Windows Azure Platform 系列文章目录

      使用Blob Service CDN需要将所有需要缓存的文件放入Blob Service中,然后设置CDN指向这个Storage Service。但是大部分情况下,特别是在进行网站开发的情况下,使用的图片一般都是放在网站目录下而非Blob Service中。虽然可以将这些文件迁移到Blob中,但是在开发的时候还是需要使用本地文件,实际操作起来还是比较费时费力的。另外,基于Blob Service的CDN只能缓存静态文件,如果应用程序需要缓存一些动态的内容,比如缓存某个页面的输出该怎么办呢?为了解决上述问题,Windows Azure平台提供了基于Hosted Service的CDN服务。

      我将会给大家介绍如何使用Windows Azure平台上的Hosted Service。

      首先,我们打开Visual Studio 2012,新建一个Cloud Solution,命名为AzureCDN。添加一个ASP.NET的Web Role。

      然后在Web Role Project下增加,添加一个名为"CDN"的文件夹(大小写不敏感)。在这个文件夹里我们先增加1张图片Moon,然后增加文件夹msft,在msft文件夹里增加3张图片。

      请注意:Windows Azure Hosted CDN服务将使“/cdn”文件夹下所有内容启用CDN。我们必须把所有需要CDN服务的内容(图片,css,文档等)复制到cdn文件夹里。

      然后我们打开项目中的Default.aspx页面。添加2个image控件:分别为imgBlob和imgCDN。图片的来源分别指向Azure Storage Blob中和CDN Url。

      

      然后我们在Default.aspx.cs的Page_Load函数里,增加如下代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.WindowsAzure.ServiceRuntime;
    
    namespace WebRole1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                imgBlob.ImageUrl = RoleEnvironment.GetConfigurationSettingValue("imgBlobURL");
                imgCDN.ImageUrl = RoleEnvironment.GetConfigurationSettingValue("imgCDNURL");
            }
        }
    }

      主要的功能是:从ServiceConfiguration.cscfg里读取相关的图片超链接信息,然后加载到imgBlob和imgCDN控件。

      然后我们修改WebRole.cs中的代码,具体内容如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.Diagnostics;
    using Microsoft.WindowsAzure.ServiceRuntime;
    using System.Diagnostics;
    
    namespace WebRole1
    {
        public class WebRole : RoleEntryPoint
        {
            public override bool OnStart()
            {
                // For information on handling configuration changes
                // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
    
                // 当用配置文件中ConfigurationSettings时必须调用CloudStorageAccount.SetConfigurationSettingPublisher
                // 来说明当配置文件在发布后被更改时将采取何种操作
                CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                {
                    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
                    RoleEnvironment.Changed += (sender, arg) =>
                    {
                        if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
    
                            .Any((change) => (change.ConfigurationSettingName == configName)))
                        {
                            if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
                            {
                                RoleEnvironment.RequestRecycle();
                            }
                        }
                    };
                });
                RoleEnvironment.Changing += RoleEnvironmentChanging;
    
                return base.OnStart();
            }
    
            private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
            {
                // If a configuration setting is changing
                if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
                {
                    // Set e.Cancel to true to restart this role instance
                    e.Cancel = false;
                }
            }
    
    
        }
    }

      这些代码的主要功能是:如果CSCFG发生变化的时候将e.Cancel设置为False,不需要重启Web Role。

      然后我们修改CSCFG的配置信息,增加imgCDNURL和imgBlobURL,Value分别是图片的http website。我使用上一章已经上传成功的图片WindowsAzure.png。

      注意:如果对读取Azure配置文件不熟悉的网友,请参考我的这篇文章Windows Azure Platform (十四) Configuration的变更和通知机制

      

      然后我们使用Visual Studio发布这个Azure Solution,服务名称和Url都使用LeiAzureCDN,WebSite的发布过程略。

      发布成功后,我们浏览发布成功的Azure Website:http://leiazurecdn.cloudapp.net/。图片加载成功,效果如下:

      实际上加载的2张图片都是来自于Azure Blob, url为 http://threestone.blob.core.windows.net/gallery/WindowsAzure.png,我们暂时还未使用Azure Hosted Service CDN服务。

      那我们开始配置Azure Hosted Service CDN的功能。首先登陆Windows Azure Management Portal:

      然后我们选择"CDN"-->选中"LeiAzureCDN"(我们之前创建的托管站点)-->选择"新建终结点":

      在弹出的"新建CDN终结点里",

    1.启用CDN:启用或不启用CDN。

    2.HTTPS:如果我们需要使用HTTPS连接,选中它。

    3.查询字符串:如果我们缓存托管服务内容或使用查询字符串来指定被恢复的内容,选中它。

      CDN新建终结点成功后如下图。请注意观察LeiAzureCDN的CDN HTTP终结点地址是http://az274005.vo.msecnd.net

      等待20分钟......让CDN在全世界传播

      

      (20分钟后...)我们使用已经发布成功的LeiAzureCDN的HTTP终结点是:http://az274005.vo.msecnd.net

      请注意观察AzureCDN这个Cloud Solution Web Role根目录下的CDN的文件夹内容:

    我们修改AzureCDN的配置部署,修改imgCDNURL的value为CDN URL,我们修改加载CDN\msft\wp7.jpg的图片

     然后我们用IE重新打开http://leiazurecdn.cloudapp.net/,页面加载的时候会重新读取最新的cscfg内容:

      

      

      

  • 相关阅读:
    Linux踩坑填坑记录
    Scala安装后,在IDEA中配置
    Centos 搭建Hadoop
    conductor FAQ
    conductor Workflow Metrics
    conductor APIs
    Extending Conductor
    conductor任务域
    Conductor Task Workers
    Conductor Server
  • 原文地址:https://www.cnblogs.com/threestone/p/2542076.html
Copyright © 2011-2022 走看看