使用Google Cloud Platform分散您的应用程序
by Simeon Kostadinov
通過Simeon Kostadinov
使用Google Cloud Platform分散您的應用程序 (Decentralize your application with Google Cloud Platform)
When first starting a new software project, you normally choose a certain programming language, a specific framework and libraries. Then you begin coding. After 2 - 3 months you end up with a nicely working single application.
首次啟動新軟件項目時,通常會選擇某種編程語言,特定框架和庫。 然后開始編碼。 2-3個月后,您將獲得一個運行良好的單個應用程序。
But, as the project grows and more functionalities are added, you quickly realize the disadvantages of a centralized system. Difficult to maintain and unscalable are some of the reasons which will make you search for a better solution. Here is where Microservices come in help.
但是,隨著項目的發展和更多功能的添加,您很快就會意識到集中式系統的弊端。 難以維護和不可擴展是某些使您尋找更好的解決方案的原因。 這是微服務提供幫助的地方。
什么是微服務? (What are Microservices?)
Microservices are independently built systems, each running in their own process and often communicating with REST API. Representing different parts of your application, they are separately deployable and each part can be written in any language.
微服務是獨立構建的系統,每個系統都在各自的進程中運行,并經常與REST API通信。 代表應用程序的不同部分,它們可以分別部署,并且每個部分都可以用任何語言編寫。
You can easily see how, by dealing with the problems of a monolithic system, Microservices have become a requirement for any state-of-the-art software.
您可以輕松地看到,通過處理單片系統的問題,微服務已成為任何最新軟件的要求。
I strongly recommend reading Microservices (by James Lewis) and On Monoliths and Microservices if you want to understand more in depth what are the key concepts in this architectural style.
如果您想更深入地了解這種體系結構樣式中的關鍵概念,我強烈建議您閱讀Microservices(James Lewis著)和On Monoliths and Microservices 。
你要建造什么? (What are you going to build?)
This article will walk you through the process of implementing a Microservice using Google Cloud Platform.
本文將引導您完成使用Google Cloud Platform實施微服務的過程。
Imagine you’re developing an application that accepts a text input from a user and determine the category of the key words within the input.
假設您正在開發一個應用程序,該應用程序接受用戶的文本輸入并確定輸入中關鍵字的類別。
We’ll use an example to illustrate the functionality of the App. Consider the sample text below from the GCP Cloud Natural Language API website:
我們將使用一個示例來說明該應用程序的功能。 考慮以下來自GCP Cloud Natural Language API網站的示例文本:
“Google, headquartered in Mountain View, unveiled the new Android phone at the Consumer Electronic Show. Sundar Pichai said in his keynote that users love their new Android phones.”“總部位于山景城的Google在消費者電子展上推出了新的Android手機。 Sundar Pichai在主題演講中說,用戶喜歡他們的新Android手機。”Our web App would accept the text above as input, and return the category that the key words belong to, as in the figure below:
我們的Web應用程序將接受上面的文本作為輸入,并返回關鍵字所屬的類別,如下圖所示:
This feature is quite likeable and people use it hundreds of times each day. Now, if you’re going to offer this functionality as a service that receives a high amount of daily traffic, you want to respond with a stable and reliable system.
此功能相當不錯,人們每天使用數百次。 現在,如果您要提供此功能作為一項接收大量日常流量的服務,則希望以穩定可靠的系統進行響應。
That’s why we’ll build a lightweight Flask App, hosted on Google App Engine. Integrating it with Google Cloud Pub/Sub will help us handle all the asynchronous requests we receive and help us assure that users don’t wait too long for a response.
因此,我們將構建一個輕量級的Flask應用程序,該應用程序托管在Google App Engine上 。 將其與Google Cloud Pub / Sub集成在一起將幫助我們處理收到的所有異步請求,并確保用戶不會等待太久就等待響應。
創建和部署應用程序 (Create and deploy the application)
Let’s first start with the Flask app (you can also choose Django, Node.js, Go or anything used to build server-side applications). If you’re not very familiar with developing a Flask App, this Flask Series can show you step-by-step how to set up an application.
讓我們首先從Flask應用程序開始(您也可以選擇Django,Node.js,Go或用于構建服務器端應用程序的任何東西)。 如果您對開發Flask應用程序不太熟悉,則此Flask系列可以逐步向您展示如何設置應用程序。
For the purpose of this tutorial we will use this simple example:
就本教程而言,我們將使用以下簡單示例:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網站,似乎不再可用
First you need to install the dependencies pip install Flask gunicorn. You will be using gunicorn to run the application on Google App Engine. For local access you can run python text.py in the console and find the app on port 8080.
首先,您需要安裝依賴項pip install Flask gunicorn 。 您將使用gunicorn在Google App Engine上運行該應用程序。 對于本地訪問,您可以在控制臺中運行python text.py并在端口8080上找到該應用程序。
To deploy the app to Google App Engine, you need to take these steps:
要將應用程序部署到Google App Engine,您需要執行以下步驟:
Create a project (follow the ‘Before you begin’ instructions from the documentation). Save the project id for later.
創建一個項目(按照文檔中的“開始之前”的說明進行操作 )。 保存項目ID,以備后用。
Create app.yaml file (shown below), which is used by the Google App Engine to recognize the application.
創建app.yaml文件(如下所示),Google App Engine使用該文件來識別該應用程序。
Run gcloud app deploy in the console.
在控制臺中運行gcloud app deploy 。
The app.yaml file looks like this:
app.yaml文件如下所示:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網站,似乎不再可用
Line 3 is important, where you use gunicorn to tell Google App Engine to run the application app from a file called text.py (the Flask app). You can learn more about the .yaml file structure here. After deployment you should be able to access your project from https://[YOUR_PROJECT_ID].appspot.com.
第3行非常重要,您可以使用gunicorn告訴Google App Engine從名為text.py的文件中運行應用app (Flask應用) 。 您可以在此處了解有關.yaml文件結構的更多信息。 部署后,您應該可以從https://[YOUR_PROJECT_ID].appspot.com訪問您的項目。
When building production ready applications, you often want to test your code before pushing it live. One way to do this is to run your App within a server locally. A better approach is to have a development version of the app which can be tested not only from your local machine but also from a hosted environment. You can use Google App Engine versions for this.
在構建可用于生產環境的應用程序時,您通常需要先測試代碼,然后再將其發布。 一種方法是在本地服務器上運行您的應用程序。 更好的方法是擁有該應用程序的開發版本,該版本不僅可以在本地計算機上進行測試,還可以在托管環境中進行測試。 您可以為此使用Google App Engine版本 。
Just deploy your App with gcloud app deploy -v textdev (for development) or gcloud app deploy -v textprod (for production).
只需使用gcloud app deploy -v textdev (用于開發)或gcloud app deploy -v textprod (用于生產)來部署您的App。
Then navigate to https://textdev.[YOUR_PROJECT_ID].appspot.com or https://textprod.[YOUR_PROJECT_ID].appspot.com to access the specific version.
然后導航到https://textdev.[YOUR_PROJECT_ID].appspot.com或https://textprod.[YOUR_PROJECT_ID].appspot.com以訪問特定版本。
縮放到無窮大 (Scale to infinity)
So far so good. You have a working application, hosted on the Google Cloud Platform. Now you need to add Google Cloud Pub/Sub and Google Natural Language API.
到目前為止,一切都很好。 您有一個正在運行的應用程序,該應用程序托管在Google Cloud Platform上。 現在,您需要添加Google Cloud Pub / Sub和Google自然語言API 。
But first, let’s explain the architecture.
但是首先,讓我們解釋一下架構。
Once a request is received, the Flask app will publish a message with the text to a topic (created below). Then a subscriber (Python script) will pull this message and apply the Google Natural Language API to each text. Finally, the result will be saved to a database.
收到請求后,Flask應用程序將向主題發布消息(文本如下)。 然后,訂閱者(Python腳本)將拉出此消息,并將Google自然語言API應用于每個文本。 最后,結果將保存到數據庫中。
For multiple requests, the app asynchronously publishes them to the topic and the subscriber starts executing the first one. When ready, it picks the second one and so on.
對于多個請求,應用程序將它們異步發布到主題,訂閱者開始執行第一個請求。 準備就緒后,它將選擇第二個,依此類推。
Now you need to modify text.py file:
現在您需要修改text.py文件:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網站,似乎不再可用
The code on line 15 and 16 creates the publisher. On line 18 it publishes a message containing the user email and text input.
第15和16行上的代碼創建發布者。 在第18行,它發布了一條包含用戶電子郵件和文本輸入的消息。
You only need to fill in the project_id and topic_id (line 6 and 7).
您只需要填寫project_id和topic_id (第6行和第7行)。
Since the project_id was used earlier, just add it here.
由于先前使用了project_id ,因此只需在此處添加即可。
For the topic_id you need to do the following:
對于topic_id您需要執行以下操作:
Enable Google Cloud Pub/Sub API
啟用Google Cloud Pub / Sub API
Go to the Pub/Sub page of your project
轉到項目的發布/訂閱頁面
- Create a topic and a subscription 創建主題和訂閱
Use the topic name as your topic_id
使用主題名稱作為您的topic_id
Keep the subscription name for later.
保留訂閱名稱以備后用。
You will need it as your
您將需要它作為您的
subscription_id
subscription_id
Wonderful! Now you have a working publisher.
精彩! 現在您有了一個正在工作的發布者。
Let’s jump into setting up the subscriber. There are two files that need to be created: worker.py and startup-script.sh.
讓我們進入設置訂戶的過程。 需要創建兩個文件: worker.py和startup-script.sh 。
The worker.py looks like this:
worker.py看起來像這樣:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網站,似乎不再可用
The file is slightly larger but we will examine it step-by-step, starting from the bottom.
該文件稍大,但我們將從底部開始逐步進行檢查。
When the file is executed, the code on line 44 runs main(). This function sets the subscriber with your project_id and subscription_id and assigns a callback to it.
執行文件后,第44行的代碼運行main() 。 此函數為訂戶設置您的project_id和subscription_id并為其分配回調。
The callback (initialized on line 7) is going to receive all messages and perform the required task (to determine the category of a text). If you follow the code from the callback, you can easily see how the Google Natural Language API is being used.
callback (在第7行初始化)將接收所有消息并執行所需的任務(以確定文本的類別)。 如果您遵循callback的代碼,則可以輕松了解Google Natural Language API的使用方式。
The interesting line is 11 where message.ack() acknowledges the current message. You can see this is as if the worker is saying: “I am done with this message and ready to handle the next one”.
有趣的行是11,其中message.ack()確認當前消息。 您可以看到,好像工作人員在說:“我已經完成了此消息,并準備處理下一條消息”。
Now, you need to implement startup-script.sh.
現在,您需要實現startup-script.sh 。
This is a shell script with several commands:
這是一個帶有幾個命令的shell腳本:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網站,似乎不再可用
Before explaining the code above, I need to clarify the process.
在解釋上面的代碼之前,我需要澄清一下過程。
Basically, Google Cloud Compute Engine gives you the ability to scale an application by providing as many virtual machines (VM) as needed to run several workers simultaneously.
基本上, Google Cloud Compute Engine通過提供所需數量的虛擬機(VM)來同時運行多個工作程序,使您能夠擴展應用程序。
You just need to add the code for the worker, which you already have, and set the configurations of the VM. Together with the worker.py, you also need to add a startup-script.sh which will run every time a new VM boots up.
您只需要添加已經具有的工作程序代碼,然后設置VM的配置即可。 與worker.py一起,您還需要添加一個startup-script.sh ,它將在每次啟動新VM時運行。
New VM instances are booted up to prevent delay in responses when a high number of messages is received.
啟動新的VM實例以防止收到大量消息時響應延遲。
For a deeper and more technical explanation of this process check out the documentation.
有關此過程的更詳細的技術說明,請查閱文檔 。
Now, let me walk you through the script:
現在,讓我引導您完成腳本:
Line 1: means that the script should always be run with bash, rather than another shell.
第1行 :意味著腳本應始終使用bash運行,而不是其他shell。
Lines 2 and 3: creates and enters into a new directory where all of the files will be stored.
第2和3行 :創建并進入一個新目錄,其中將存儲所有文件。
Line 4: copies the worker.py file from Google Cloud Storage into the VM (I will explain how to upload your files to the storage below).
第4行 :將worker.py文件從Google Cloud Storage復制到VM(我將說明如何將文件上傳到下面的存儲)。
Line 5: here you need to specify a JSON string of your key so that Google can verify your credentials. In order to get this string you need to create a service account. Select Furnish a new private key and for Key type use JSON. A file will be downloaded to your computer. Copy the content and turn it into a JSON string (using JSON.stringify(key_in_json_format) in a browser console). Paste it instead of SERVICE_ACCOUNT_KEY.
第5行 :您需要在此處指定密鑰的JSON字符串,以便Google可以驗證您的憑據。 為了獲得此字符串,您需要創建一個服務帳戶 。 選擇Furnish a new private key 和對于Key type 使用JSON 。 文件將下載到您的計算機。 復制內容并將其轉換為JSON字符串(在瀏覽器控制臺中使用JSON.stringify(key_in_json_format) )。 粘貼它而不是SERVICE_ACCOUNT_KEY 。
Line 6: exports the key as an environment variable which will be used by the Google APIs to verify your credentials.
第6行 :將密鑰導出為環境變量 ,Google API將使用該變量來驗證您的憑據。
Lines 7 - 12: sets up configurations and installs the python libraries.
第7至12行 :設置配置并安裝python庫。
Line 15: runs the worker.
第15行 :管理工人。
Now you need to upload worker.py and startup-script.sh to your storage and set up the VM. To upload the files just go here and create a new bucket with the same name as your project id. Create a folder called workers and upload the scripts inside. Make sure to change the worker.py to a ‘Public link’ and edit the permissions of the startup-script.sh to have your service account as an owner.
現在,您需要將worker.py和startup-script.sh上傳到您的存儲設備并設置VM。 要上傳文件,只需在此處創建一個與您的項目ID同名的新存儲桶。 創建一個名為worker的文件夾,然后在其中上傳腳本。 確保將worker.py更改為“公共鏈接”并編輯startup-script.sh的權限 至 將您的服務帳戶作為所有者。
配置和測試 (Configurations and testing)
The final step is to set up the configurations of the VM and test the system. Just follow the ‘Create an instance template’ instructions from the documentation and you are good to go!
最后一步是設置VM的配置并測試系統。 只需按照文檔中的“創建實例模板”說明進行操作,就可以了!
Once the VM boots up, you can try sending requests to your application and examine how it reacts by checking the logs.
VM啟動后,您可以嘗試將請求發送到應用程序,并通過檢查日志來檢查其React。
最后的想法 (Final thoughts)
Going through Google’s documentation may help you a lot. Also check out this tutorial - you may find it useful while implementing some of the steps above.
仔細閱讀Google的文檔可能會對您有所幫助。 另外,請查看本教程 -在實施上述某些步驟時,您可能會發現它很有用。
I want to express my gratefulness to Logan Allen for helping me better understand this process. I hope you find it useful.
我要感謝洛根·艾倫 ( Logan Allen)幫助我更好地理解這一過程。 希望對你有幫助。
Leave any questions or suggestions in the comment section.
在評論部分留下任何問題或建議。
翻譯自: https://www.freecodecamp.org/news/decentralize-your-application-with-google-cloud-platform-7149ec6d0255/
總結
以上是生活随笔為你收集整理的使用Google Cloud Platform分散您的应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到往高处爬是什么意思
- 下一篇: 小程序 graphql_GraphQL应