什么是云服务模型以及如何将其打包?What is the Cloud Service model and how do I package it?

云服务由以下三个组件创建:服务定义 (.csdef)、服务配置 (.cscfg) 和服务包 (.cspkg)。A cloud service is created from three components, the service definition (.csdef), the service config (.cscfg), and a service package (.cspkg). ServiceDefinition.csdefServiceConfig.cscfg 文件都基于 XML,同时介绍云服务的结构及其配置方式;统称为模型。Both the ServiceDefinition.csdef and ServiceConfig.cscfg files are XML-based and describe the structure of the cloud service and how it's configured; collectively called the model. ServicePackage.cspkg 是基于 ServiceDefinition.csdef 和其他文件生成的 zip 文件,它包含所有必需的基于二进制的依赖项。The ServicePackage.cspkg is a zip file that is generated from the ServiceDefinition.csdef and among other things, contains all the required binary-based dependencies. Azure 可从 ServicePackage.cspkg 和 ServiceConfig.cscfg 两者创建云服务。Azure creates a cloud service from both the ServicePackage.cspkg and the ServiceConfig.cscfg.

云服务在 Azure 中开始运行后,可以通 ServiceConfig.cscfg 文件重新进行配置,但不能更改定义。Once the cloud service is running in Azure, you can reconfigure it through the ServiceConfig.cscfg file, but you cannot alter the definition.

想了解哪方面的详细信息?What would you like to know more about?

ServiceDefinition.csdefServiceDefinition.csdef

ServiceDefinition.csdef 文件指定 Azure 用于配置云服务的设置。The ServiceDefinition.csdef file specifies the settings that are used by Azure to configure a cloud service. Azure Service Definition Schema (.csdef File)(Azure 服务定义架构(.csdef 文件))为服务定义文件提供允许的格式。The Azure Service Definition Schema (.csdef File) provides the allowable format for a service definition file. 以下示例显示了可为 Web 角色和辅助角色定义的设置:The following example shows the settings that can be defined for the Web and Worker roles:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyServiceName" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole1" vmsize="Medium">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="HttpIn" endpointName="HttpIn" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="HttpIn" protocol="http" port="80" />
      <InternalEndpoint name="InternalHttpIn" protocol="http" />
    </Endpoints>
    <Certificates>
      <Certificate name="Certificate1" storeLocation="LocalMachine" storeName="My" />
    </Certificates>
    <Imports>
      <Import moduleName="Connect" />
      <Import moduleName="Diagnostics" />
      <Import moduleName="RemoteAccess" />
      <Import moduleName="RemoteForwarder" />
    </Imports>
    <LocalResources>
      <LocalStorage name="localStoreOne" sizeInMB="10" />
      <LocalStorage name="localStoreTwo" sizeInMB="10" cleanOnRoleRecycle="false" />
    </LocalResources>
    <Startup>
      <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple" />
    </Startup>
  </WebRole>

  <WorkerRole name="WorkerRole1">
    <ConfigurationSettings>
      <Setting name="DiagnosticsConnectionString" />
    </ConfigurationSettings>
    <Imports>
      <Import moduleName="RemoteAccess" />
      <Import moduleName="RemoteForwarder" />
    </Imports>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="tcp" port="10000" />
      <InternalEndpoint name="Endpoint2" protocol="tcp" />
    </Endpoints>
  </WorkerRole>
</ServiceDefinition>

可以参考 服务定义架构 更好地了解此处使用的 XML 架构,而以下是部分元素的快速说明:You can refer to the Service Definition Schema for a better understanding of the XML schema used here, however, here is a quick explanation of some of the elements:

SitesSites
包含 IIS7 中承载的网站或 Web 应用程序的定义。Contains the definitions for websites or web applications that are hosted in IIS7.

InputEndpointsInputEndpoints
包含用于联系云服务的终结点的定义。Contains the definitions for endpoints that are used to contact the cloud service.

InternalEndpointsInternalEndpoints
包含角色实例用于相互通信的终结点的定义。Contains the definitions for endpoints that are used by role instances to communicate with each other.

ConfigurationSettingsConfigurationSettings
包含特定角色功能的设置定义。Contains the setting definitions for features of a specific role.

CertificatesCertificates
包含角色所需证书的定义。Contains the definitions for certificates that are needed for a role. 前面的代码示例显示了用于 Azure Connect 的配置的证书。The previous code example shows a certificate that is used for the configuration of Azure Connect.

LocalResourcesLocalResources
包含本地存储资源的定义。Contains the definitions for local storage resources. 本地存储资源是角色实例运行于的虚拟机的文件系统中的保留目录。A local storage resource is a reserved directory on the file system of the virtual machine in which an instance of a role is running.

ImportsImports
包含已导入模块的定义。Contains the definitions for imported modules. 前面的代码示例显示了远程桌面连接和 Azure Connect 的模块。The previous code example shows the modules for Remote Desktop Connection and Azure Connect.

StartupStartup
包含在角色启动时运行的任务。Contains tasks that are run when the role starts. 任务在 .cmd 文件或可执行文件中定义。The tasks are defined in a .cmd or executable file.

ServiceConfiguration.cscfgServiceConfiguration.cscfg

云服务设置配置由 ServiceConfiguration.cscfg 文件中的值确定。The configuration of the settings for your cloud service is determined by the values in the ServiceConfiguration.cscfg file. 指定要为此文件中每个角色部署的实例数。You specify the number of instances that you want to deploy for each role in this file. 在服务定义文件中定义的配置设置值已添加到服务配置文件中。The values for the configuration settings that you defined in the service definition file are added to the service configuration file. 与云服务相关联的所有管理证书的指纹也会添加到该文件中。The thumbprints for any management certificates that are associated with the cloud service are also added to the file. Azure Service Configuration Schema (.cscfg File)(Azure 服务配置架构(.cscfg 文件))为服务配置文件提供允许的格式。The Azure Service Configuration Schema (.cscfg File) provides the allowable format for a service configuration file.

服务配置文件不与应用程序一起打包,而是作为单独的文件上传到 Azure 并用于配置云服务。The service configuration file is not packaged with the application, but is uploaded to Azure as a separate file and is used to configure the cloud service. 无需重新部署云服务即可上传新的服务配置文件。You can upload a new service configuration file without redeploying your cloud service. 云服务正在运行时可以更改云服务的配置值。The configuration values for the cloud service can be changed while the cloud service is running. 以下示例显示了可为 Web 角色和辅助角色定义的配置设置:The following example shows the configuration settings that can be defined for the Web and Worker roles:

<?xml version="1.0"?>
<ServiceConfiguration serviceName="MyServiceName" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
  <Role name="WebRole1">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="SettingName" value="SettingValue" />
    </ConfigurationSettings>

    <Certificates>
      <Certificate name="CertificateName" thumbprint="CertThumbprint" thumbprintAlgorithm="sha1" />
      <Certificate name="Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption"
         thumbprint="CertThumbprint" thumbprintAlgorithm="sha1" />
    </Certificates>
  </Role>
</ServiceConfiguration>

可以参考 服务配置架构 以更好了解此处使用的 XML 架构,而以下是元素的快速说明:You can refer to the Service Configuration Schema for better understanding the XML schema used here, however, here is a quick explanation of the elements:

实例Instances
为角色配置运行角色实例数。Configures the number of running instances for the role. 若要防止云服务在升级期间可能变得不可用,建议部署面向 web 角色的多个实例。To prevent your cloud service from potentially becoming unavailable during upgrades, it is recommended that you deploy more than one instance of your web-facing roles. 部署多个实例即表示遵守 Azure 计算服务级别协议 (SLA) 中的准则,此协议可以保证在为一个服务部署了两个或多个角色实例时,面向 Internet 的角色拥有 99.95% 的外部连接。By deploying more than one instance, you are adhering to the guidelines in the Azure Compute Service Level Agreement (SLA), which guarantees 99.95% external connectivity for Internet-facing roles when two or more role instances are deployed for a service.

ConfigurationSettingsConfigurationSettings
为角色配置运行实例的设置。Configures the settings for the running instances for a role. <Setting> 元素的名称必须与服务定义文件中的设置定义匹配。The name of the <Setting> elements must match the setting definitions in the service definition file.

CertificatesCertificates
配置服务使用的证书。Configures the certificates that are used by the service. 前面的代码示例演示如何定义 RemoteAccess 模块的证书。The previous code example shows how to define the certificate for the RemoteAccess module. thumbprint 属性的值必须设置为要使用的证书的指纹。The value of the thumbprint attribute must be set to the thumbprint of the certificate to use.

Note

通过使用文本编辑器,可以将证书的指纹添加到配置文件中。The thumbprint for the certificate can be added to the configuration file by using a text editor. 或者,可以在 Visual Studio 中角色的“属性”页的“证书”选项卡上添加值。Or, the value can be added on the Certificates tab of the Properties page of the role in Visual Studio.

定义角色实例的端口Defining ports for role instances

Azure 仅允许 Web 角色有一个入口点。Azure allows only one entry point to a web role. 即所有通信都通过一个 IP 地址完成。Meaning that all traffic occurs through one IP address. 可以通过配置主机头使请求指向正确的位置来配置网站共享一个端口。You can configure your websites to share a port by configuring the host header to direct the request to the correct location. 此外,可将应用程序配置为侦听 IP 地址上的已知端口。You can also configure your applications to listen to well-known ports on the IP address.

以下示例显示了具有网站和 Web 应用程序的 Web 角色的配置。The following sample shows the configuration for a web role with a website and web application. 该网站配置为端口 80 上的默认入口位置,Web 应用程序配置为接收来自名为“mail.mysite.chinacloudapp.cn”的备用主机标头的请求。The website is configured as the default entry location on port 80, and the web applications are configured to receive requests from an alternate host header that is called “mail.mysite.chinacloudapp.cn”.

<WebRole>
  <ConfigurationSettings>
    <Setting name="DiagnosticsConnectionString" />
  </ConfigurationSettings>
  <Endpoints>
    <InputEndpoint name="HttpIn" protocol="http" port="80" />
    <InputEndpoint name="Https" protocol="https" port="443" certificate="SSL"/>
    <InputEndpoint name="NetTcp" protocol="tcp" port="808" certificate="SSL"/>
  </Endpoints>
  <LocalResources>
    <LocalStorage name="Sites" cleanOnRoleRecycle="true" sizeInMB="100" />
  </LocalResources>
  <Site name="Mysite" packageDir="Sites\Mysite">
    <Bindings>
      <Binding name="http" endpointName="HttpIn" />
      <Binding name="https" endpointName="Https" />
      <Binding name="tcp" endpointName="NetTcp" />
    </Bindings>
  </Site>
  <Site name="MailSite" packageDir="MailSite">
    <Bindings>
      <Binding name="mail" endpointName="HttpIn" hostheader="mail.mysite.cloudapp.cn" />
    </Bindings>
    <VirtualDirectory name="artifacts" />
    <VirtualApplication name="storageproxy">
      <VirtualDirectory name="packages" packageDir="Sites\storageProxy\packages"/>
    </VirtualApplication>
  </Site>
</WebRole>

更改角色的配置Changing the configuration of a role

当云服务在 Azure 中运行时,可以更新其配置而无需使服务处于脱机状态。You can update the configuration of your cloud service while it is running in Azure, without taking the service offline. 要更改配置信息,可以上传新的配置文件或就地编辑配置文件,并将其应用于正在运行的服务。To change configuration information, you can either upload a new configuration file, or edit the configuration file in place and apply it to your running service. 可对服务配置进行以下更改:The following changes can be made to the configuration of a service:

  • 更改配置设置的值Changing the values of configuration settings
    当配置设置改变时,角色实例可以选择在实例处于联机状态时应用此更改,或选择正常回收实例并在实例处于脱机状态时应用此更改。When a configuration setting changes, a role instance can choose to apply the change while the instance is online, or to recycle the instance gracefully and apply the change while the instance is offline.

  • 更改角色实例的服务拓扑Changing the service topology of role instances
    拓扑更改不会影响正在运行的实例,但正在删除实例的情况除外。Topology changes do not affect running instances, except where an instance is being removed. 所有剩余的实例通常不需要回收;但可以选择回收角色实例以响应拓扑更改。All remaining instances generally do not need to be recycled; however, you can choose to recycle role instances in response to a topology change.

  • 更改证书指纹Changing the certificate thumbprint
    仅可在角色实例处于脱机状态时更新一个证书。You can only update a certificate when a role instance is offline. 如果在角色实例处于联机状态时添加、删除或更改了某个证书,则 Azure 会使实例脱机以更新证书,并在更改完成后使其重新联机。If a certificate is added, deleted, or changed while a role instance is online, Azure gracefully takes the instance offline to update the certificate and bring it back online after the change is complete.

使用服务运行时事件处理配置更改Handling configuration changes with Service Runtime Events

Azure 运行时库包括 Microsoft.WindowsAzure.ServiceRuntime 命名空间,它为与 Azure 环境(来自角色实例中运行的代码)的交互提供类。The Azure Runtime Library includes the Microsoft.WindowsAzure.ServiceRuntime namespace, which provides classes for interacting with the Azure environment from code running in an instance of a role. RoleEnvironment 类定义在配置更改前后引发的以下事件:The RoleEnvironment class defines the following events that are raised before and after a configuration change:

  • Changing 事件Changing event
    此事件发生在配置更改应用于某个角色的指定实例之前,使你有机会记下角色实例(如有必要)。This occurs before the configuration change is applied to a specified instance of a role giving you a chance to take down the role instances if necessary.
  • Changed 事件Changed event
    发生在配置更改已应用于某个角色的指定实例之后。Occurs after the configuration change is applied to a specified instance of a role.

Note

由于证书更改始终使角色实例处于脱机状态,因此不会引发 RoleEnvironment.Changing 或 RoleEnvironment.Changed 事件。Because certificate changes always take the instances of a role offline, they do not raise the RoleEnvironment.Changing or RoleEnvironment.Changed events.

ServicePackage.cspkgServicePackage.cspkg

要将应用程序部署为 Azure 中的云服务,必须首先以适当的格式打包该应用程序。To deploy an application as a cloud service in Azure, you must first package the application in the appropriate format. 可以使用 CSPack 命令行工具(与 Azure SDK 一起安装)创建包文件,作为 Visual Studio 的替代。You can use the CSPack command-line tool (installed with the Azure SDK) to create the package file as an alternative to Visual Studio.

CSPack 使用服务定义文件和服务配置文件的内容来定义包的内容。CSPack uses the contents of the service definition file and service configuration file to define the contents of the package. CSPack 生成可以使用 Azure 门户 上传到 Azure 的应用程序包文件 (.cspkg)。CSPack generates an application package file (.cspkg) that you can upload to Azure by using the Azure portal. 默认情况下,该应用程序包名为 [ServiceDefinitionFileName].cspkg,但可以通过使用 CSPack 的 /out 选项指定不同的名称。By default, the package is named [ServiceDefinitionFileName].cspkg, but you can specify a different name by using the /out option of CSPack.

CSPack 位于CSPack is located at
C:\Program Files\Microsoft SDKs\Azure\.NET SDK\[sdk-version]\bin\

Note

CSPack.exe(在 Windows 中)可通过运行随 SDK 一起安装的“Azure 命令提示符”快捷方式使用。CSPack.exe (on windows) is available by running the Azure Command Prompt shortcut that is installed with the SDK.

运行 CSPack.exe 程序本身来查看有关所有可能的开关和命令的文档。Run the CSPack.exe program by itself to see documentation about all the possible switches and commands.

Tip

在 Azure 计算模拟器中本地运行云服务时,使用“/copyonly”选项。此选项将应用程序的二进制文件复制到目录布局,以便在计算模拟器中运行。Run your cloud service locally in the Azure Compute Emulator, use the /copyonly option This option copies the binary files for the application to a directory layout from which they can be run in the compute emulator.

打包云服务的示例命令Example command to package a cloud service

以下示例创建包含 Web 角色信息的应用程序包。The following example creates an application package that contains the information for a web role. 该命令指定待使用的服务定义文件、可以找到二进制文件的目录以及包文件名称。The command specifies the service definition file to use, the directory where binary files can be found, and the name of the package file.

cspack [DirectoryName]\[ServiceDefinition]
       /role:[RoleName];[RoleBinariesDirectory]
       /sites:[RoleName];[VirtualPath];[PhysicalPath]
       /out:[OutputFileName]

如果应用程序包含 Web 角色和辅助角色,则使用以下命令:If the application contains both a web role and a worker role, the following command is used:

cspack [DirectoryName]\[ServiceDefinition]
       /out:[OutputFileName]
       /role:[RoleName];[RoleBinariesDirectory]
       /sites:[RoleName];[VirtualPath];[PhysicalPath]
       /role:[RoleName];[RoleBinariesDirectory];[RoleAssemblyName]

其中变量如下所示定义:Where the variables are defined as follows:

变量Variable Value
[DirectoryName][DirectoryName] 包含 Azure 项目 .csdef 文件的根项目目录下的子目录。The subdirectory under the root project directory that contains the .csdef file of the Azure project.
[ServiceDefinition][ServiceDefinition] 服务定义文件的名称。The name of the service definition file. 默认情况下,此文件名为 ServiceDefinition.csdef。By default, this file is named ServiceDefinition.csdef.
[OutputFileName][OutputFileName] 生成的包文件的名称。The name for the generated package file. 通常,此值设为该应用程序的名称。Typically, this is set to the name of the application. 如果未指定任何文件名称,则应用程序包将创建为 [ApplicationName].cspkg。If no file name is specified, the application package is created as [ApplicationName].cspkg.
[RoleName][RoleName] 在服务定义文件中定义的角色的名称。The name of the role as defined in the service definition file.
[RoleBinariesDirectory][RoleBinariesDirectory] 该角色二进制文件的位置。The location of the binary files for the role.
[VirtualPath][VirtualPath] 在服务定义的站点部分中定义的每个虚拟路径的物理目录。The physical directories for each virtual path defined in the Sites section of the service definition.
[PhysicalPath][PhysicalPath] 在服务定义的站点节点中定义的每个虚拟路径的内容的物理目录。The physical directories of the contents for each virtual path defined in the site node of the service definition.
[RoleAssemblyName][RoleAssemblyName] 角色的二进制文件的名称。The name of the binary file for the role.

后续步骤Next steps

我正在创建云服务包,并且我想要...I'm creating a cloud service package and I want to...

我正在使用 Visual Studio,我想要...I am using Visual Studio and I want to...