开发环境 - 如何调用/使用其他微服务 [英] Developer environment - how to call/consume other micro services

查看:446
本文介绍了开发环境 - 如何调用/使用其他微服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我的环境 - Java,Play2,MySql

My Environment - Java, Play2, MySql

在Play2上写入3个无状态Restful Microservices - > / S1,/ S2,/ S3

I've written 3 stateless Restful Microservices on Play2 -> /S1,/S2,/S3

S1从S2和S3消耗数据。所以当用户命中/ S1时,该服务异步调用/ S2,/ S3,合并数据并返回最终的json输出。侧面注意 - 这些服务将最终作为码头图像发货。

S1 consumes data from S2 and S3. So when user hits /S1, that service asynchronously calls /S2, /S3, merges data and returns final json output. Side note - The services will be shipped eventually as docker images.

为了在开发人员环境中进行测试,我在端口9000,9001上运行/ s1,/ s2,/ s3 9002。我从一个配置文件中取回端口号码。我打了服务,一切正常。但是有一个更好的方法来设置我的开发人员框上的测试环境吗?示例 - 如果我想要运行20个服务等等。

For testing in developer environment, I run /s1,/s2,/s3 on ports 9000, 9001 and 9002 respectively. I pickup the port numbers from a config file etc. I hit the services and everything works fine. But there is a better way to setup the test env on my developer box correct? Example - What if I want to run 20 services etc..

所以说,在生产中,他们将被称为像mydomain.com/s1,mydomain.com / s2,mydomain.com/s3等。我想在我的开发人员环境框中完成这一点....我想有一些反向代理涉及我想象。

So with that said, on production they will be called just like mydomain.com/s1, mydomain.com/s2, mydomain.com/s3 etc. I want to accomplish this on my developer environment box....I guess there's some reverse proxying involved I would imagine.

问题

所以问题是,如何调用/ S2 / S3,无需在开发者环境中指定或使用端口号。人们如何在本地机器上测试微服务?

So the question is, how do I call /S2 and /S3 from within S1 without specifying or using the port number on developer environment. How are people testing microservices on their local machine?

额外奖金

我将把我的服务作为码头图像发货,如何用docker容器(每个容器运行一个服务)完成同样的事情。

Knowing that I'll be shipping my services as docker images, how do I accomplish the same thing with docker containers (each container running one service)

推荐答案

最简单的方法(IMO)是将您的开发环境设置为尽可能接近您的生产环境。如果您希望生产应用程序使用20个微服务器,每个应用程序都运行在单独的容器中,然后使用您的开发机器。这样,当您部署到生产时,您不必从使用端口更改为使用主机名。

The easiest way (IMO) is to set up your development environment to mirror as closely as possible your production environment. If you want your production application to work with 20 microservices, each running in a separate container, then do that with your development machine. That way, when you deploy to production, you don't have to change from using ports to using hostnames.

设置大量微服务器的最简单方法一堆不同的容器可能与或Docker即将推出的综合协调工具。由于我们没有关于即将到来的所有细节,我将使用图。这是一个生产服务器的 fig.yml 文件:

The easiest way to set up a large set of microservices in a bunch of different containers is probably with Fig or with Docker's upcoming integrated orchestration tools. Since we don't have all the details on what's coming, I'll use Fig. This is a fig.yml file for a production server:

application:
  image: application-image
  links:
    - service1:service1
    - service2:service2
    - service3:service3
    ...

service1:
  image: service1-image
  ...

service2:
  image: service2-image
  ...

service3:
  image: service3-image
  ...

缩写为 fig.yml 文件将在应用程序和所有服务之间设置链接,以便在代码中,您可以通过主机名 service1 service2 等。

This abbreviated fig.yml file will set up links between the application and all the services so that in your code, you can refer to them via hostname service1, service2, etc.

出于开发目的,有很多更多需要进入这里:对于您可能希望安装编辑代码的目录的每个服务,您可能需要公开一些端口,以便您可以直接测试服务等。但是,它的核心是,开发环境是一样的生产环境。

For development purposes, there's lots more that needs to go in here: for each of the services you'll probably want to mount a directory in which to edit the code, you may want to expose some ports so you can test services directly, etc. But at it's core, the development environment is the same as the production environment.

听起来好像很多,但是像图这样的工具使得配置和运行应用程序变得非常简单。如果你不想使用图,那么你可以用Docker命令来做同样的事情 - 关键是容器之间的链接。我可能会创建一个脚本来为生产和开发环境设置。

It does sound like a lot, but a tool like Fig makes it really easy to configure and run your application. If you don't want to use Fig, then you can do the same with Docker commands - the key is the links between containers. I'd probably create a script to set things up for both the production and development environments.

这篇关于开发环境 - 如何调用/使用其他微服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆