mirror of https://github.com/istio/istio.io.git
Translate zh-trans-1230 (istio#5413) (#5413)
This commit is contained in:
parent
c694917494
commit
5eb1aaf4d2
|
|
@ -1,50 +1,38 @@
|
|||
---
|
||||
title: Run a Microservice Locally
|
||||
overview: Learn how to work on a single service on your local machine.
|
||||
title: 本地运行微服务
|
||||
overview: 学习如何在本地机器上进行单一服务的工作。
|
||||
weight: 10
|
||||
---
|
||||
|
||||
{{< boilerplate work-in-progress >}}
|
||||
|
||||
Before the advent of microservice architecture, development teams built,
|
||||
deployed and ran the whole application as one large chunk of software. To test a
|
||||
small change in their module not merely by unit testing, the developers had to
|
||||
build the whole application. Therefore the builds took large amount of time.
|
||||
After the build, the developers deployed their version of the application into a
|
||||
test server. The developers ran the server either on a remote machine, or on their
|
||||
local computer. In the latter case, the developers had to install and operate a
|
||||
rather complex environment on their local computer.
|
||||
在微服务架构出现之前,开发团队会将整个应用程序作为一个大型软件进行构建、部署和运行。想要测试模块中一个微小的改变,
|
||||
开发人员不仅要通过单元测试,他们必须重新构建整个程序。因此,构建花费了大量的时间。完成构建后,
|
||||
开发人员将应用程序版本部署到测试服务器上。他们会把服务跑在远程或本地计算机中。在后一种情况下,开发者会在他们
|
||||
的本地计算机上安装并管理一个相当复杂的环境。
|
||||
|
||||
In the era of microservice architecture, the developers write, build, test and
|
||||
run small software services. Builds are fast. With modern frameworks like
|
||||
[Node.js](https://nodejs.org/en/) there is no need to install and operate
|
||||
complex server environments to test a single service, since the service runs as
|
||||
a regular process. You do not have to deploy your service to some environment to
|
||||
merely test it, so you just build your service and run it immediately on your
|
||||
local computer.
|
||||
在微服务架构时代,开发人员编写、构建、测试和运行小型的软件服务。构建是快速的。使用类似
|
||||
[Node.js](https://nodejs.org/en/) 这样的现代框架, 由于服务是作为常规进程来运行的,就不需要安装并管理
|
||||
复杂的服务环境来测试它了。您不再仅仅为了测试您的服务就得将它部署到某个环境了,您只需要构建您的服务并且直接在你本地机器上运行即可。
|
||||
|
||||
This module covers the different aspects involved in developing a single service
|
||||
on a local machine. You don't need to write code though. Instead, you build,
|
||||
run, and test an existing service: `ratings`.
|
||||
该模块涵盖了在本地机器上开发单个服务所涉及的不同方面。不过,您无需编写代码,只需要编译、运行和测试现有服务 `rating` 。
|
||||
|
||||
The `ratings` service is a small web app written in
|
||||
[Node.js](https://nodejs.org/en/) that can run on its own. It performs similar
|
||||
actions to those of other web apps:
|
||||
`ratings` 服务是用 [Node.js](https://nodejs.org/zh-cn/) 编写的一个可以单独运行的小型 web 应用程序。
|
||||
它与其他 web 应用程序执行类似的操作:
|
||||
|
||||
- Listen to the port it receives as a parameter.
|
||||
- Expect `HTTP GET` requests on the `/ratings/{productID}` path and return the
|
||||
ratings of the product matching the value the client specifies for `productID`.
|
||||
- Expect `HTTP POST` requests on the `/ratings/{productID}` path and update the
|
||||
ratings of the product matching the value you specify for `productID`.
|
||||
- 侦听它作为参数接受的端口。
|
||||
- 期待在 `/ratings/{productID}` 路径上的 `HTTP GET` 请求,并返回与客户端指定的 `productID`
|
||||
的值所匹配的产品的评级。
|
||||
- 期待在 `/ratings/{productID}` 路径上的 `HTTP POST` 请求,并更新与您指定的 `productID`
|
||||
的值所匹配的产品的评级。
|
||||
|
||||
Follow these steps to download the code of the app, install its dependencies,
|
||||
and run it locally:
|
||||
请按照下列步骤下载应用程序的代码,安装其依赖项,然后在本地运行它:
|
||||
|
||||
1. Download
|
||||
[the service's code]({{< github_blob >}}/samples/bookinfo/src/ratings/ratings.js)
|
||||
and
|
||||
[the package file]({{< github_blob >}}/samples/bookinfo/src/ratings/package.json)
|
||||
into a separate directory:
|
||||
1. 将
|
||||
[服务代码]({{< github_blob >}}/samples/bookinfo/src/ratings/ratings.js)
|
||||
和
|
||||
[其 package 文件]({{< github_blob >}}/samples/bookinfo/src/ratings/package.json)
|
||||
下载到一个单独的目录中:
|
||||
|
||||
{{< text bash >}}
|
||||
$ mkdir ratings
|
||||
|
|
@ -53,24 +41,23 @@ and run it locally:
|
|||
$ curl -s {{< github_file >}}/samples/bookinfo/src/ratings/package.json -o package.json
|
||||
{{< /text >}}
|
||||
|
||||
1. Skim the service's code and note the following elements on the code:
|
||||
- The web server's features:
|
||||
- listening to a port
|
||||
- handling requests and responses
|
||||
- The aspects related to HTTP:
|
||||
- headers
|
||||
- path
|
||||
- status code
|
||||
1. 浏览服务的代码,并注意代码中以下元素:
|
||||
- web 服务器的特征:
|
||||
- 监听一个端口
|
||||
- 处理请求和响应
|
||||
- 与 HTTP 相关的方面:
|
||||
- 请求头
|
||||
- 路径
|
||||
- 状态码
|
||||
|
||||
{{< tip >}}
|
||||
In Node.js, the web server's functionality is embedded in the code of the application. A Node.js
|
||||
web application runs as a standalone process.
|
||||
在 Node.js 中, web服务器的功能嵌入在应用程序的代码中。 一个 Node.js
|
||||
web 应用程序作为一个独立进程运行。
|
||||
{{< /tip >}}
|
||||
|
||||
1. Node.js applications are written in JavaScript, which means that there is no
|
||||
explicit compilation step. Instead, they use [just-in-time compilation](https://en.wikipedia.org/wiki/Just-in-time_compilation). To build a Node.js application, then means to install its dependencies. Install
|
||||
the dependencies of the `ratings` service in the same folder where you
|
||||
stored the service code and the package file:
|
||||
1. Node.js 应用程序是用 JavaScript 编写的, 这意味着没有显式编译步骤。 相反,它们
|
||||
使用 [just-in-time 即时编译](https://zh.wikipedia.org/wiki/%E5%8D%B3%E6%97%B6%E7%BC%96%E8%AF%91)。要
|
||||
构建 Node.js 应用程序, 则意味着要安装其依赖库。 将 `rating` 服务的依赖库安装在存储服务代码和 package 文件的同一目录下:
|
||||
|
||||
{{< text bash >}}
|
||||
$ npm install
|
||||
|
|
@ -82,7 +69,7 @@ and run it locally:
|
|||
added 24 packages in 2.094s
|
||||
{{< /text >}}
|
||||
|
||||
1. Run the service, passing `9080` as a parameter. The application then listens on port 9080.
|
||||
1. 通过传递 `9080` 参数来运行服务, 然后应用程序在9080端口上监听。
|
||||
|
||||
{{< text bash >}}
|
||||
$ npm start 9080
|
||||
|
|
@ -92,38 +79,36 @@ and run it locally:
|
|||
{{< /text >}}
|
||||
|
||||
{{< tip >}}
|
||||
The `ratings` service is a web app and you can communicate with it as you would
|
||||
with any other web app. You can use a browser or a command line web client like
|
||||
[`curl`](https://curl.haxx.se) or [`Wget`](https://www.gnu.org/software/wget/).
|
||||
Since you run the `ratings` service locally, you can also access it via the
|
||||
`localhost` hostname.
|
||||
该 `ratings` 服务是一个 web 应用程序,您可以像访问其他 web 应用程序那样访问它。
|
||||
您可以使用浏览器或者像
|
||||
[`curl`](https://curl.haxx.se) 或 [`Wget`](https://www.gnu.org/software/wget/)
|
||||
的命令行 web 客户端。由于您在本地运行了 `rating` 服务,因此您也可以通过 `localhost`
|
||||
主机名访问它。
|
||||
{{< /tip >}}
|
||||
|
||||
1. Open [http://localhost:9080/ratings/7](http://localhost:9080/ratings/7) in
|
||||
your browser or access `ratings` using the `curl` command:
|
||||
1. 在浏览器中打开 [http://localhost:9080/ratings/7](http://localhost:9080/ratings/7) 或者使用 `curl` 命令来访问 `ratings`:
|
||||
|
||||
{{< text bash >}}
|
||||
$ curl localhost:9080/ratings/7
|
||||
{"id":7,"ratings":{"Reviewer1":5,"Reviewer2":4}}
|
||||
{{< /text >}}
|
||||
|
||||
1. Use the `POST` method of the `curl` command to set the ratings for the
|
||||
product to `1`:
|
||||
1. 使用 curl 命令的 POST 方法将产品的评级设置为 1 :
|
||||
|
||||
{{< text bash >}}
|
||||
$ curl -X POST localhost:9080/ratings/7 -d '{"Reviewer1":1,"Reviewer2":1}'
|
||||
{"id":7,"ratings":{"Reviewer1":1,"Reviewer2":1}}
|
||||
{{< /text >}}
|
||||
|
||||
1. Check the updated ratings:
|
||||
1. 检查更新的评级:
|
||||
|
||||
{{< text bash >}}
|
||||
$ curl localhost:9080/ratings/7
|
||||
{"id":7,"ratings":{"Reviewer1":1,"Reviewer2":1}}
|
||||
{{< /text >}}
|
||||
|
||||
1. Use `Ctrl-C` in the terminal running the service to stop it.
|
||||
1. 在服务运行的终端使用 Ctrl-C 停止它。
|
||||
|
||||
Congratulations, you can now build, test, and run a service on your local computer!
|
||||
恭喜, 您现在可以在本地计算机上构建、测试和运行服务了!
|
||||
|
||||
You are ready to learn how to package the service into a container.
|
||||
您已经做好了学习如何打包服务到容器的准备了。
|
||||
|
|
|
|||
Loading…
Reference in New Issue