無伺服器應用程式:架構、模式和 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

Copyright © 2018 by 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 及其他網際網路網站參考) 可能會隨時變更,恕不另行通知。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:

Bill Wagner ,Microsoft Corp 的資深內容開發人員。Bill 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. 您會了解觸發程序和繫結程序,以及如何使用永久的函式實作依賴狀態的無伺服器應用程式。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. 浪費的原因是過度佈建服務器作為損毀修復和待命伺服器的備份電腦,以啟用相應放大。幸運的是,使用虛擬機器(Vm)的虛擬化技術(如hyper-v)引進了基礎結構即服務(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. 開發人員並不需要建置 VM,然後設定 .NET Framework 和 Internet Information Services (IIS) 伺服器,只要選擇「平台目標」,如「Web 應用程式」或「API 端點」,然後直接部署程式碼。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、Logic Apps或其他無伺服器平台的實作詳細資料。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. 例如,本指南不會說明,Logic Apps 的進階工作流程或 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.