本教學課程將逐步引導您設定一組應用程式,以透過 Pub/Sub 傳送訊息的方式進行通訊,而非使用同步遠端程序呼叫 (RPC)。透過解耦應用程式,訊息功能可:
- 可讓應用程式更穩定可靠
- 可能可以簡化開發
例如呼叫者 (發布者) 不需要接收者 (訂閱者) 維持上線可用的狀態,發布端將訊息傳送至 Pub/Sub。發布者不需要知道哪些及多少訂閱者應用程式需要接收訊息。因此,只要應用程式可用,您就能仰賴此服務將訊息傳送至一個或多個訂閱者應用程式。
系統總覽
在本教學課程中,您將啟動發布者應用程式,透過一對多通訊機制,向兩個訂閱者傳送「Hello, World!」訊息,如下圖所示:
兩個訂閱者應用程式使用相同程式碼,但您可於不同時間啟動。這個程序說明 Pub/Sub 如何啟用非同步通訊。如要建構此系統,請完成下列步驟:
- 建立讓應用程式用於驗證的 IAM 服務帳戶。
- 設定身分與存取權管理權限。
- 建立 Pub/Sub 主題和訂閱項目。
- 啟動三個獨立應用程式:一個發布者及兩個訂閱者。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/pubsub.publisher, roles/pubsub.subscriber
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/pubsub.publisher, roles/pubsub.subscriber
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
下載本教學課程所需的 Pub/Sub Python 檔案。
git clone https://github.com/googleapis/python-pubsub.git
在繼續進行之前,請關閉任何開啟的終端機。
針對每個教學課程應用程式啟動一個終端機 (一個發布者及兩個訂閱者)。為了方便起見,本教學課程會呼叫下列終端機:
- 「publisher」終端機
- 「sub_one」終端機
- 「sub_two」終端機
在 publisher 終端機中,建立並啟用名為
pyenv-qs
的 Python 虛擬環境。Bash
python -m venv pyenv-qs source pyenv-qs/bin/activate
PowerShell
py -m venv pyenv-qs .\pyenv-qs\Scripts\activate
在 sub_one 和 sub_two 終端機中執行下列指令:
Bash
source pyenv-qs/bin/activate
PowerShell
.\pyenv-qs\Scripts\activate
執行啟動指令後,指令提示訊息會包含下列值
(pyenv-qs) $
。在發布端終端機中,使用
pip
安裝 Pub/Sub Python 用戶端程式庫:python -m pip install --upgrade google-cloud-pubsub
在所有三個終端機中,使用目前的專案 ID 設定環境變數。此 gcloud 指令會判定您選取的專案 ID,並將其設為變數:
Bash
export PROJECT=`gcloud config get-value project`
PowerShell
$env:PROJECT=$(gcloud config get-value project)
在所有三個終端機中,變更為包含範例程式碼的專案路徑。
cd python-pubsub/samples/snippets/quickstart/
「Publisher」(發布者) 應用程式傳送「Hello, World!」訊息至 Pub/Sub,但不知道當下的訂閱狀況。伺服器也會指派訊息 ID。
「Subscriber 1」(訂閱者 1) 應用程式接收到「Hello World」訊息並進行輸出,然後傳送確認訊息至 Pub/Sub。
「Publisher」應用程式會輸出確認訊息。確認訊息會告知 Pub/Sub 訊息已成功處理,不需要再次傳送至此訂閱者或任何其他「sub_one」sub_one訂閱者。
- 停止所有執行中的應用程式。
- 從本機環境刪除範例程式碼目錄。
刪除主題。
gcloud pubsub topics delete hello_topic
刪除訂閱項目。
gcloud pubsub subscriptions delete sub_one
gcloud pubsub subscriptions delete sub_two
在 Google Cloud 控制台的「IAM & admin」(IAM 與管理員) 區段關閉教學課程專案。
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
檢驗教學課程的
pub.py
和sub.py
程式碼,並在 GitHub 上瀏覽其他 Pub/Sub 範例。您可建立每秒發布當地時間的pub.py
版本做為練習。瞭解如何批次處理訊息。
使用推送訂閱來接收可觸發 App Engine 端點或 Cloud Functions 的訊息。
利用重播擷取之前已確認的訊息。根據預設,Pub/Sub 會從訂閱項目移除已確認的訊息。例如在本教學課程中,您無法重新執行
sub.py
來再次接收「Hello, World!」訊息。重播功能可讓您設定訂閱,以接收確認後的訊息。開始使用其他語言的用戶端程式庫。
安裝 Python
本教學課程使用 Pub/Sub 用戶端程式庫,因此需要使用 Python 3.7 以上版本。完成安裝 Python 的操作說明。
設定 Pub/Sub 專案
如要管理發布和訂閱應用程式之間的訊息流程,您需要建立主題和兩個不同的訂閱項目。
建立 Pub/Sub 主題
使用 ID hello_topic
建立主題:
gcloud pubsub topics create hello_topic
建立 Pub/Sub 訂閱項目
建立兩個訂閱項目,並附加至主題。
這些訂閱是 StreamingPull 訂閱,這是一種提取類型的訂閱。
訂閱 1
建立 ID 為 sub_one
的訂閱項目,並附加至 hello_topic
。
gcloud pubsub subscriptions create sub_one --topic=hello_topic
訂閱 2
建立 ID 為 sub_two
的訂閱項目,並附加至 hello_topic
。
gcloud pubsub subscriptions create sub_two --topic=hello_topic
建構一對多系統
下載發布者和訂閱者程式碼
設定三個終端機
啟動應用程式並觀察訊息流程
啟動「Subscriber 1」(訂閱者 1) 應用程式
請在「sub_one」sub_one終端機中,啟動「Subscriber 1」:
Bash
python sub.py $PROJECT sub_one
PowerShell
py sub.py $env:PROJECT sub_one
啟動後,這個應用程式會與伺服器開啟雙向串流連線。Pub/Sub 會透過串流傳送訊息。
啟動「Publisher」(發布者) 應用程式
請在「publisher」終端機中,啟動「Publisher」應用程式:
Bash
python pub.py $PROJECT hello_topic
PowerShell
py pub.py $env:PROJECT hello_topic
發布者應用程式啟動後,Pub/Sub 系統會執行下列操作:
Pub/Sub 從「sub_one」sub_one移除訊息。
啟動「Subscriber 2」(訂閱者 2) 應用程式
請在「sub_two」sub_two終端機中,啟動「Subscriber 2」:
Bash
python sub.py $PROJECT sub_two
PowerShell
py sub.py $env:PROJECT sub_two
此訂閱者接收傳送至「sub_two」sub_two訂閱的訊息。「Subscriber 2」(訂閱者 2) 會重複使用 sub.py
指令碼。其中的不同之處在於「Subscriber 2」(訂閱者 2) 要等到「Publisher」(發布者) 傳送訊息至主題及訂閱後才會啟動。如果「Publisher」(發布者) 直接呼叫「Subscriber 2」(訂閱者 2),發布應用程式必須等到「Subscriber 2」(訂閱者 2) 上線,否則就必須逾時。Pub/Sub 可以有效為「Subscriber 2」(訂閱者 2) 儲存此訊息,藉此管理這個程序。
您現在可以使用 Pub/Sub 進行開發了!
還順利嗎?
您可以在 Pub/Sub 支援頁面上取得其他資源及連結。
清除所用資源
後續步驟
您可嘗試進行以下項目: