robertwerner_sf
robertwerner_sf

Reputation: 1303

Locally Developing Multiple Microservices

Imagine you have a React front-end (FE) which is interacting with a Python based back-end (BE) of multiple microservices. So the FE might call an endpoint at BE-A and then later at BE-B and perhaps later at BE-C. Some pages may need to interact with BE-A, BE-B, and BE-C all at once.

When this is deployed remotely, all works perfectly fine but when working on things locally, is there an easy way to do this with BE-A running in one Terminal session, BE-B in a second Terminal session, and so on?

Note: I'm aware of the local Docker approach but am wondering if there's a simpler way to do it without anything like Docker?

Upvotes: 0

Views: 1282

Answers (1)

xargs
xargs

Reputation: 3079

I am not really sure what you use with python but I will assume you are using something like flusk. Lets say you are using flask and you have your scripts for services as micro-service-a.py, micro-service-b.py and micro-service-c.py. You could run them like this:

In Linux Bash

Open a terminal session 1 and run:

$ export FLASK_APP=micro-service-a.py

$ flask run --host 0.0.0.0 --port 5001

Open a terminal session 2 and run:

$ export FLASK_APP=micro-service-b.py

$ flask run --host 0.0.0.0 --port 5002

Open a terminal session 3 and run:

$ export FLASK_APP=micro-service-c.py

$ flask run --host 0.0.0.0 --port 5003

This way you will each of your services running in a different port so you can consume each service api from different port on localhost like localhost:5001, localhost:5002 and localhost:5003.

In Windows Powershel

The only difference would be that you would set the env:FLASK_APP in a different way. For example:

$env:FLASK_APP = "micro-service-a.py"

flask run

You can read more about the CLI commands here.

You can even write some small powershell or bash script to initialize, start the processes, stop and remove them. You don't have to do it manually in all 3 terminal sessions and if you have more micro-services then it would be even more terminal sessions which you will need to manage. That would make your work easier.


You can do that sure but instead I would highly recommend using Docker for this purpose. The good thing about docker is that you would create a docker-compose which would have its own network for your environment. This would include all the parts of your application including your ReactApp as one container and a container per micro-service. This way you could even have different python versions in each micro-service and the commands to initialize, build and run, stop, start, remove your whole app would be like:

docker-compose -f "docker-compose-my-react-and-python-app.yml" up -d

docker-compose -f "docker-compose-my-react-and-python-app.yml" stop

docker-compose -f "docker-compose-my-react-and-python-app.yml" start

docker-compose -f "docker-compose-my-react-and-python-app.yml" down -v 

Note: I'm aware of the local Docker approach but am wondering if there's a simpler way to do it without anything like Docker?

Using docker for this is much simpler then writing and managing all those scripts(bash or powerhsell) manually.

You can find a lot of examples how to create Dockerfile and docker-compose files for similar apps as you are building here on Stack-overflow or other sources online.

Upvotes: 1

Related Questions