Protocol buffers organization in Python microservices

In this post one of the approaches to organizing Protobuf files in microservices architecture using gRPC.

For proto files, I use a separate repository named protos. The directory structure replicates the one in a microservice. If in microservice_1 there is a directory app/grpc for stubs, then in protos there is microservice_1/app/grpc.

In each microservice, I include a Makefile with commands for generating server and client stubs from Protobuf files. The proto target compiles the Protobuf definitions into Python code using grpcio_tools.

install_grpc_tools:
	pip install grpcio-tools
	pip install mypy-protobuf

proto: ## prepare grpc files
	python -m grpc_tools.protoc --proto_path=../protos/microservice_1 --python_out=. --grpc_python_out=. --mypy_out=. ../protos/microservice_1/app/grpc/family-sites.proto

I use mypy-protobuf and types-protobuf libs to bypass mypy linting.

setup.cfg to exclude the directory with stubs from flake8 checks

[flake8]
max-line-length = 120
exclude =
    app/grpc

And in .pre-commit-config.yaml:

repos:
  - repo: https://github.com/PyCQA/flake8
  ...
  - repo: https://github.com/pre-commit/mirrors-mypy
  ...
exclude: app/grpc
comments powered by Disqus