使用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.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 / 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_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您需要执行以下操作:

  • 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.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/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394948.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pta通讯录排序用python实现,python实现将android手机通讯录vcf文件转化为csv

经常会遇到将手机通讯录导出到电脑并转化为在电脑中可编辑的情况,在网上搜索了很久当前不外乎两种处理方式。1.使用电脑的outlook的通讯簿功能,将手机导出的vcf文件导入到outlook的通讯录中,然后再导出为可编辑文件;2.是使用专用软…

从物联网发展历程看区块链挑战

2009年,中本聪发布了第一个比特币节点,五年后区块链俨然成为一个规模巨大的产业。 虽然看起来,基于区块链的新的商业时代距离我们似乎只有一步之遥,但在2016年,我们已经意识到区块链产业不会那么快获得成功。 早期的新…

编程软件python是什么意思_程序员Python编程必备5大工具,你用过几个?

Python是编程入门不错的选择,现在也有不少的程序员业余时间会研究这门编程语言。 学习Python有时候没有第一时间找到好工具,会吃不少的苦头。毕竟好的工具能将工作效率多倍速提升。下面W3Cschool给小伙伴们推荐Python编程必备的5大工具: 0、I…

Linux ReviewBoard安装与配置

目录 0. 引言1. 安装步骤2. 配置站点 2.1 创建数据库2.2 开始安装2.3 修改文件访问权限2.4 Web服务器配置2.5 修改django相关配置正文 回到顶部0. 引言 环境:Ubuntu 14.04 Server(虚拟机) 这篇文章里说的是review board官方的安装方式&#x…

小程序 graphql_GraphQL应用程序中的五个常见问题(以及如何解决)

小程序 graphqlby Sacha Greif由Sacha Greif GraphQL应用程序中的五个常见问题(以及如何解决) (Five Common Problems in GraphQL Apps (And How to Fix Them)) 了解如何释放GraphQL的强大功能而不会遭受缺点 (Learn to unlock the power of GraphQL without suffering its dr…

又拍云 php5月18号那,又拍云文档中心

移动流量平台概述又拍云手机流量营销平台,整合移动、电信、联通三大运营商流量资源,将强大的流量营销服务,通过接口等形式提供给商家合作伙伴,帮助商家开展品牌宣传、APP/游戏/微信公众号/网站的拉新与促活等多种营销活动。通过接…

SQL SERVER2000将多行查询结果拼接到一行数据及函数的创建

处理前的查询结果如上图: 通过借助SQL变量的定义 DECLARE Scope varchar(1000) DECLARE Num int SET Scope SET Num 1 SELECT ScopeScopeconvert(varchar(8),Num)、DescScope DescOper;, Num Num1 From fuel_contractQualityNew Where ContractID0120090001…

kindeditor简单使用

先引入&#xff1a; <script src"/static/jquery-3.3.1.min.js"></script><script src"/static/kindeditor-4.1.11-zh-CN/kindeditor/kindeditor-all.js"></script> 基本使用参数&#xff1a; $(function () {KindEditor.create(#…

windows nginx c++读取请求数据_轻松应对百万并发的Nginx,搞懂LinuxC/C++这些技术栈升职加薪...

在深入了解 Nginx 各种原理及在极端场景下的一些错误场景处理时&#xff0c;需要首先理解什么是网络事件。Nginx 是一个事件驱动的框架&#xff0c;所谓事件主要指的是网络事件&#xff0c;Nginx 每个网络连接会对应两个网络事件&#xff0c;一个读事件一个写事件。在深入了解 …

github 6月开源项目_我的开源项目如何在短短5天内在GitHub上赢得6,000颗星

github 6月开源项目Last month I launched two open source projects on GitHub. A few days later, my Front-End Checklist was showing more than 6,000 stars (17,000 as of writing). And I got 600 stars for my Resources-Front-End-Beginner project!上个月&#xff0c…

如何成为一位牛逼的高手

郑昀 演讲稿 创建于2016/9/15 最后更新于2016/9/21 很多人都在思考一个问题&#xff1a; 怎样才能想出来一个牛逼的 idea&#xff1f; 有一位喜欢抽烟喝酒烫头的大师 给出了答案&#xff1a; 这事儿吧&#xff0c;简单&#xff0c;一共分两步。 第一步先让自己成为一个牛逼的人…

thinkphp html php文件,ThinkPHP生成静态HTML文件

View.class.php/*** 加载模板和页面输出 可以返回输出内容* access public* param string $templateFile 模板文件名* param string $charset 模板输出字符集* param string $contentType 输出类型* param string $content 模板输出内容* param string $prefix 模板缓存前缀* r…

day01语法python入门_2

十&#xff1a;while循环 1.基本循环 while条件#循环体#如果条件为真&#xff0c;那么循环体则执行#如果条件为假&#xff0c;那么循环体不执行。2.break break 用于退出所有循环 while True:print "123"breakprint "456"3.continue while True:print &quo…

Python dict() 函数

Python dict() 函数 Python 内置函数 描述 dict() 函数用于创建一个字典。 语法 dict 语法&#xff1a; class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) 参数说明&#xff1a; **kwargs -- 关键字mapping -- 元素的容器。iterable -- 可迭代…

贝塞尔曲线:原理、自定义贝塞尔曲线View、使用!!!

一、原理 转自&#xff1a;http://www.2cto.com/kf/201401/275838.html Android动画学习Demo(3) 沿着贝塞尔曲线移动的Property Animation Property Animation中最重要&#xff0c;最基础的一个类就是ValueAnimator了。Property Animation利用ValueAnimator来跟踪记录对象属性已…

ios pusher使用_如何使用JavaScript和Pusher构建实时图

ios pusher使用by Rahat Khanna通过拉哈特汉娜 如何使用JavaScript和Pusher构建实时图 (How to build a Realtime Graph using JavaScript and Pusher) The world needs everything uber-fast now. There are plenty of data streams being generated by different systems ev…

python数据分析与基础实战_《python数据分析与挖掘实战》基础概念

数据建模.png 数据挖掘的基本任务:利用分类与预测、聚类分析、关联规则、时序模式、偏差检测、智能推荐等方法&#xff0c;帮助企业提取数据中蕴含的商业价值&#xff0c;提高企业竞争力。 数据探索&#xff1a;异常值分析、缺失值分析、相关分析和周期性分析。 数据预处理:数据…

简述JAVA线程调度的原理,Rxjava原理(二)--线程调度

1. 创建线程池和线程管理策略分析// 在开发中使用Rxjava来完成线程切换会调用到以下方法(还有几个就不一一列举了&#xff0c;原理一样的)&#xff0c;那么就从这里开始分析Schedulers.io()Schedulers.computation()Schedulers.newThread()AndroidSchedulers.mainThread()当我们…

[前端随笔][css] 弹性布局

说在前面 弹性布局&#xff0c;顾名思义就是有弹性&#xff0c;能够根据屏幕/当前空间大小自由伸缩的。使用弹性布局可以很好的适应各种尺寸的客户端。 关键代码 display:flex;    设定元素为弹性布局  <文档传送门> box-flex: 参数;   设定元素为弹性布局  &…

不同的模块中定义同样的宏为不同的值合法吗_如何创建自定义的建模规范

本文摘要&#xff1a;主要介绍如何创建自定义的建模规范检查&#xff0c;以及在建模规范检查中&#xff0c;如何增加自动修正模型使之符合规范。比如我们想创建一个自定义的规则&#xff0c;对于constant模块&#xff0c;1. 如果value是参数的话&#xff0c;则输出数据类型必须…