Development environment - how to call / consume other microservices

Background

My environment is Java, Play2, MySql

I wrote 3 stateless Restresh Microservices on Play2 → / S1, / S2, / S3

S1 consumes data from S2 and S3. Therefore, when the user presses / S 1, this service asynchronously calls / S 2, / S3, concatenates the data, and returns the final json output. Side notes. Services will eventually be shipped as docker images.

For testing in the development environment, I run / s 1, / s2, / s3 on ports 9000, 9001 and 9002, respectively. I select port numbers from a configuration file, etc. I am in the services and everything is working fine. But is there a better way to configure test env on my developer block correctly? Example. What if I want to run 20 services, etc.

Thus, they will be called in the same way as mydomain.com/s1, mydomain.com/s2, mydomain.com/s3, etc. I want to accomplish this in my core development environment .... I’ll assume that there are some reverse proxies that I assumed.

Question

So the question is, how can I call / S 2 and / S3 from S1 without specifying or using a port number in the developer environment. How do people test microservices on their local machine?

Extra bonus

Knowing that I will send my services in the form of docker images, how can I do the same with docker containers (each container works with one service)

+2
source share
2 answers

The easiest way (IMO) is to set up your development environment to reflect your production environment as accurately as possible. If you want your production application to work with 20 microservices, each of which was launched in a separate container, then do it using your development machine. Thus, when deployed to production, you do not need to change the use of ports to use host names.

The easiest way to set up a large set of microservices in a bunch of different containers, probably with Fig or with the upcoming Docker integrated orchestration tools . Since we do not have all the details about what will happen, I will use fig. This is the fig.yml file for the 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 ... 

This shortened fig.yml file will establish connections between the application and all services so that in your code you can access them through the hostname service2 , service2 , etc.

For development purposes, you need even more: for each of the services that you probably want to mount a directory for editing code, you can open several ports so that you can test services directly, etc. But in this core, the development environment is the same as in the production environment.

It sounds a lot, but a tool like Fig really makes it easy to set up and run your application. If you do not want to use Fig, then you can do the same with the Docker commands - the key is the links between the containers. I would probably create a script to configure for both the production environment and for development.

+1
source

Example. What if I want to run 20 services, etc.

docker will create entries in the hosts hosts file for each of the associated containers using their alias. Therefore, if you are linking many containers, you can simply access them using your nickname. Do not map the port in the public port using -p 9000: 9000. Thus, all your services can be on port 9000 on their own docker host, which can be found with / etc / hosts

"how can i do the same ..."

This is an open-ended question, here are some good reads on this topic. SkyDock , and SkyDNS is the best way to discover the service and weave simplifies the use of networks between remote docker containers. I have not seen a better end-to-end solution, although some may be there.

0
source

Source: https://habr.com/ru/post/980838/


All Articles