Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Cloud Functions cho Firebase
plat_ios
plat_android
plat_web
plat_flutter
plat_cpp
plat_unity
Cloud Functions cho Firebase là một khung không máy chủ cho phép bạn tự động chạy mã phụ trợ để phản hồi các sự kiện do sự kiện nền, yêu cầu HTTPS,
Admin SDK hoặc các công việc
Cloud Scheduler kích hoạt. Mã JavaScript, TypeScript hoặc Python của bạn được lưu trữ trên cơ sở hạ tầng của Google Cloud và chạy trong một môi trường được quản lý. Bạn không cần quản lý và mở rộng quy mô máy chủ của riêng mình.
Bạn đã sử dụng Cloud Functions trong Google Cloud chưa?
Tìm hiểu thêm về cách Firebase phù hợp với bức tranh này.
Bắt đầu
Trường hợp sử dụng
Các chức năng chính
Tích hợp các tính năng của Firebase và kết nối Firebase với Google Cloud |
Các hàm mà bạn viết có thể phản hồi các sự kiện do nhiều tính năng của Firebase và Google Cloud tạo ra, từ điều kiện kích hoạt của Firebase Authentication đến điều kiện kích hoạt của Cloud Storage.
Tích hợp trên các tính năng của Firebase bằng Admin SDK cùng với Cloud Functions, đồng thời tích hợp với các dịch vụ của bên thứ ba bằng cách viết webhook của riêng bạn.
Cloud Functions giảm thiểu mã nguyên mẫu, giúp bạn dễ dàng sử dụng Firebase và Google Cloud trong hàm của mình. |
Không cần bảo trì |
Triển khai mã JavaScript, TypeScript hoặc Python lên các máy chủ của chúng tôi bằng một lệnh từ dòng lệnh.
Sau đó, Firebase sẽ tự động tăng quy mô tài nguyên điện toán để phù hợp với các mẫu sử dụng của người dùng. Bạn không bao giờ phải lo lắng về thông tin đăng nhập, cấu hình máy chủ, việc cấp phép máy chủ mới hoặc ngừng hoạt động máy chủ cũ. |
Đảm bảo tính riêng tư và bảo mật cho logic của bạn |
Trong nhiều trường hợp, nhà phát triển muốn kiểm soát logic ứng dụng trên máy chủ để tránh hành vi giả mạo ở phía máy khách. Ngoài ra, đôi khi bạn không muốn cho phép mã đó bị đảo ngược.
Cloud Functions hoàn toàn tách biệt với ứng dụng, vì vậy, bạn có thể chắc chắn rằng Cloud Functions ở chế độ riêng tư và luôn thực hiện chính xác những gì bạn muốn. |
Tính năng này hoạt động như thế nào?
Sau khi bạn viết và triển khai một hàm, các máy chủ của Google sẽ bắt đầu quản lý hàm đó ngay lập tức. Bạn có thể kích hoạt hàm trực tiếp bằng một yêu cầu HTTP, Admin SDK hoặc một công việc theo lịch, hoặc trong trường hợp hàm nền, máy chủ của Google sẽ lắng nghe các sự kiện và chạy hàm khi hàm được kích hoạt.
Khi tải tăng hoặc giảm, Google sẽ phản hồi bằng cách nhanh chóng mở rộng quy mô số lượng phiên bản máy chủ ảo cần thiết để chạy hàm của bạn. Mỗi hàm chạy riêng biệt trong môi trường riêng với cấu hình riêng.
Vòng đời của một hàm nền
- Bạn viết mã cho một hàm mới, chọn một trình cung cấp sự kiện (chẳng hạn như Cloud Firestore) và xác định các điều kiện mà hàm sẽ thực thi.
- Khi bạn triển khai hàm:
- CLI Firebase sẽ tạo một kho lưu trữ
.zip
cho mã hàm, sau đó kho lưu trữ này sẽ được tải lên một vùng chứa Cloud Storage (bắt đầu bằng gcf-sources
) trước khi Cloud Functions tạo một kho lưu trữ Artifact Registry (có tên là gcf-artifacts
) trong dự án của bạn.
- Cloud Build truy xuất mã hàm và tạo nguồn hàm. Bạn có thể xem nhật ký Cloud Build trong bảng điều khiển Google Cloud.
- Hình ảnh vùng chứa cho mã hàm đã tạo được tải lên một kho lưu trữ Artifact Registry riêng tư trong dự án của bạn (có tên là
gcf-artifacts
) và hàm mới của bạn sẽ được triển khai.
- Khi trình cung cấp sự kiện tạo ra một sự kiện phù hợp với các điều kiện của hàm, mã sẽ được gọi. Hàm này có một tài khoản dịch vụ được đính kèm. Bạn có thể dùng tài khoản này để truy cập vào các dịch vụ khác của Firebase nhờ Firebase Admin SDK.
- Nếu hàm đang xử lý nhiều sự kiện, Google sẽ tạo thêm các phiên bản để xử lý công việc nhanh hơn. Nếu hàm ở trạng thái rảnh, các phiên bản sẽ được dọn dẹp.
- Khi bạn cập nhật hàm bằng cách triển khai mã đã cập nhật, các phiên bản cho phiên bản cũ sẽ được dọn dẹp cùng với các cấu phần phần mềm trong Artifact Registry và được thay thế bằng các phiên bản mới.
- Khi bạn xoá hàm, tất cả các phiên bản và tệp lưu trữ zip sẽ được dọn dẹp, cùng với các cấu phần phần mềm có liên quan trong Artifact Registry.
Mối kết nối giữa hàm và trình cung cấp sự kiện sẽ bị xoá.
Ngoài việc theo dõi các sự kiện bằng một hàm nền, bạn có thể gọi trực tiếp các hàm bằng một yêu cầu HTTP hoặc một lệnh gọi từ ứng dụng. Bạn cũng có thể kích hoạt các hàm theo một lịch biểu cố định hoặc đưa các hàm tác vụ vào hàng đợi thông qua Admin SDK.
Đường dẫn triển khai
|
Thiết lập Cloud Functions |
Cài đặt CLI Firebase và khởi động Cloud Functions trong dự án Firebase của bạn. |
|
Viết hàm |
Viết mã JavaScript, mã TypeScript hoặc mã Python để xử lý các sự kiện từ các dịch vụ Firebase, dịch vụ Google Cloud hoặc các nhà cung cấp sự kiện khác. |
|
Hàm kiểm thử |
Sử dụng trình mô phỏng cục bộ để kiểm thử các hàm của bạn. |
|
Triển khai và giám sát |
Nâng cấp dự án của bạn lên gói giá linh hoạt (trả tiền theo mức dùng) và triển khai các hàm bằng CLI Firebase. Sau đó, bạn có thể sử dụng bảng điều khiển
Google Cloud để xem và tìm kiếm trong nhật ký của mình. |
Các bước tiếp theo
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-17 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-17 UTC."],[],[],null,["Cloud Functions for Firebase \nplat_ios plat_android plat_web plat_flutter plat_cpp plat_unity \nCloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by background events, HTTPS requests, the Admin SDK, or Cloud Scheduler jobs. Your JavaScript, TypeScript or Python code is stored on Google Cloud infrastructure and runs in a managed environment. There's no need to manage and scale your own servers.\n\n\u003cbr /\u003e\n\nAlready using Cloud Functions in Google Cloud?\n[Learn more](/docs/functions/functions-and-firebase) about how Firebase fits\ninto the picture.\n\n[Get started](/docs/functions/get-started)\n[Use cases](/docs/functions/use-cases)\n\nKey capabilities\n\n|----------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Integrates Firebase features and connects Firebase with Google Cloud | The functions you write can respond to events generated by various Firebase and Google Cloud features, from [Firebase Authentication triggers](/docs/functions/auth-events) to [Cloud Storage Triggers](/docs/functions/gcp-storage-events). Integrate across Firebase features using the [Admin SDK](/docs/admin/setup) together with Cloud Functions, and integrate with third-party services by writing your own webhooks. Cloud Functions minimizes boilerplate code, making it easier to use Firebase and Google Cloud inside your function. |\n| Zero maintenance | Deploy your JavaScript, TypeScript, or Python code to our servers with one command from the command line. After that, Firebase automatically scales up computing resources to match the usage patterns of your users. You never worry about credentials, server configuration, provisioning new servers, or decommissioning old ones. |\n| Keeps your logic private and secure | In many cases, developers prefer to control application logic on the server to avoid tampering on the client side. Also, sometimes it's not desirable to allow that code to be reverse engineered. Cloud Functions is fully insulated from the client, so you can be sure it is private and always does exactly what you want. |\n\nHow does it work?\n\nAfter you write and deploy a function, Google's servers begin to manage the\nfunction immediately. You can fire the function directly with an HTTP request,\nthe Admin SDK, or a scheduled job, or, in the case of background functions,\nGoogle's servers listen for events and run the function when it is\ntriggered.\n\nAs the load increases or\ndecreases, Google responds by rapidly scaling the number of virtual server\ninstances needed to run your function. Each function runs in isolation, in its\nown environment with its own configuration.\n\nLifecycle of a background function\n\n1. You write code for a new function, selecting an event provider (such as Cloud Firestore), and defining the conditions under which the function should execute.\n2. When you deploy your function:\n 1. The Firebase CLI creates a `.zip` archive of the function code, which is then uploaded to a Cloud Storage bucket (prefixed with `gcf-sources`) before Cloud Functions creates an Artifact Registry repository (named `gcf-artifacts`) in your project.\n 2. Cloud Build retrieves the function code and builds the function source. You can view Cloud Build logs in the [Google Cloud console](//console.cloud.google.com/logs/query;query%3Dresource.type%253D%2522build%2522&sa=D&ust=1597179510979000&usg=AFQjCNG2BmFrgWjGoP83WiMrB2TLLXpBEQ).\n 3. The container image for the built functions code is uploaded to a private Artifact Registry repository in your project (named `gcf-artifacts`), and your new function is rolled out.\n3. When the event provider generates an event that matches the function's conditions, the code is invoked. The function has a service account attached to it that can be used to access other Firebase services with the help of the Firebase Admin SDK.\n4. If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.\n5. When you update the function by deploying updated code, instances for older versions are cleaned up along with build artifacts in Artifact Registry, and replaced by new instances.\n6. When you delete the function, all instances and zip archives are cleaned up, along with related build artifacts in Artifact Registry. The connection between the function and the event provider is removed.\n\nIn addition to listening for events with a background function, you can call\nfunctions directly with an HTTP request or a\n[call from the client](/docs/functions/callable). You can also trigger functions\non a fixed [schedule](/docs/functions/schedule-functions) or\n[enqueue task functions](/docs/functions/task-functions) via the Admin SDK.\n\nImplementation path\n\n|---|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | Set up Cloud Functions | Install the Firebase CLI and initialize Cloud Functions in your Firebase project. |\n| | Write functions | Write JavaScript code, TypeScript code, or Python code to handle events from Firebase services, Google Cloud services, or other event providers. |\n| | Test functions | Use the [local emulator](/docs/functions/local-emulator) to test your functions. |\n| | Deploy and monitor | Upgrade your project to the [pay-as-you-go Blaze pricing plan](/pricing) and deploy your functions using the Firebase CLI. You can then use the [Google Cloud console](//console.cloud.google.com/functions/list) to view and search through your logs. |\n\nNext steps\n\n- [Get started](/docs/functions/get-started) setting up, creating, and deploying functions.\n- Learn more about [what you can do with functions](/docs/functions/use-cases).\n- Try the [Cloud Functions codelab](https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#0)."]]