Merge pull request #4626 from firstadream/master

翻译完成
This commit is contained in:
Xingyu.Wang 2016-11-06 22:05:09 +08:00 committed by GitHub
commit 1a85c2e8e3
2 changed files with 142 additions and 144 deletions

View File

@ -1,144 +0,0 @@
Translating by Firstadream
Rapid prototyping with docker-compose
========================================
In this write-up we'll look at a Node.js prototype for **finding stock of the Raspberry PI Zero** from three major outlets in the UK.
I wrote the code and deployed it to an Ubuntu VM in Azure within a single evening of hacking. Docker and the docker-compose tool made the deployment and update process extremely quick.
### Remember linking?
If you've already been through the [Hands-On Docker tutorial][1] then you will have experience linking Docker containers on the command line. Linking a Node hit counter to a Redis server on the command line may look like this:
```
$ docker run -d -P --name redis1
$ docker run -d hit_counter -p 3000:3000 --link redis1:redis
```
Now imagine your application has three tiers
- Web front-end
- Batch tier for processing long running tasks
- Redis or mongo database
Explicit linking through `--link` is just about manageable with a couple of containers, but can get out of hand as we add more tiers or containers to the application.
### Enter docker-compose
![](http://blog.alexellis.io/content/images/2016/05/docker-compose-logo-01.png)
>Docker Compose logo
The docker-compose tool is part of the standard Docker Toolbox and can also be downloaded separately. It provides a rich set of features to configure all of an application's parts through a plain-text YAML file.
The above example would look like this:
```
version: "2.0"
services:
redis1:
image: redis
hit_counter:
build: ./hit_counter
ports:
- 3000:3000
```
From Docker 1.10 onwards we can take advantage of network overlays to help us scale out across multiple hosts. Prior to this linking only worked across a single host. The `docker-compose scale` command can be used to bring on more computing power as the need arises.
>View the [docker-compose][2] reference on docker.com
### Real-world example: Raspberry PI Stock Alert
![](http://blog.alexellis.io/content/images/2016/05/Raspberry_Pi_Zero_ver_1-3_1_of_3_large.JPG)
>The new Raspberry PI Zero v1.3 image courtesy of Pimoroni
There is a huge buzz around the Raspberry PI Zero - a tiny microcomputer with a 1GHz CPU and 512MB RAM capable of running full Linux, Docker, Node.js, Ruby and many other popular open-source tools. One of the best things about the PI Zero is that costs only 5 USD. That also means that stock gets snapped up really quickly.
*If you want to try Docker or Swarm on the PI check out the tutorial below.*
>[Docker Swarm on the PI Zero][3]
### Original site: whereismypizero.com
I found a webpage which used screen scraping to find whether 4-5 of the most popular outlets had stock.
- The site contained a static HTML page
- Issued one XMLHttpRequest per outlet accessing /public/api/
- The server issued the HTTP request to each shop and performed the scraping
Every call to /public/api/ took 3 seconds to execute and using Apache Bench (ab) I was only able to get through 0.25 requests per second.
### Reinventing the wheel
The retailers didn't seem to mind whereismypizero.com scraping their sites for stock, so I set about writing a similar tool from the ground up. I had the intention of handing a much higher amount of requests per second through caching and de-coupling the scrape from the web tier. Redis was the perfect tool for the job. It allowed me to set an automatically expiring key/value pair (i.e. a simple cache) and also to transmit messages between Node processes through pub/sub.
>Fork or star the code on Github: [alexellis/pi_zero_stock][4]
If you've worked with Node.js before then you will know it is single-threaded and that any CPU intensive tasks such as parsing HTML or JSON could lead to a slow-down. One way to mitigate that is to use a second worker process and a Redis messaging channel as connective tissue between this and the web tier.
- Web tier
-Gives 200 for cache hit (Redis key exists for store)
-Gives 202 for cache miss (Redis key doesn't exist, so issues message)
-Since we are only ever reading a Redis key the response time is very quick.
- Stock Fetcher
-Performs HTTP request
-Scrapes for different types of web stores
-Updates a Redis key with a cache expire of 60 seconds
-Also locks a Redis key to prevent too many in-flight HTTP requests to the web stores.
```
version: "2.0"
services:
web:
build: ./web/
ports:
- "3000:3000"
stock_fetch:
build: ./stock_fetch/
redis:
image: redis
```
*The docker-compose.yml file from the example.*
Once I had this working locally deploying to an Ubuntu 16.04 image in the cloud (Azure) took less than 5 minutes. I logged in, cloned the repository and typed in `docker compose up -d`. That was all it took - rapid prototyping a whole system doesn't get much better. Anyone (including the owner of whereismypizero.com) can deploy the new solution with just two lines:
```
$ git clone https://github.com/alexellis/pi_zero_stock
$ docker-compose up -d
```
Updating the site is easy and just involves a `git pull` followed by a `docker-compose up -d` with the `--build` argument passed along.
If you are still linking your Docker containers manually, try Docker Compose for yourself or my code below:
>Fork or star the code on Github: [alexellis/pi_zero_stock][5]
### Check out the test site
The test site is currently deployed now using docker-compose.
>[stockalert.alexellis.io][6]
![](http://blog.alexellis.io/content/images/2016/05/Screen-Shot-2016-05-16-at-22-34-26-1.png)
Preview as of 16th of May 2016
----------
via: http://blog.alexellis.io/rapid-prototype-docker-compose/
作者:[Alex Ellis][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://blog.alexellis.io/author/alex/
[1]: http://blog.alexellis.io/handsondocker
[2]: https://docs.docker.com/compose/compose-file/
[3]: http://blog.alexellis.io/dockerswarm-pizero/
[4]: https://github.com/alexellis/pi_zero_stock
[5]: https://github.com/alexellis/pi_zero_stock
[6]: http://stockalert.alexellis.io/

View File

@ -0,0 +1,142 @@
通过docker-compose进行快速原型设计
========================================
在这篇文章中我们将考察一个Node.js原型该原型用于从英国三个主要折扣网店查找“Raspberry PI Zero”的库存。
我写好了代码然后经过一晚的hacking把它部署在Aure上的Ubuntu虚拟机上。Docker和docker-compose工具使得部署和更新过程非常快。
### 还记得链接指令吗?
如果你已经阅读过[Hands-on Docker tutorial][1]那么你应该已经可以使用命令行链接Docker容器。通过命令行将节点命中计数器链接到Redis服务器其命令可能如下所示
```
$ docker run -d -P --name redis1
$ docker run -d hit_counter -p 3000:3000 --link redis1:redis
```
现在假设你的应用程序分为三层:
- Web前端
- 处理长时间运行任务的批处理层
- Redis或者mongo数据库
通过`--link`的显式链接只是可以用几个容器来管理,但是可能会因为我们向应用程序添加更多层或容器而失控。
###加入docker-compose
![](http://blog.alexellis.io/content/images/2016/05/docker-compose-logo-01.png)
>Docker Compose logo
docker-compose工具是标准Docker工具箱的一部分也可以单独下载。 它提供了一组丰富的功能通过纯文本YAML文件配置所有应用程序的部件。
上面的例子看起来像这样:
```
version: "2.0"
services:
redis1:
redis1:
image: redis
hit_counter:
build: ./hit_counter
ports:
- 3000:3000
```
从Docker 1.10开始,我们可以利用网络覆盖来帮助我们在多个主机上进行扩展。 在此之前,链接仅在单个主机上工作。 “docker-compose scale”命令可以用来在需要时带来更多的计算能力。
>查看docker.com上的[docker-compose] [2]参考
真实工作示例Raspberry PI股票警示
![](http://blog.alexellis.io/content/images/2016/05/Raspberry_Pi_Zero_ver_1-3_1_of_3_large.JPG)
>新的Raspberry PI Zero v1.3图片由Pimoroni提供
Raspberry PI Zero嗡嗡作响 - 它是一个极小的微型计算机具有1GHz CPU和512MB RAM可以运行完整的Linux、Docker、Node.js、Ruby和其他许多流行的开源工具。 PI Zero最好的优点之一就是它成本只有5美元。 这也意味着它去库存的速度非常之快。
*如果你想在PI上尝试Docker和Swarm请查看下面的教程。*
>[Docker Swarm on the PI Zero][3]
### 原始网站whereismypizero.com
我发现一个网页它使用屏幕抓取以找出4-5个最受欢迎的折扣网店是否有库存。
- 网站包含静态HTML网页
- 向每个折扣网店发出一个XMLHttpRequest访问/ public / api /
- 服务器向每个网店发出HTTP请求并执行抓屏
每一次对/ public / api /的调用其执行花3秒钟而使用Apache Benchab我每秒只能得到0.25个请求。
### 重新发明轮子
零售商似乎并不介意whereismypizero.com抓取他们的网站的商品库存信息所以我开始从头写一个类似的工具。 我有意通过缓存和解耦从web层处理更多的抓取请求。 Redis是执行这项工作的完美工具。 它允许我设置一个自动过期的键/值对即一个简单的缓存还可以通过pub / sub在节点进程之间传输消息。
>复刻或者追踪github上的代码: [alexellis/pi_zero_stock][4]
如果之前使用过Node.js你肯定知道它是单线程的并且任何CPU密集型任务如解析HTML或JSON都可能导致速度放缓。 一种缓解这种情况的方法是使用一个工作进程和Redis消息通道作为它和web层之间的连接组织。
- Web层
使用200作为缓冲命中存储Redis键
使用202作为高速缓存未命中Redis键不存在因此出现问题消息
因为我们只是读一个Redis键响应时间非常快。
- 库存抓取器
执行HTTP请求
用于在不同类型的网店上抓屏
更新具有高速缓存超时时间为60秒的Redis键
另外锁定一个Redis键以防止过多的HTTP请求到网店。
```
version: "2.0"
services:
web:
build: ./web/
ports:
- "3000:3000"
stock_fetch:
build: ./stock_fetch/
redis:
image: redis
```
*来自示例的docker-compose.yml文件*
一旦本地正常工作再向Azure的Ubuntu 16.04镜像云部署就轻车熟路只花了不到5分钟。 我登录克隆仓库并键入“docker compose up -d” 这就是所有的工作 - 快速实现整个系统的原型不会比这几个步骤更多。 任何人包括whereismypizero.com的所有者只需两行命令就可以部署新解决方案
```
$ git clone https://github.com/alexellis/pi_zero_stock
$ docker-compose up -d
```
更新网站很容易,只需要一个`git pull'`命令,然后执行`docker-compose up -d`命令,该命令需要带上`--build`参数。
如果你仍然手动链接你的Docker容器请自己或使用如下我的代码尝试Docker Compose
>复刻或者追踪github上的代码 [alexellis/pi_zero_stock][5]
###一睹测试网站芳容
目前测试网站使用docker-compose部署。
>[stockalert.alexellis.io][6]
![](http://blog.alexellis.io/content/images/2016/05/Screen-Shot-2016-05-16-at-22-34-26-1.png)
预览截止2016年5月16日
----------
via: http://blog.alexellis.io/rapid-prototype-docker-compose/
作者:[Alex Ellis][a]
译者:[firstadream](https://github.com/firstadream)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://blog.alexellis.io/author/alex/
[1]: http://blog.alexellis.io/handsondocker
[2]: https://docs.docker.com/compose/compose-file/
[3]: http://blog.alexellis.io/dockerswarm-pizero/
[4]: https://github.com/alexellis/pi_zero_stock
[5]: https://github.com/alexellis/pi_zero_stock
[6]: http://stockalert.alexellis.io/