使用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,您需要执行以下步骤:

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.comhttps://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 / SubGoogle自然语言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_idtopic_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您需要执行以下操作:

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.pystartup-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_idsubscription_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 linkand edit the permissions of the startup-script.sh to have your service account as an owner.

现在,您需要将worker.pystartup-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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值