Simple microservices example of communication through gRPC and RabbitMQ
- Clone the repository
git clone https://github.com/KERELKO/microservices
- Copy the
.env.example
files to.env
for each service and fill in the required environment variables. Run this commands from the root directory
cd product_service
cat .env.example > .env
cd ../auth_service
cat .env.example > .env
- Run services with command
./entrypoint.sh
The ./entrypoint.sh
script starts up the necessary Docker containers
After installing and running Docker containers you must see API docs on your
local machine on the url http://127.0.0.1:8001/api/docs
(auth-service) and http://127.0.0.1:8000/api/docs
(product-service)
- Go to
/product_service/src/common/container.py
- Replace line
container.register(AbstractAuthService, instance=gRPCAuthService())
with
container.register(AbstractAuthService, instance=RabbitAuthService())
- Run the following command in the root directory
./entrypoint.rmq.sh
- Result is the same as in Usage section
To test the functionality of the services, you can use Postman
or pytest
; you can also use Swagger, but only for endpoints that do not have Cookie
requirements.
Important
At the moment (23.09.2024) API docs (Swagger) cannot set cookies, so it will always throw 401 Unauthorized
to product-service api calls
You can run tests with pytest in product-service with command (only in product_service
directory):
pytest tests/
this command will execute all available tests for the product-service
there are available only e2e tests that ensure communication with auth-service and product-service
Note
If you are developing microservices, it's a good practice to separate each microservice in its own repository. Here all microservices are in one repository for illustrative purposes, particularly for communication between services.
.
├── auth_service
│ ├── docker-compose.yaml
│ ├── Dockerfile
│ ├── entrypoint.sh
│ ├── grpc_
│ │ ├── __init__.py
│ │ ├── unary_auth_pb2_grpc.py
│ │ ├── unary_auth_pb2.py
│ │ ├── unary_auth_pb2.pyi
│ │ └── unary_auth.proto
│ ├── Makefile
│ ├── poetry.lock
│ ├── pyproject.toml
│ └── src
│ ├── common
│ │ ├── config.py
│ │ ├── db
│ │ │ ├── __init__.py
│ │ │ └── sqlalchemy
│ │ │ ├── config.py
│ │ │ ├── __init__.py
│ │ │ └── models.py
│ │ ├── di.py
│ │ ├── dto.py
│ │ ├── exceptions.py
│ │ ├── __init__.py
│ │ └── utils.py
│ ├── entrypoints
│ │ ├── fastapi_app.py
│ │ ├── grpc_server.py
│ │ ├── __init__.py
│ │ └── rabbitmq_consumer.py
│ ├── services
│ │ ├── auth.py
│ │ ├── exceptions.py
│ │ └── __init__.py
│ ├── storages
│ │ ├── __init__.py
│ │ └── repositories
│ │ ├── base.py
│ │ └── impl.py
│ └── web
│ ├── exceptions.py
│ ├── handlers.py
│ ├── __init__.py
│ └── schemas.py
├── entrypoint.sh
├── LICENSE
├── Makefile
├── message_broker
│ ├── docker-compose.yaml
│ └── rabbitmq
│ └── log
├── product_service
│ ├── docker-compose.yaml
│ ├── Dockerfile
│ ├── grpc_
│ │ ├── __init__.py
│ │ ├── unary_auth_pb2_grpc.py
│ │ ├── unary_auth_pb2.py
│ │ ├── unary_auth_pb2.pyi
│ │ └── unary_auth.proto
│ ├── main.py
│ ├── poetry.lock
│ ├── pyproject.toml
│ ├── src
│ │ ├── common
│ │ │ ├── config.py
│ │ │ ├── container.py
│ │ │ ├── dto.py
│ │ │ └── __init__.py
│ │ ├── __init__.py
│ │ ├── repositories
│ │ │ ├── base.py
│ │ │ ├── __init__.py
│ │ │ └── mongo.py
│ │ ├── services
│ │ │ ├── base.py
│ │ │ ├── exceptions.py
│ │ │ ├── impl.py
│ │ │ └── __init__.py
│ │ └── web
│ │ ├── handlers.py
│ │ ├── __init__.py
│ │ ├── middlewares.py
│ │ ├── schemas.py
│ │ └── utils.py
│ └── tests
│ ├── e2e
│ │ ├── conftest.py
│ │ ├── __init__.py
│ │ └── test_gw_communication.py
│ └── __init__.py
└── README.md
24 directories, 71 files