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内容:

  

  

  

 

分类: Azure Storage

本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/archive/2012/06/11/2542076.html,如需转载请自行联系原作者

来源:https://yq.aliyun.com/articles/382878


智能推荐

Windows Azure Storage (6) Windows Azure Storage之Table

 《Windows Azure Platform 系列文章目录》     最近想了想,还是有必要把Windows Azure Table Storage 给说清楚。     1.概念   Windows Azure Table是存储在云端的非关系型数据表,主要存储结构化数据,简单理解上来是类似SQL Server的一张单表,包含了列名和行数据,但是无法执行关系型运算(...

Windows Azure Storage论文解读

Windows Azure Storage论文解读 整体架构 文件流层 stream layer 存储引擎 存储优化 分区层 Partition Layer 存储引擎 WAS总结 WAS是微软开发的云存储系统,提供Blob、Table、Queue三种类型的服务,它广泛部署于微软内部。其论文发表于2011年SOSP。 整体架构 WAS主要分为两个部分:定位服务(Location Service LS...

Windows Azure Cloud Service (10) Role的生命周期

《Windows Azure Platform 系列文章目录》     在上一章内容中,我们提到了Windows Azure会依次调用角色(Role)实例的OnStart()方法和Run()方法。   在本节中我们会对Role的生存周期进行进一步的了解。首先,让我们走到幕后看看一个Role Instance是怎么被发布到虚拟机上并启动起来的。      Role在虚拟机上部署和运行的过程...

Windows Azure Storage (22) Azure Storage如何支持多级目录

 《Windows Azure Platform 系列文章目录》     熟悉Azure平台的读者都知道,Azure Blob有三层架构。如下图:(注意blob.core.chinacloudapi.net是Azure China的Service Endpoint)      1.Blob Name: 存储账号名称   2.Container Name,容器名称。概念上类似于文件夹   ...

Windows Azure Storage (24) 启用Azure Blob日志

  《Windows Azure Platform 系列文章目录》     之前有一个业务需求,客户想知道Azure Storage是否有日志功能,可以检查某个Azure Blob文件在某个时间点被删除。   后来问了相关的同事,其实在Azure ARM Portal里面,可以开启诊断功能,监控具体的操作日志。这里写个Blog记录一下。     1.我们登录Azure ARM ...

猜你喜欢

使用Windows Azure Storage Analytics新特性

自昨天其发布以来,我就开始使用这个新特性,用Windows Azure存储日志分析数据。简而言之,这个分析让你记录有关存储账户(blob、表和队列)调用的细节(或多或少满足你的需要),包括详细的日志(API调用时)和总体指标(例如消耗多少存储空间以及在过去一小时内完成的请求数量)。 由于我正在学习这些新功能,我将我的实验结果放在网上,网址是http://storageanalytics.cloud...

如何更有价值采集数据、高效分析数据?

上回说到,用户行为数据的意义和价值《为什么要进行用户行为分析?》,以及互联网产品用户模型的构建,这其中就包含了对数据的采集和分析两大块儿,本文将从数据采集的三大要点、如何让分析更有价值更高效、以及数据分析思维三部分展开聊。 一、数据采集的三大要点 1、全面性 数据量足够具有分析价值、数据面足够支撑分析需求。 比如对于“查看商品详情”这一行为,需要采集用户触发时的环境信息、会...

shell脚本编程基础(三)

结构化命令(一) if-then和case语句。 If-then-else语句 当if语句中的命令返回非零退出状态码时, bash shell会执行else部分中的命令。 嵌套if-then语句的问题在于代码不易阅读,很难理清逻辑流程。 可以使用else部分的另一种形式:elif。这样就不用再书写多个if-then语句了。 elif使 用另一个if-then语句延续else部分。 elif语句行提...

IBM主机:知天命后再出发

                    第1页:市场究竟还需不需要主机? 第2页:z13改进了什么?第3页:z13还需要改进什么?               ...

ADC基础知识小结

1.过采样和降采样区别          内插(过采样,零填充)输入信号Fin<fs/2,可实现更宽Nyquist域;        抽取(降采样,LPF)输入信号Fin>fs/2,采样率低于Nyquist采样率,会发生混叠,一般在ADC输入之前加抗混叠滤波器。生活中车轮转的很快时眼睛的采样率不够...

问答精选

URL for a user content site and SEO

I was thinking about how i should write my URLs. I want them to A) Be user friendly B) SEO C) allow fast DB queries. The information i have are username, category, mediaId, title and other data i dont...

How to use the Clojure -> macro with an inner function

I'm a Clojure beginner and I want to understand the -> macro This code works: But this doesn't even compile and I don't know how to deal with the error message: CompilerException java.lang.IllegalA...

Java Program to make Tic Tac Toe not working

For my programming class I'm supposed to make a program that simulates a game of tic tac toe. My teacher provided all the methods and said we shouldn't need to add any or take any away, and told us we...

How can i exit the for statement in assembly

The purpose of this code is to flash the bits turned on three times, exit the loop and turn them off. Currently the code seems to be in an infinite loop and does not exit the loop after the count is 0...

Simple Camel test fails with no messages recieved

Am using Spring Boot and I have just added camel to it. I have a simple camel route setup : When I try to create simple test for this route with : It fails with Not sure what could be a problem here. ...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答