迁移应用程序之前了解云服务与 Service Fabric 之间的差异。Learn about the differences between Cloud Services and Service Fabric before migrating applications.

Azure Service Fabric 是面向高度可缩放、高度可靠的分布式应用程序的下一代云应用程序平台。Azure Service Fabric is the next-generation cloud application platform for highly scalable, highly reliable distributed applications. 其中引入了许多用于打包、部署、升级和管理分布式云应用程序的新功能。It introduces many new features for packaging, deploying, upgrading, and managing distributed cloud applications.

本简介指南介绍如何将应用程序从云服务迁移到 Service Fabric。This is an introductory guide to migrating applications from Cloud Services to Service Fabric. 内容侧重于云服务与 Service Fabric 之间的体系结构和设计差异。It focuses primarily on architectural and design differences between Cloud Services and Service Fabric.

应用程序和基础结构Applications and infrastructure

云服务与 Service Fabric 之间的最基本差异在于 VM、工作负荷及应用程序之间的关系。A fundamental difference between Cloud Services and Service Fabric is the relationship between VMs, workloads, and applications. 此处的工作负荷定义为编写的、用来执行特定任务或提供服务的代码。A workload here is defined as the code you write to perform a specific task or provide a service.

  • 云服务涉及到将应用程序部署为 VM。Cloud Services is about deploying applications as VMs. 编写的代码与 VM 实例(如 Web 角色或辅助角色)密切结合。The code you write is tightly coupled to a VM instance, such as a Web or Worker Role. 在云服务中部署工作负荷就是要部署一个或多个运行该工作负荷的 VM 实例。To deploy a workload in Cloud Services is to deploy one or more VM instances that run the workload. 应用程序与 VM 没有区别,因此对于应用程序没有正式的定义。There is no separation of applications and VMs, and so there is no formal definition of an application. 可将应用程序视为云服务部署中的一组 Web 角色或辅助角色实例,或视为整个云服务部署。An application can be thought of as a set of Web or Worker Role instances within a Cloud Services deployment or as an entire Cloud Services deployment. 在此示例中,应用程序显示为一组角色实例。In this example, an application is shown as a set of role instances.

    云服务应用程序和拓扑

  • Service Fabric 涉及到将应用程序部署至现有的 VM 或运行 Service Fabric 的 Windows 或 Linux 计算机。Service Fabric is about deploying applications to existing VMs or machines running Service Fabric on Windows or Linux. 编写的服务完全与底层的基础结构分离(由 Service Fabric 应用程序平台抽象化),因此可将应用程序部署到多个环境。The services you write are completely decoupled from the underlying infrastructure, which is abstracted away by the Service Fabric application platform, so an application can be deployed to multiple environments. Service Fabric 中的工作负荷称为“服务”,一个或多个服务会在 Service Fabric 应用程序平台上运行的且正式定义的应用程序中分组。A workload in Service Fabric is called a "service," and one or more services are grouped in a formally-defined application that runs on the Service Fabric application platform. 可将多个应用程序部署到单个 Service Fabric 群集。Multiple applications can be deployed to a single Service Fabric cluster.

    Service Fabric 应用程序和拓扑

Service Fabric 本身是在 Windows 或 Linux 中运行的应用程序平台层,而云服务是用于部署 Azure 托管的且附加了工作负荷的 VM 的系统。Service Fabric itself is an application platform layer that runs on Windows or Linux, whereas Cloud Services is a system for deploying Azure-managed VMs with workloads attached. Service Fabric 应用程序模型有许多优点:The Service Fabric application model has a number of advantages:

  • 快速部署。Fast deployment times. 创建 VM 实例可能非常耗时。Creating VM instances can be time consuming. 在 Service Fabric 中,只需部署 VM 一次即可构成托管 Service Fabric 应用程序平台的群集。In Service Fabric, VMs are only deployed once to form a cluster that hosts the Service Fabric application platform. 此后,可将应用程序包快速部署到该群集。From that point on, application packages can be deployed to the cluster very quickly.
  • 高密度托管。High-density hosting. 在云服务中,一个辅助角色 VM 托管一个工作负荷。In Cloud Services, a Worker Role VM hosts one workload. 在 Service Fabric 中,应用程序与运行它们的 VM 分离,这意味着可以将大量应用程序部署到少量 VM,从而降低大量部署所需的总体成本。In Service Fabric, applications are separate from the VMs that run them, meaning you can deploy a large number of applications to a small number of VMs, which can lower overall cost for larger deployments.
  • Service Fabric 平台可在任何配备 Windows Server 或 Linux 计算机的位置(无论是 Azure 还是本地)运行。The Service Fabric platform can run anywhere that has Windows Server or Linux machines, whether it's Azure or on-premises. 该平台在底层基础结构上面提供一个抽象层,因此应用程序可在不同的环境中运行。The platform provides an abstraction layer over the underlying infrastructure so your application can run on different environments.
  • 分布式应用程序管理。Distributed application management. Service Fabric 平台不仅能够托管分布式应用程序,而且能够帮助管理这些应用程序的生命周期,不管托管 VM 或计算机的生命周期如何。Service Fabric is a platform that not only hosts distributed applications, but also helps manage their lifecycle independently of the hosting VM or machine lifecycle.

应用程序体系结构Application architecture

云服务应用程序的体系结构通常包含众多的外部服务依赖项(例如服务总线、Azure 表和 Blob 存储、SQL、Redis 等),以管理应用程序的状态与数据,以及云服务部署中 Web 角色和辅助角色之间的通信。The architecture of a Cloud Services application usually includes numerous external service dependencies, such as Service Bus, Azure Table and Blob Storage, SQL, Redis, and others to manage the state and data of an application and communication between Web and Worker Roles in a Cloud Services deployment. 完整云服务应用程序的示例如下:An example of a complete Cloud Services application might look like this:

云服务体系结构

Service Fabric 应用程序还可以选择在整个应用程序中使用相同的外部服务。Service Fabric applications can also choose to use the same external services in a complete application. 在此示例云服务体系结构中,从云服务迁移到 Service Fabric 的最简单路径是只将云服务部署替换为 Service Fabric 应用程序,并将整个体系结构保持相同。Using this example Cloud Services architecture, the simplest migration path from Cloud Services to Service Fabric is to replace only the Cloud Services deployment with a Service Fabric application, keeping the overall architecture the same. 只需进行少量的代码更改,即可将 Web 角色和辅助角色移植到 Service Fabric 无状态服务。The Web and Worker Roles can be ported to Service Fabric stateless services with minimal code changes.

简单迁移后的 Service Fabric 体系结构

在此阶段,系统应会像往常一样继续工作。At this stage, the system should continue to work the same as before. 利用 Service Fabric 的有状态服务,在适当的情况下,可将外部状态存储内部化为有状态服务。Taking advantage of Service Fabric's stateful features, external state stores can be internalized as stateful services where applicable. 这比单纯将 Web 角色和辅助角色迁移到 Service Fabric 无状态服务更复杂,因为需要编写为应用程序提供同等功能(就像外部服务所做的那样)的自定义服务。This is more involved than a simple migration of Web and Worker Roles to Service Fabric stateless services, as it requires writing custom services that provide equivalent functionality to your application as the external services did before. 这样做的好处包括:The benefits of doing so include:

  • 删除外部依赖项Removing external dependencies
  • 统一部署、管理和升级模型。Unifying the deployment, management, and upgrade models.

内部化这些服务后,生成的示例体系结构如下所示:An example resulting architecture of internalizing these services could look like this:

完整迁移后的 Service Fabric 体系结构

通信和工作流Communication and workflow

大多数云服务应用程序由多个层组成。Most Cloud Service applications consist of more than one tier. 同样,Service Fabric 应用程序由多个服务组成(通常是许多的服务)。Similarly, a Service Fabric application consists of more than one service (typically many services). 两种常见的通信模型是直接通信,以及通过外部持久性存储通信。Two common communication models are direct communication and via an external durable storage.

直接通信Direct communication

使用直接通信时,层之间可以通过各自公开的终结点直接通信。With direct communication, tiers can communicate directly through endpoint exposed by each tier. 在无状态环境(例如云服务)中,这意味着需要选择 VM 角色的实例(随机或者通过轮询机制平衡负载),并直接连接到其终结点。In stateless environments such as Cloud Services, this means selecting an instance of a VM role, either randomly or round-robin to balance load, and connecting to its endpoint directly.

云服务直接通信

直接通信是 Service Fabric 中常见的通信模型。Direct communication is a common communication model in Service Fabric. Service Fabric 和云服务的重要差别在于,在云服务中是连接到 VM,而在 Service Fabric 中是连接到服务。The key difference between Service Fabric and Cloud Services is that in Cloud Services you connect to a VM, whereas in Service Fabric you connect to a service. 这种差别之所以重要,其原因如下:This is an important distinction for a couple reasons:

  • Service Fabric 中的服务未绑定到托管它们的 VM;这些服务可以在群集中四处移动。事实上,它们会出于以下各种原因而四处移动:资源平衡、故障转移、应用程序和基础结构升级,以及放置或负载约束。Services in Service Fabric are not bound to the VMs that host them; services may move around in the cluster, and in fact, are expected to move around for various reasons: Resource balancing, failover, application and infrastructure upgrades, and placement or load constraints. 这意味着服务实例的地址可随时更改。This means a service instance's address can change at any time.
  • Service Fabric 中的一个 VM 可以托管多个服务,且每个服务都有唯一终结点。A VM in Service Fabric can host multiple services, each with unique endpoints.

Service Fabric 提供服务发现机制(称为“命名服务”),用于解析服务的终结点地址。Service Fabric provides a service discovery mechanism, called the Naming Service, which can be used to resolve endpoint addresses of services.

Service Fabric 直接通信

队列Queues

无状态环境(如云服务)中层之间的常见通信机制是使用外部存储队列将一个层的工作任务持久存储到另一个层。A common communication mechanism between tiers in stateless environments such as Cloud Services is to use an external storage queue to durably store work tasks from one tier to another. 一种常见方案是将作业发送到 Azure 队列或服务总线的 Web 层,辅助角色实例可在其中取消排队和处理作业。A common scenario is a web tier that sends jobs to an Azure Queue or Service Bus where Worker Role instances can dequeue and process the jobs.

云服务队列通信

在 Service Fabric 中可以使用相同的通信模型。The same communication model can be used in Service Fabric. 这有助于将现有的云服务应用程序迁移到 Service Fabric。This can be useful when migrating an existing Cloud Services application to Service Fabric.

Service Fabric 直接通信

奇偶校验Parity

云服务的控制和易用程度类似于 Service Fabric,但它现在是旧版服务,因此建议将 Service Fabric 用于新的开发;下面是 API 的对比情况:Cloud Services is similar to Service Fabric in degree of control versus ease of use, but it's now a legacy service and Service Fabric is recommended for new development; the following is an API comparison:

云服务 APICloud Service API Service Fabric APIService Fabric API 说明Notes
RoleInstance.GetIDRoleInstance.GetID FabricRuntime.GetNodeContext.NodeId 或 .NodeNameFabricRuntime.GetNodeContext.NodeId or .NodeName ID 是 NodeName 的属性ID is a property of the NodeName
RoleInstance.GetFaultDomainRoleInstance.GetFaultDomain FabricClient.QueryManager.GetNodeListFabricClient.QueryManager.GetNodeList 按 NodeName 筛选并使用 FD 属性Filter on NodeName and use FD Property
RoleInstance.GetUpgradeDomainRoleInstance.GetUpgradeDomain FabricClient.QueryManager.GetNodeListFabricClient.QueryManager.GetNodeList 按 NodeName 筛选并使用 Upgrade 属性Filter on NodeName, and use Upgrade property
RoleInstance.GetInstanceEndpointsRoleInstance.GetInstanceEndpoints FabricRuntime.GetActivationContext 或 Naming (ResolveService)FabricRuntime.GetActivationContext or Naming (ResolveService) CodePackageActivationContext,由 FabricRuntime.GetActivationContext 提供以及通过 ServiceInitializationParameters.CodePackageActivationContext provided during .Initialize 在副本中提供CodePackageActivationContext which is provided both by FabricRuntime.GetActivationContext and within the replicas via ServiceInitializationParameters.CodePackageActivationContext provided during .Initialize
RoleEnvironment.GetRolesRoleEnvironment.GetRoles FabricClient.QueryManager.GetNodeListFabricClient.QueryManager.GetNodeList 如果要按类型执行同种筛选,则可以通过 FabricClient.ClusterManager.GetClusterManifest 从群集清单获取节点类型列表,并从此处获取角色/节点类型。If you want to do the same sort of filtering by type you can get the list of node types from the cluster manifest via FabricClient.ClusterManager.GetClusterManifest and grab the role/node types from there.
RoleEnvironment.GetIsAvailableRoleEnvironment.GetIsAvailable Connect-WindowsFabricCluster 或创建指向特定节点的 FabricRuntimeConnect-WindowsFabricCluster or create a FabricRuntime pointed to a particular node *
RoleEnvironment.GetLocalResourceRoleEnvironment.GetLocalResource CodePackageActivationContext.Log/Temp/WorkCodePackageActivationContext.Log/Temp/Work *
RoleEnvironment.GetCurrentRoleInstanceRoleEnvironment.GetCurrentRoleInstance CodePackageActivationContext.Log/Temp/WorkCodePackageActivationContext.Log/Temp/Work *
LocalResource.GetRootPathLocalResource.GetRootPath CodePackageActivationContext.Log/Temp/WorkCodePackageActivationContext.Log/Temp/Work *
Role.GetInstancesRole.GetInstances FabricClient.QueryManager.GetNodeList 或 ResolveServiceFabricClient.QueryManager.GetNodeList or ResolveService *
RoleInstanceEndpoint.GetIPEndpointRoleInstanceEndpoint.GetIPEndpoint FabricRuntime.GetActivationContext 或 Naming (ResolveService)FabricRuntime.GetActivationContext or Naming (ResolveService) *

后续步骤Next Steps

从云服务迁移到 Service Fabric 的最简单路径是只将云服务部署替换为 Service Fabric 应用程序,并将应用程序的整个体系结构保持大致相同。The simplest migration path from Cloud Services to Service Fabric is to replace only the Cloud Services deployment with a Service Fabric application, keeping the overall architecture of your application roughly the same. 以下文章提供了帮助将 Web 角色或辅助角色转换为 Service Fabric 无状态服务的指南。The following article provides a guide to help convert a Web or Worker Role to a Service Fabric stateless service.