无服务器应用:体系结构、模式和 Azure 实现Serverless apps: Architecture, patterns, and Azure implementation

显示无服务器应用电子书封面的屏幕截图。

下载地址:https://aka.ms/serverless-ebookDOWNLOAD available at: https://aka.ms/serverless-ebook

发布者PUBLISHED BY

Microsoft 开发人员部门、.NET 和 Visual Studio 产品团队Microsoft Developer Division, .NET, and Visual Studio product teams

Microsoft Corporation 的一个部门A division of Microsoft Corporation

One Microsoft WayOne Microsoft Way

Redmond, Washington 98052-6399Redmond, Washington 98052-6399

版权所有 © 2018 Microsoft CorporationCopyright © 2018 by Microsoft Corporation

保留所有权利。All rights reserved. 未经发布者书面许可,不得以任何形式或任何方式复制或传播本书中的任何内容。No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher.

本书“按原样”提供,表达作者的观点和看法。This book is provided "as-is" and expresses the author's views and opinions. 本书中表达的观点、看法和信息(包括 URL 和其他 Internet 网站引用)如有更改,恕不另行通知。The views, opinions and information expressed in this book, including URL and other Internet website references, may change without notice.

本书中提及的一些示例仅用于说明,纯属虚构。Some examples depicted herein are provided for illustration only and are fictitious. 不存在任何实际关联或联系,请勿妄加推断。No real association or connection is intended or should be inferred.

Microsoft 和 https://www.microsoft.com 上“商标”网页列出的商标是 Microsoft 集团公司的商标。Microsoft and the trademarks listed at https://www.microsoft.com on the "Trademarks" webpage are trademarks of the Microsoft group of companies.

Mac 和 macOS 是 Apple Inc. 的商标Mac and macOS are trademarks of Apple Inc.

所有其他标记和徽标均为其各自所有者的财产。All other marks and logos are property of their respective owners.

作者:Author:

Microsoft Corp. 高级云大使 Jeremy LiknessJeremy Likness, Senior Cloud Advocate, Microsoft Corp.

参与者:Contributor:

Microsoft Corp. 高级云大使 Cecil PhillipCecil Phillip, Senior Cloud Advocate, Microsoft Corp.

编辑:Editors:

Microsoft Corp. 高级内容开发者 Bill WagnerBill Wagner, Senior Content Developer, Microsoft Corp.

Microsoft Corp. 高级内容开发者 Maira WenzelMaira Wenzel, Senior Content Developer, Microsoft Corp.

参与者和审阅者:Participants and reviewers:

Steve Smith,Ardalis Services 所有者 。Steve Smith, Owner, Ardalis Services.

介绍Introduction

无服务器是云平台向纯云本机代码方向的进化。Serverless is the evolution of cloud platforms in the direction of pure cloud native code. 通过无服务器,开发者更接近业务逻辑,避免基础结构问题。Serverless brings developers closer to business logic while insulating them from infrastructure concerns. 这种模式并不意味着“没有服务器”,而是“更少的服务器”。It's a pattern that doesn't imply "no server" but rather, "less server." 无服务器代码是事件驱动的。Serverless code is event-driven. 无论是传统的 HTTP Web 请求,还是计时器或上传文件的结果都可以触发代码。Code may be triggered by anything from a traditional HTTP web request to a timer or the result of uploading a file. 无服务器背后的基础结构可实现即时缩放以满足弹性需求,并提供微计费以真正“为所使用的量付费”。The infrastructure behind serverless allows for instant scale to meet elastic demands and offers micro-billing to truly "pay for what you use." 无服务器需要一种新的生成应用程序的思维和方法,它并不是解决每个问题的正确解决方案。Serverless requires a new way of thinking and approach to building applications and isn't the right solution for every problem. 作为开发者,必须确定:As a developer, you must decide:

  • 无服务器的优点和缺点是什么?What are the pros and cons of serverless?
  • 为什么要考虑为自己的应用程序使用无服务器?Why should you consider serverless for your own applications?
  • 如何生成、测试、部署和维护无服务器代码?How can you build, test, deploy, and maintain your serverless code?
  • 在现有应用程序中将代码迁移到无服务器有什么意义?完成此转换的最佳方法是什么?Where does it make sense to migrate code to serverless in existing applications, and what is the best way to accomplish this transformation?

关于本指南About this guide

本指南重点介绍使用无服务器的应用程序的云本机开发。This guide focuses on cloud native development of applications that use serverless. 本书重点介绍了这些优点并揭示了开发无服务器应用的潜在缺点,并提供了对无服务器体系结构的调查。The book highlights the benefits and exposes the potential drawbacks of developing serverless apps and provides a survey of serverless architectures. 介绍了有关使用无服务器的许多示例以及各种无服务器设计模式。Many examples of how serverless can be used are illustrated along with various serverless design patterns.

本指南介绍了 Azure 无服务器平台的组件,并重点介绍了如何使用 Azure Functions 实现无服务器。This guide explains the components of the Azure serverless platform and focuses specifically on implementation of serverless using Azure Functions. 你将了解触发器和绑定以及如何使用 Durable Functions 实现依赖于状态的无服务器应用。You'll learn about triggers and bindings as well as how to implement serverless apps that rely on state using durable functions. 最后,业务示例和案例研究将有助于提供用于确定无服务器是否是项目正确方法的上下文和参考框架。Finally, business examples and case studies will help provide context and a frame of reference to determine whether serverless is the right approach for your projects.

云平台的演变Evolution of cloud platforms

无服务器是云平台多次迭代的巅峰。Serverless is the culmination of several iterations of cloud platforms. 这种演变始于数据中心的物理计算机,然后经历了基础结构即服务 (IaaS) 和平台即服务 (PaaS) 的发展阶段。The evolution began with physical metal in the data center and progressed through Infrastructure as a Service (IaaS) and Platform as a Service (PaaS).

从本地到无服务器的演变

在云之前,开发和运营之间存在明显的界限。Before the cloud, a discernible boundary existed between development and operations. 部署应用程序需要回答无数问题,例如:Deploying an application meant answering myriad questions like:

  • 应该安装什么硬件?What hardware should be installed?
  • 如何保护对计算机的物理访问?How is physical access to the machine secured?
  • 数据中心是否需要不间断电源 (UPS)?Does the data center require an Uninterruptible Power Supply (UPS)?
  • 存储备份发送到何处?Where are storage backups sent?
  • 应该有冗余电源吗?Should there be redundant power?

此类问题不胜枚举,相关开销巨大。The list goes on and the overhead was enormous. 在许多情况下,IT 部门不得不处理大量浪费。In many situations, IT departments were forced to deal with incredible waste. 浪费是由服务器过度分配导致的,例如用于灾难恢复的备份计算机以及用于横向扩展的备用服务器。幸运的是,随着虚拟化技术(如 Hyper-V)和虚拟机 (VM) 的引入,产生了基础结构即服务 (IaaS)。The waste was due to over-allocation of servers as backup machines for disaster recovery and standby servers to enable scale-out. Fortunately, the introduction of virtualization technology (like Hyper-V) with Virtual Machines (VMs) gave rise to Infrastructure as a Service (IaaS). 虚拟化基础结构允许运营将一组标准服务器设置为主干网,从而形成一个灵活的环境,能够“按需”预配唯一服务器。Virtualized infrastructure allowed operations to set up a standard set of servers as the backbone, leading to a flexible environment capable of provisioning unique servers "on demand." 更重要的是,虚拟化为使用云提供虚拟机“即服务”奠定了基础。More important, virtualization set the stage for using the cloud to provide virtual machines "as a service." 公司不必担心冗余电源或物理计算机的问题。Companies could easily get out of the business of worrying about redundant power or physical machines. 他们可专注于虚拟环境。Instead, they focused on the virtual environment.

IaaS 仍然需要大量开销,因为运营仍然负责执行各种任务。IaaS still requires heavy overhead because operations is still responsible for various tasks. 这些任务包括:These tasks include:

  • 修补和备份服务器。Patching and backing up servers.
  • 安装包。Installing packages.
  • 使操作系统保持最新状态。Keeping the operating system up-to-date.
  • 监控应用程序。Monitoring the application.

新的发展通过平台即服务 (PaaS) 减少了开销。The next evolution reduced the overhead by providing Platform as a Service (PaaS). 借助 PaaS,云提供程序可处理操作系统、安全修补程序和支持特定平台所需的包。With PaaS, the cloud provider handles operating systems, security patches, and even the required packages to support a specific platform. 开发者只需选择“平台目标”(如“Web 应用程序”或“API 终结点”)并直接部署代码,而不用生成 VM,然后配置 .NET Framework 并启用 Internet Information Services (IIS) 服务器。Instead of building a VM then configuring the .NET Framework and standing up Internet Information Services (IIS) servers, developers simply choose a "platform target" such as "web application" or "API endpoint" and deploy code directly. 基础结构问题减少到:The infrastructure questions are reduced to:

  • 需要什么大小的服务?What size services are needed?
  • 如何横向扩展服务(添加更多服务器或节点)?How do the services scale out (add more servers or nodes)?
  • 如何纵向扩展服务(增加托管服务器或节点的容量)?How do the services scale up (increase the capacity of hosting servers or nodes)?

无服务器通过专注于事件驱动代码进一步抽象化服务器。Serverless further abstracts servers by focusing on event-driven code. 开发者可关注执行一项操作的微服务,而非平台。Instead of a platform, developers can focus on a microservice that does one thing. 生成无服务器代码的两个关键问题是:The two key questions for building the serverless code are:

  • 什么触发代码?What triggers the code?
  • 代码会执行哪些操作?What does the code do?

通过无服务器,抽象化基础结构。With serverless, infrastructure is abstracted. 在某些情况下,开发者不再担心主机。In some cases, the developer no longer worries about the host at all. 无论是运行 IIS、Kestrel、Apache 还是其他 Web 服务器的实例来管理 Web 请求,开发者都会关注 HTTP 触发器。Whether or not an instance of IIS, Kestrel, Apache, or some other web server is running to manage web requests, the developer focuses on an HTTP trigger. 触发器为请求提供标准的跨平台有效负载。The trigger provides the standard, cross-platform payload for the request. 有效负载不仅简化了开发过程,而且便于测试,在某些情况下,使代码可跨平台轻松移植。The payload not only simplifies the development process, but facilitates testing and in some cases, makes the code easily portable across platforms.

无服务器的另一个功能是微计费。Another feature of serverless is micro-billing. Web 应用通常托管 Web API 终结点。It's common for web applications to host Web API endpoints. 在传统的裸机、IaaS 和 PaaS 实现中,托管 API 的资源是连续付费的。In traditional bare metal, IaaS and even PaaS implementations, the resources to host the APIs are paid for continuously. 这意味着即使没有访问终结点,也需要付费来托管终结点。That means you pay to host the endpoints even when they aren't being accessed. 通常会发现调用某个 API 比调用其他 API 的次数更多,因此整个系统基于支持热门终结点进行缩放。Often you'll find one API is called more than others, so the entire system is scaled based on supporting the popular endpoints. 借助无服务器,可独立缩放每个终结点并支付使用费用,因此在不调用 API 时不会产生任何成本。Serverless enables you to scale each endpoint independently and pay for usage, so no costs are incurred when the APIs aren't being called. 在许多情况下,迁移可以大大降低用于支持终结点的持续成本。Migration may in many circumstances dramatically reduce the ongoing cost to support the endpoints.

本指南未涵盖的内容What this guide doesn't cover

本指南特别强调了体系结构方法和设计模式,并未深入探讨 Azure Functions、逻辑应用或其他无服务器平台的实现细节。This guide specifically emphasizes architecture approaches and design patterns and isn't a deep dive into the implementation details of Azure Functions, Logic Apps, or other serverless platforms. 本指南不包括逻辑应用的高级工作流或 Azure Functions 的功能等,如配置跨源资源共享(CORS)、应用自定义域或上传 SSL 证书。This guide doesn't cover, for example, advanced workflows with Logic Apps or features of Azure Functions such as configuring Cross-Origin Resource Sharing (CORS), applying custom domains, or uploading SSL certificates. 可以通过联机 Azure Functions 文档获得这些详细信息。These details are available through the online Azure Functions documentation.

其他资源Additional resources

本指南的目标读者Who should use the guide

本指南面向具有以下需求的开发者和解决方案架构师:需要使用 .NET 生成企业应用程序(这些应用程序可能使用本地或云端的无服务器组件)。This guide was written for developers and solution architects who want to build enterprise applications with .NET that may use serverless components either on premises or in the cloud. 可为具有以下需求的开发者、架构师和技术决策制定者提供帮助:It's useful to developers, architects, and technical decision makers interested in:

  • 了解无服务器开发的优点和缺点Understanding the pros and cons of serverless development
  • 学习如何处理无服务器体系结构Learning how to approach serverless architecture
  • 无服务器应用实现的示例Example implementations of serverless apps

如何使用本指南How to use the guide

本指南的第一部分通过比较几种不同的体系结构方法来探讨无服务器的可行性。The first part of this guide examines why serverless is a viable option by comparing several different architecture approaches. 本指南分析了技术和开发生命周期,因为软件开发的所有方面都受到体系结构决策的影响。It examines both the technology and development lifecycle, because all aspects of software development are impacted by architecture decisions. 然后,本指南介绍了用例和设计模式,并说明了使用 Azure Functions 的参考实现。The guide then examines use cases and design patterns and includes reference implementations using Azure Functions. 每个部分都包含介绍特定方面的其他资源。Each section contains additional resources to learn more about a particular area. 该指南最后提供了有关无服务器实现的演练和实践探索的资源。The guide concludes with resources for walkthroughs and hands-on exploration of serverless implementation.

提供你的反馈Send your feedback

我们正在不断完善指南和相关示例,欢迎你提供反馈!The guide and related samples are constantly evolving, so your feedback is welcomed! 如果对如何改进本指南有任何建议,请使用 GitHub 问题上任何页面底部的反馈部分进行反馈。If you have comments about how this guide can be improved, use the feedback section at the bottom of any page built on GitHub issues.