Skip to content

Latest commit

 

History

History
174 lines (162 loc) · 5.76 KB

README.md

File metadata and controls

174 lines (162 loc) · 5.76 KB

Microservices example

Simple microservices example of communication through gRPC and RabbitMQ

Schema

image

Technologies

  1. gRPC
  2. FastAPI
  3. RabbitMQ
  4. MongoDB
  5. PostgreSQL
  6. Docker & Docker Compose

How to Use

Requirements

  1. Docker
  2. Docker Compose

Install & Run

  1. Clone the repository
git clone https://github.com/KERELKO/microservices
  1. 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
  1. Run services with command
./entrypoint.sh

The ./entrypoint.sh script starts up the necessary Docker containers

Usage

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)

How to use RabbitMQ RPC instead of gRPC

  1. Go to /product_service/src/common/container.py
  2. Replace line
container.register(AbstractAuthService, instance=gRPCAuthService())

with

container.register(AbstractAuthService, instance=RabbitAuthService())
  1. Run the following command in the root directory
./entrypoint.rmq.sh
  1. Result is the same as in Usage section

Testing

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

Pytest

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

Project Structure

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