[英]Differences in designing a serverless function vs. regular server
我想知道在設計無服務器功能時采用什么方法,同時將設計常規服務器作為參考點。
對於傳統服務器,人們會專注於定義 collections,然后可以在每個服務器上運行 CRUD 操作(HTTP 動詞,例如 GET 或 POST)。 例如,您將有一組users
,您可以通過app.get('/users', ...)
獲取所有記錄,通過app.get('/users/{id}', ...)
或通過app.post('/users', ...)
創建一個。
您設計無服務器 function 的方法有何不同? 具體來說:
getAllUsers()
的方法來定義users
集合,然后僅使用 GET 來指定您要對其執行的操作。lookupUser()
, endTrialForUser()
(如果我們從lookupUser()
得到的用戶試用時間超過 7 天,就會觸發)等等,然后從客戶端一個接一個地運行它們(決定是否應該在客戶端結束試用 - 似乎很不安全),或者你會創建一個getUser()
然后處理那里的所有邏輯嗎?.../users/${id}/accountData
的事情。 您 go 如何獲取嵌套數據? 你會不會每次都返回一個完整的JSON?我一直在尋找有關此事的一些綜合文章,但沒有運氣。 有什么建議么?
這是您提出的一個非常廣泛的問題。 讓我試着逐點回答。
首先,您在這里談論的方法是Serverless API
項目方法。 您可以克隆他們的示例項目,以更好地了解如何構建REST
api 來執行CRUD
操作。 首先安裝 SAM cli ,然后運行以下命令。
$ sam init
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Cloning from https://github.com/aws/aws-sam-cli-app-templates
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Multi-step workflow
3 - Serverless API
4 - Scheduled task
5 - Standalone function
6 - Data processing
7 - Infrastructure event management
8 - Machine Learning
Template: 3
Which runtime would you like to use?
1 - dotnetcore3.1
2 - nodejs14.x
3 - nodejs12.x
4 - python3.9
5 - python3.8
Runtime: 2
Based on your selections, the only Package type available is Zip.
We will proceed to selecting the Package type as Zip.
Based on your selections, the only dependency manager available is npm.
We will proceed copying the template using npm.
Project name [sam-app]: sample-app
-----------------------
Generating application:
-----------------------
Name: sample-app
Runtime: nodejs14.x
Architectures: x86_64
Dependency Manager: npm
Application Template: quick-start-web
Output Directory: .
Next steps can be found in the README file at ./sample-app/README.md
Commands you can use next
=========================
[*] Create pipeline: cd sample-app && sam pipeline init --bootstrap
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
HTTP
操作與其合適的HTTP verbs
。 這可以在 API 網關配置,可以在 Lambda 代碼中檢查。 檢查處理程序的源代碼和您剛剛使用SAM
克隆的項目中的template.yml
文件。 // src/handlers/get-by-id.js
if (event.httpMethod !== 'GET') {
throw new Error(`getMethod only accepts GET method, you tried: ${event.httpMethod}`);
}
# template.yml
Events:
Api:
Type: Api
Properties:
Path: /{id}
Method: GET
命名完全取決於開發人員。 您可以采用與常規服務器項目相同的方法。 您可以使用名稱getAllUsers
或users
定義處理程序,然后將該資源的路徑設置為AWS API Gateway
中的GET /users
。 您可以選擇您想要的HTTP verbs
。 查看本教程以獲得更好的理解。
這又取決於你。 您可以創建一個單獨的 Lambda 來處理所有這些邏輯,或者創建由客戶端根據前一個 API 的響應一個接一個地觸發的 Lambda。我會說,創建一個單獨的 Lambda 並只返回累積響應以減少請求的數量。 但同樣,這完全取決於 UI 集成。 如果您的屏幕需要單獨的 API 調用,那么請務必創建單獨的 lambda。
這不是真的。 我們可以在 API 網關中指定動態路由。 在 API 網關中設置路由時,您可以使用{variableName}
輕松地在路由中設置通配符。
GET /users/{userId}
然后userId
將通過event.pathParameters
在 lambda function 中供您使用。
GET /users/{userId}?a=x
同樣,您甚至可以傳遞查詢字符串並通過代碼中的event.queryStringParameters
訪問它們。 看看使用路由。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.