Create the Python Microservice with `pyms`

PyMS, Python MicroService, is a Microservice chassis pattern like Spring Boot (Java) or Gizmo (Golang). PyMS is a collection of libraries, best practices and recommended ways to build microservices with Python which handles cross-cutting concerns:
- Externalized configuration
- Logging
- Health checks
- Metrics
- Distributed tracing
PyMS is powered by Flask, Connexion and Opentracing.
Activate the Virtual Environment
These steps are according to my skill level in python for creating a virtual environment
python3 -m venv pyms-tutorial
source pyms-tutorial/bin/activate
Environment activation is not mandatory to create the microservice with `pyms`
Install pyms
pip install py-ms[all]
Create a project from scaffold
PyMS has a command-line option to create a project template like Microservices Scaffold.
Prerequisite
- `PyMS` uses cookiecutter to download and install this template
pip install cookiecutter
- Create a git repository and keep the repo URL copied [Optional]
Create the Project
pyms startproject
Interactive way of answering a few questions to create the project.
pyms startprojectYou've downloaded /home/balu/.cookiecutters/cookiecutter-pyms before. Is it okay to delete and re-download it? [yes]: yes1. project_repo_url [https://github.com/python-microservices/microservices-scaffold]: https://github.com/JinnaBalu/sample-pyms 2. project_name [Python Microservices Boilerplate]: Python Microservice3. project_folder [python_microservice]: 4. project_short_description [Python Boilerplate contains all the boilerplate you need to create a Python package.]: Python Microservice Sample with PyMS5. create_model_class [y]: y6. microservice_with_swagger_and_connexion [y]: y7. microservice_with_traces [y]: y8. microservice_with_metrics [y]: y9. application_root [/python_microservice]: /python_microservice10. Select open_source_license:
1 - MIT license
2 - BSD license
3 - ISC license
4 - Apache Software License 2.0
5 - GNU General Public License v3
6 - Not open source
Choose from 1, 2, 3, 4, 5, 6 [1]: 6
Run Application
cd python_microservice/
pip install -r requirements.txt
Do some code changes in the manage.py which in bold or check with the tutorials in Tutorial Example.
from flask_script import Manager
from flask import jsonify, current_app, request
from project.app import create_appapp = create_app()
manager = Manager(app)@app.route("/")
def index():
app.logger.info("There are my headers: \n{}".format(request.headers))
return jsonify({"main": "hello world {}".format(current_app.config["APP_NAME"])})if __name__ == '__main__'
manager.run()
run the application with
python manage.py runserver
Output:
INFO:werkzeug: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Run the application as a container
Create the docker images with
docker build -t pyms-tutorial -f Dockerfile .
Run the container with
docker run -d -p 5001:5000 pyms-tutorial
Now the application is up and running on `http://127.0.0.1:5001/`
Reference
1. [PyMS](https://py-ms.readthedocs.io/en/latest/)
2. [Python Microservices Github](https://github.com/python-microservices/microservices-scaffold)