使用Google Cloud Platform分散您的应用程序
by Simeon Kostadinov
通過Simeon Kostadinov
使用Google Cloud Platform分散您的應(yīng)用程序 (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.
首次啟動(dòng)新軟件項(xiàng)目時(shí),通常會(huì)選擇某種編程語言,特定框架和庫。 然后開始編碼。 2-3個(gè)月后,您將獲得一個(gè)運(yùn)行良好的單個(gè)應(yīng)用程序。
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.
但是,隨著項(xiàng)目的發(fā)展和更多功能的添加,您很快就會(huì)意識到集中式系統(tǒng)的弊端。 難以維護(hù)和不可擴(kuò)展是某些使您尋找更好的解決方案的原因。 這是微服務(wù)提供幫助的地方。
什么是微服務(wù)? (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.
微服務(wù)是獨(dú)立構(gòu)建的系統(tǒng),每個(gè)系統(tǒng)都在各自的進(jìn)程中運(yùn)行,并經(jīng)常與REST API通信。 代表應(yīng)用程序的不同部分,它們可以分別部署,并且每個(gè)部分都可以用任何語言編寫。
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.
您可以輕松地看到,通過處理單片系統(tǒng)的問題,微服務(wù)已成為任何最新軟件的要求。
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.
如果您想更深入地了解這種體系結(jié)構(gòu)樣式中的關(guān)鍵概念,我強(qiáng)烈建議您閱讀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.
本文將引導(dǎo)您完成使用Google Cloud Platform實(shí)施微服務(wù)的過程。
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.
假設(shè)您正在開發(fā)一個(gè)應(yīng)用程序,該應(yīng)用程序接受用戶的文本輸入并確定輸入中關(guān)鍵字的類別。
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:
我們將使用一個(gè)示例來說明該應(yīng)用程序的功能。 考慮以下來自GCP Cloud Natural Language API網(wǎng)站的示例文本:
“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在消費(fèi)者電子展上推出了新的Android手機(jī)。 Sundar Pichai在主題演講中說,用戶喜歡他們的新Android手機(jī)。”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應(yīng)用程序?qū)⒔邮苌厦娴奈谋咀鳛檩斎?#xff0c;并返回關(guān)鍵字所屬的類別,如下圖所示:
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.
此功能相當(dāng)不錯(cuò),人們每天使用數(shù)百次。 現(xiàn)在,如果您要提供此功能作為一項(xiàng)接收大量日常流量的服務(wù),則希望以穩(wěn)定可靠的系統(tǒng)進(jìn)行響應(yīng)。
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.
因此,我們將構(gòu)建一個(gè)輕量級的Flask應(yīng)用程序,該應(yīng)用程序托管在Google App Engine上 。 將其與Google Cloud Pub / Sub集成在一起將幫助我們處理收到的所有異步請求,并確保用戶不會(huì)等待太久就等待響應(yīng)。
創(chuàng)建和部署應(yīng)用程序 (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應(yīng)用程序開始(您也可以選擇Django,Node.js,Go或用于構(gòu)建服務(wù)器端應(yīng)用程序的任何東西)。 如果您對開發(fā)Flask應(yīng)用程序不太熟悉,則此Flask系列可以逐步向您展示如何設(shè)置應(yīng)用程序。
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
此嵌入來自外部網(wǎng)站,似乎不再可用
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.
首先,您需要安裝依賴項(xiàng)pip install Flask gunicorn 。 您將使用gunicorn在Google App Engine上運(yùn)行該應(yīng)用程序。 對于本地訪問,您可以在控制臺中運(yùn)行python text.py并在端口8080上找到該應(yīng)用程序。
To deploy the app to Google App Engine, you need to take these steps:
要將應(yīng)用程序部署到Google App Engine,您需要執(zhí)行以下步驟:
Create a project (follow the ‘Before you begin’ instructions from the documentation). Save the project id for later.
創(chuàng)建一個(gè)項(xiàng)目(按照文檔中的“開始之前”的說明進(jìn)行操作 )。 保存項(xiàng)目ID,以備后用。
Create app.yaml file (shown below), which is used by the Google App Engine to recognize the application.
創(chuàng)建app.yaml文件(如下所示),Google App Engine使用該文件來識別該應(yīng)用程序。
Run gcloud app deploy in the console.
在控制臺中運(yùn)行g(shù)cloud 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
此嵌入來自外部網(wǎng)站,似乎不再可用
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的文件中運(yùn)行應(yīng)用app (Flask應(yīng)用) 。 您可以在此處了解有關(guān).yaml文件結(jié)構(gòu)的更多信息。 部署后,您應(yīng)該可以從https://[YOUR_PROJECT_ID].appspot.com訪問您的項(xiàng)目。
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.
在構(gòu)建可用于生產(chǎn)環(huán)境的應(yīng)用程序時(shí),您通常需要先測試代碼,然后再將其發(fā)布。 一種方法是在本地服務(wù)器上運(yùn)行您的應(yīng)用程序。 更好的方法是擁有該應(yīng)用程序的開發(fā)版本,該版本不僅可以在本地計(jì)算機(jī)上進(jìn)行測試,還可以在托管環(huán)境中進(jìn)行測試。 您可以為此使用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 (用于開發(fā))或gcloud app deploy -v textprod (用于生產(chǎn))來部署您的App。
Then navigate to https://textdev.[YOUR_PROJECT_ID].appspot.com or https://textprod.[YOUR_PROJECT_ID].appspot.com to access the specific version.
然后導(dǎo)航到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.
到目前為止,一切都很好。 您有一個(gè)正在運(yùn)行的應(yīng)用程序,該應(yīng)用程序托管在Google Cloud Platform上。 現(xiàn)在,您需要添加Google Cloud Pub / Sub和Google自然語言API 。
But first, let’s explain the architecture.
但是首先,讓我們解釋一下架構(gòu)。
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應(yīng)用程序?qū)⑾蛑黝}發(fā)布消息(文本如下)。 然后,訂閱者(Python腳本)將拉出此消息,并將Google自然語言API應(yīng)用于每個(gè)文本。 最后,結(jié)果將保存到數(shù)據(jù)庫中。
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.
對于多個(gè)請求,應(yīng)用程序?qū)⑺鼈儺惒桨l(fā)布到主題,訂閱者開始執(zhí)行第一個(gè)請求。 準(zhǔn)備就緒后,它將選擇第二個(gè),依此類推。
Now you need to modify text.py file:
現(xiàn)在您需要修改text.py文件:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網(wǎng)站,似乎不再可用
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行上的代碼創(chuàng)建發(fā)布者。 在第18行,它發(fā)布了一條包含用戶電子郵件和文本輸入的消息。
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您需要執(zhí)行以下操作:
Enable Google Cloud Pub/Sub API
啟用Google Cloud Pub / Sub API
Go to the Pub/Sub page of your project
轉(zhuǎn)到項(xiàng)目的發(fā)布/訂閱頁面
- Create a topic and a subscription 創(chuàng)建主題和訂閱
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.
精彩! 現(xiàn)在您有了一個(gè)正在工作的發(fā)布者。
Let’s jump into setting up the subscriber. There are two files that need to be created: worker.py and startup-script.sh.
讓我們進(jìn)入設(shè)置訂戶的過程。 需要?jiǎng)?chuàng)建兩個(gè)文件: 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
此嵌入來自外部網(wǎng)站,似乎不再可用
The file is slightly larger but we will examine it step-by-step, starting from the bottom.
該文件稍大,但我們將從底部開始逐步進(jìn)行檢查。
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.
執(zhí)行文件后,第44行的代碼運(yùn)行main() 。 此函數(shù)為訂戶設(shè)置您的project_id和subscription_id并為其分配回調(diào)。
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行初始化)將接收所有消息并執(zhí)行所需的任務(wù)(以確定文本的類別)。 如果您遵循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()確認(rèn)當(dāng)前消息。 您可以看到,好像工作人員在說:“我已經(jīng)完成了此消息,并準(zhǔn)備處理下一條消息”。
Now, you need to implement startup-script.sh.
現(xiàn)在,您需要實(shí)現(xiàn)startup-script.sh 。
This is a shell script with several commands:
這是一個(gè)帶有幾個(gè)命令的shell腳本:
This embed is from an external site and no longer seems to be available
此嵌入來自外部網(wǎng)站,似乎不再可用
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通過提供所需數(shù)量的虛擬機(jī)(VM)來同時(shí)運(yùn)行多個(gè)工作程序,使您能夠擴(kuò)展應(yīng)用程序。
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.
您只需要添加已經(jīng)具有的工作程序代碼,然后設(shè)置VM的配置即可。 與worker.py一起,您還需要添加一個(gè)startup-script.sh ,它將在每次啟動(dòng)新VM時(shí)運(yùn)行。
New VM instances are booted up to prevent delay in responses when a high number of messages is received.
啟動(dòng)新的VM實(shí)例以防止收到大量消息時(shí)響應(yīng)延遲。
For a deeper and more technical explanation of this process check out the documentation.
有關(guān)此過程的更詳細(xì)的技術(shù)說明,請查閱文檔 。
Now, let me walk you through the script:
現(xiàn)在,讓我引導(dǎo)您完成腳本:
Line 1: means that the script should always be run with bash, rather than another shell.
第1行 :意味著腳本應(yīng)始終使用bash運(yùn)行,而不是其他shell。
Lines 2 and 3: creates and enters into a new directory where all of the files will be stored.
第2和3行 :創(chuàng)建并進(jìn)入一個(gè)新目錄,其中將存儲(chǔ)所有文件。
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復(fù)制到VM(我將說明如何將文件上傳到下面的存儲(chǔ))。
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可以驗(yàn)證您的憑據(jù)。 為了獲得此字符串,您需要?jiǎng)?chuàng)建一個(gè)服務(wù)帳戶 。 選擇Furnish a new private key 和對于Key type 使用JSON 。 文件將下載到您的計(jì)算機(jī)。 復(fù)制內(nèi)容并將其轉(zhuǎn)換為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行 :將密鑰導(dǎo)出為環(huán)境變量 ,Google API將使用該變量來驗(yàn)證您的憑據(jù)。
Lines 7 - 12: sets up configurations and installs the python libraries.
第7至12行 :設(shè)置配置并安裝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.
現(xiàn)在,您需要將worker.py和startup-script.sh上傳到您的存儲(chǔ)設(shè)備并設(shè)置VM。 要上傳文件,只需在此處創(chuàng)建一個(gè)與您的項(xiàng)目ID同名的新存儲(chǔ)桶。 創(chuàng)建一個(gè)名為worker的文件夾,然后在其中上傳腳本。 確保將worker.py更改為“公共鏈接”并編輯startup-script.sh的權(quán)限 至 將您的服務(wù)帳戶作為所有者。
配置和測試 (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!
最后一步是設(shè)置VM的配置并測試系統(tǒng)。 只需按照文檔中的“創(chuàng)建實(shí)例模板”說明進(jìn)行操作,就可以了!
Once the VM boots up, you can try sending requests to your application and examine how it reacts by checking the logs.
VM啟動(dòng)后,您可以嘗試將請求發(fā)送到應(yīng)用程序,并通過檢查日志來檢查其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.
仔細(xì)閱讀Google的文檔可能會(huì)對您有所幫助。 另外,請查看本教程 -在實(shí)施上述某些步驟時(shí),您可能會(huì)發(fā)現(xiàn)它很有用。
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/
總結(jié)
以上是生活随笔為你收集整理的使用Google Cloud Platform分散您的应用程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到往高处爬是什么意思
- 下一篇: 小程序 graphql_GraphQL应