您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

云设计模式Cloud Design Patterns

这些设计模式可用于在云中构建可靠且可缩放的安全应用程序。These design patterns are useful for building reliable, scalable, secure applications in the cloud.

每种模式描述了该模式解决的问题、有关应用该模式的注意事项,以及基于 Microsoft Azure 的示例。Each pattern describes the problem that the pattern addresses, considerations for applying the pattern, and an example based on Microsoft Azure. 大多数模式都包含了代码示例或代码片段,演示如何在 Azure 中实现该模式。Most of the patterns include code samples or snippets that show how to implement the pattern on Azure. 但是,无论托管在 Azure 还是其他云平台中,大多数模式都与任一分布式系统相关。However, most of the patterns are relevant to any distributed system, whether hosted on Azure or on other cloud platforms.

云中开发的难题Challenges in cloud development



可用性指系统正常工作时间所占的比例,通常以运行时间百分比度量。Availability is the proportion of time that the system is functional and working, usually measured as a percentage of uptime. 可用性受系统错误、基础结构问题、恶意攻击和系统负载的影响。It can be affected by system errors, infrastructure problems, malicious attacks, and system load. 云应用程序通常向用户提供服务级别协议 (SLA),因此,它们在设计上必须能够最大程度地保持可用性。Cloud applications typically provide users with a service level agreement (SLA), so applications must be designed to maximize availability.

Data Management

数据管理Data Management

数据管理是云应用程序的关键要素,影响大部分质量属性。Data management is the key element of cloud applications, and influences most of the quality attributes. 出于性能、可伸缩性或可用性等方面的原因,数据通常托管在不同的位置并跨多个服务器,这可能会带来一系列的挑战。Data is typically hosted in different locations and across multiple servers for reasons such as performance, scalability or availability, and this can present a range of challenges. 例如,必须保持数据一致性,通常需要将不同位置的数据进行同步。For example, data consistency must be maintained, and data will typically need to be synchronized across different locations.

Design and Implementation

设计和实施Design and Implementation

合理的设计包括很多因素(例如组件设计和部署中的一致性与连贯性)、可简化管理和部署的可维护性,以及可允许在其他应用程序和其他方案中使用的组件和子系统的可重用性。Good design encompasses factors such as consistency and coherence in component design and deployment, maintainability to simplify administration and development, and reusability to allow components and subsystems to be used in other applications and in other scenarios. 在设计和实施阶段做出的决策对云托管应用程序和服务的质量和总拥有成本具有巨大影响。Decisions made during the design and implementation phase have a huge impact on the quality and the total cost of ownership of cloud hosted applications and services.



云应用程序的分布性要求消息基础结构在理想情况下能以松散耦合的方式连接组件和服务,从而将可伸缩性最大化。The distributed nature of cloud applications requires a messaging infrastructure that connects the components and services, ideally in a loosely coupled manner in order to maximize scalability. 异步消息受到广泛使用并提供了诸多好处,但也带来了许多挑战,如消息排序、有害消息管理和幂等性等。Asynchronous messaging is widely used, and provides many benefits, but also brings challenges such as the ordering of messages, poison message management, idempotency, and more.

Management and Monitoring

管理和监视Management and Monitoring

云应用程序在远程数据中心内运行,在此中心内,无法完全控制基础结构,或者在某些情况下无法控制操作系统。Cloud applications run in a remote datacenter where you do not have full control of the infrastructure or, in some cases, the operating system. 与本地部署相比,管理和监视难度更大。This can make management and monitoring more difficult than an on-premises deployment. 应用程序必须公开运行时信息,以便管理员和操作员管理和监视系统,支持不断变化的业务要求和定制,而无需停止或重新部署应用程序。Applications must expose runtime information that administrators and operators can use to manage and monitor the system, as well as supporting changing business requirements and customization without requiring the application to be stopped or redeployed.

Performance and Scalability

性能和可伸缩性Performance and Scalability

性能是指系统在给定的时间间隔内执行任何操作的响应能力,可伸缩性则是系统能够应对负载增大且不影响性能或随时增加可用资源的能力。Performance is an indication of the responsiveness of a system to execute any action within a given time interval, while scalability is ability of a system either to handle increases in load without impact on performance or for the available resources to be readily increased. 云应用程序往往会遇到可变工作负载和活动高峰。Cloud applications typically encounter variable workloads and peaks in activity. 预测这些变化(尤其是在多租户方案中)几乎是不可能的。Predicting these, especially in a multitenant scenario, is almost impossible. 应用程序应该能够在限制范围内扩大以满足需求高峰,并在需求减少时缩小。Instead, applications should be able to scale out within limits to meet peaks in demand, and scale in when demand decreases. 可伸缩性不仅涉及计算实例,而且还涉及其他要素,例如数据存储、消息传送基础结构,等等。Scalability concerns not just compute instances, but other elements such as data storage, messaging infrastructure, and more.



复原能力是指系统能够在发生故障后进行恰当处理,然后恢复正常。Resiliency is the ability of a system to gracefully handle and recover from failures. 由于云托管的性质(应用程序通常是多租户的、使用共享平台服务、争用资源和带宽、通过 Internet 通信、在市售硬件上运行),出现暂时性故障和持久性故障的可能性增大。The nature of cloud hosting, where applications are often multitenant, use shared platform services, compete for resources and bandwidth, communicate over the Internet, and run on commodity hardware means there is an increased likelihood that both transient and more permanent faults will arise. 快速高效检测故障并恢复是保持复原能力所必需的。Detecting failures, and recovering quickly and efficiently, is necessary to maintain resiliency.



安全性是防止超出设计使用范围的恶意或意外操作,并防止泄露或丢失信息的系统能力。Security is the capability of a system to prevent malicious or accidental actions outside of the designed usage, and to prevent disclosure or loss of information. 云应用程序暴露在受信任的本地边界之外的 Internet 上,通常向公众开放,并可能为不受信任的用户提供服务。Cloud applications are exposed on the Internet outside trusted on-premises boundaries, are often open to the public, and may serve untrusted users. 应用程序的设计和部署必须防范它们受到恶意攻击,将访问权限限制给经过批准的用户,并保护敏感数据。Applications must be designed and deployed in a way that protects them from malicious attacks, restricts access to only approved users, and protects sensitive data.

模式目录Catalog of patterns

模式Pattern 摘要Summary
代表Ambassador 创建代表客户服务或应用程序发送网络请求的帮助程序服务。Create helper services that send network requests on behalf of a consumer service or application.
防损层Anti-Corruption Layer 在现代应用程序与旧系统之间实施外观或适配器层。Implement a façade or adapter layer between a modern application and a legacy system.
异步请求-答复Asynchronous Request-Reply 在后端处理需要是异步处理但前端仍需要明确响应的情况下,将后端处理与前端主机分离。Decouple backend processing from a frontend host, where backend processing needs to be asynchronous, but the frontend still needs a clear response.
用于前端的后端Backends for Frontends 创建单独的后端服务,供特定的前端应用程序或接口使用。Create separate backend services to be consumed by specific frontend applications or interfaces.
隔层Bulkhead 将应用程序的元素隔离到池中,这样,如果一个元素发生失败,其他元素可继续工作。Isolate elements of an application into pools so that if one fails, the others will continue to function.
缓存端Cache-Aside 将数据按需从数据存储加载到缓存中Load data on demand into a cache from a data store
协调Choreography 让每项服务都参与决定业务运营的处理时间和处理方式,而不是依赖于一个中心型业务流程协调程序。Let each service decide when and how a business operation is processed, instead of depending on a central orchestrator.
断路器Circuit Breaker 连接到远程服务或资源时处理故障,此类故障所需修复时间不定。Handle faults that might take a variable amount of time to fix when connecting to a remote service or resource.
声明检查Claim Check 将大型消息拆分成声明检查和有效负载,以免消息总线过载。Split a large message into a claim check and a payload to avoid overwhelming a message bus.
补偿事务Compensating Transaction 撤销一系列会共同定义最终一致操作的工作。Undo the work performed by a series of steps, which together define an eventually consistent operation.
竞争性使用者Competing Consumers 使多个并发使用者能够处理同一消息通道上收到的消息。Enable multiple concurrent consumers to process messages received on the same messaging channel.
计算资源合并Compute Resource Consolidation 将多个任务或操作合并到单个计算单元Consolidate multiple tasks or operations into a single computational unit
CQRSCQRS 使用独立接口将读取数据的操作与更新数据的操作分离。Segregate operations that read data from operations that update data by using separate interfaces.
事件溯源Event Sourcing 使用只追加存储来记录描述域中数据采取的操作的完整系列事件。Use an append-only store to record the full series of events that describe actions taken on data in a domain.
外部配置存储External Configuration Store 将配置信息从应用程序部署包移出,移到一个集中的位置。Move configuration information out of the application deployment package to a centralized location.
联合标识Federated Identity 将身份验证委托给外部标识提供者。Delegate authentication to an external identity provider.
守护程序Gatekeeper 通过使用专用的主机实例保护应用程序和服务,该实例用于充当客户端和应用程序或服务之间的中转站、验证和整理请求,并在它们之间传递请求和数据。Protect applications and services by using a dedicated host instance that acts as a broker between clients and the application or service, validates and sanitizes requests, and passes requests and data between them.
网关聚合Gateway Aggregation 使用网关可将多个单独请求聚合成一个请求。Use a gateway to aggregate multiple individual requests into a single request.
网关卸载Gateway Offloading 将共享或专用服务功能卸载到网关代理。Offload shared or specialized service functionality to a gateway proxy.
网关路由Gateway Routing 使用单个终结点将请求路由到多个服务。Route requests to multiple services using a single endpoint.
运行状况终结点监视Health Endpoint Monitoring 在应用程序中实施可让外部工具通过公开终结点定期访问的功能检查。Implement functional checks in an application that external tools can access through exposed endpoints at regular intervals.
索引表Index Table 基于数据存储中经常由查询引用的字段创建索引。Create indexes over the fields in data stores that are frequently referenced by queries.
领导选拔Leader Election 通过选拔一个实例作为领导来负责管理其他实例,协调分布式应用程序中协作性任务实例集合所执行的操作。Coordinate the actions performed by a collection of collaborating task instances in a distributed application by electing one instance as the leader that assumes responsibility for managing the other instances.
具体化视图Materialized View 当未针对所需的查询操作完美设置数据的格式时,在一个或多个数据存储中基于数据生成预填充的视图。Generate prepopulated views over the data in one or more data stores when the data isn't ideally formatted for required query operations.
管道和筛选器Pipes and Filters 将一个执行复杂处理的任务分解为一系列可重复使用的单个元素。Break down a task that performs complex processing into a series of separate elements that can be reused.
优先级队列Priority Queue 为发送到服务的请求确定优先级,以便高优先级请求能够得到比低优先级请求更快速地接收和处理。Prioritize requests sent to services so that requests with a higher priority are received and processed more quickly than those with a lower priority.
发布方/订阅方Publisher/Subscriber 使应用程序能够以异步方式向多个感兴趣的使用者公布事件,而无需将发送方与接收方耦合。Enable an application to announce events to multiple interested consumers asynchronously, without coupling the senders to the receivers.
基于队列的负载调控Queue-Based Load Leveling 使用队列在任务与所调用的服务之间充当缓冲,从而缓解间歇性负载过大现象。Use a queue that acts as a buffer between a task and a service that it invokes in order to smooth intermittent heavy loads.
重试Retry 当应用程序尝试连接到服务或网络资源时,使应用程序能够通过以透明方式重试先前失败的操作来处理预期的临时故障。Enable an application to handle anticipated, temporary failures when it tries to connect to a service or network resource by transparently retrying an operation that's previously failed.
计划程序代理监督程序Scheduler Agent Supervisor 跨一组分布式服务和其他远程资源协调一组操作。Coordinate a set of actions across a distributed set of services and other remote resources.
分片Sharding 将数据存储划分为一组水平分区或分片。Divide a data store into a set of horizontal partitions or shards.
SidecarSidecar 将应用程序的组件部署到单独的进程或容器中,以提供隔离和封装。Deploy components of an application into a separate process or container to provide isolation and encapsulation.
静态内容托管Static Content Hosting 将静态内容部署到基于云的存储服务,再由后者将它们直接传送给客户端。Deploy static content to a cloud-based storage service that can deliver them directly to the client.
StranglerStrangler 通过将特定的功能片断逐渐取代为新的应用程序和服务,逐步迁移旧系统。Incrementally migrate a legacy system by gradually replacing specific pieces of functionality with new applications and services.
限制Throttling 控制应用程序实例、单个租户或整个服务对资源的消耗。Control the consumption of resources used by an instance of an application, an individual tenant, or an entire service.
附属密钥Valet Key 使用令牌或密钥,向客户端授予对特定资源或服务的受限直接访问权限。Use a token or key that provides clients with restricted direct access to a specific resource or service.