FastAPI GRPC Integration

fastapi grpc

In a microservices architecture, services often need to communicate with each other. While REST APIs are common, gRPC offers better performance, smaller payloads, and native code generation across languages. In this article, we’ll build two microservices in FastAPI that talk to each other using gRPC.

We will cover:

  • How to define a gRPC contract using Protobuf
  • How to implement a gRPC server in one FastAPI microservice
  • How to consume that gRPC service in another FastAPI microservice
  • How to run everything with Docker Compose

Requirements:

  • Python version “3” installed on your computer.
  • Docker and Docker compose installed on your computer.

Project Structure

This is the final project structure. Make sure to create all folders first.

grpc-fastapi-demo/
├── proto/
│ └── greeter.proto
├── greeter_service/
│ ├── main.py
│ ├── greeter_pb2.py
│ └── greeter_pb2_grpc.py
├── client_service/
│ ├── main.py
│ └── greeter_pb2.py ← same as in greeter_service
│ └── greeter_pb2_grpc.py ← same as in greeter_service
├── docker-compose.yml
├── requirements.txt
└── README.md

Step 1: Define the gRPC Contract with Protobuf

Create a file named “greeter.pro” in “proto” folder.

Put these contents in it:

Step 2: Install necessary libraries

The next step is to create proto files from “greeter.pro” file which we have created in the previous step.

To do this you need a Python library named “grpcio-tools”.

To install this library, you first need a Python virtual environment.

In the root of the project run this command:

or

If “virtualenv” not installed on your system, you need to first run this command:

or

Then activate virtualenv by using this command:

And then install the necessary library:

Step 3: Create proto files

Now it’s time to create proto files from “greeter.pro”, run this command:

This will create proto files.

Then copy generated proto files into “client_service” folder:

Step 4: Create “greeter_service”

Create a main.py file inside “greeter_service” folder and put these contents in it:

As you can see this will create a GRPC server using Python.

Step 5: Create “client_service”

In the “client_service” folder create a file named “main.py” and put these contents in it:

As you can see in this file, we are creating a GRPC connection to “greeter_service” using the name of the container and the specified port.

Then we are calling SayHello function from “client_service” to “greeter_service”.

Step 6: Create Docker compose file

Create a docker compose file inside the root of the project, and put these contents in it:

Step 7: Create Dockerfile for “greeter_service”:

In the “greeter_service” folder create a file named “Dockerfile” and put these contents in it:

Step 8: Create Dockerfile for “client_service”

In the “client_service” folder create a file named “Dockerfile” and put these contents in it:

Step 9: Create “requirements.txt” for “greeter_service”

In the “greeter_service” folder create a file named “requirements.txt” and put these contents in it:

Step 10: Create “requirements.txt” for “client_service”:

In the “client_service” folder create a file named “requirements.txt” and put these contents in it:

Step 10: Final check

Now, you should have a folder structure like this:

grpc-fastapi-demo/
├── proto/
│ └── greeter.proto
├── greeter_service/
│ ├── main.py
│ ├── greeter_pb2.py
│ └── greeter_pb2_grpc.py
├── client_service/
│ ├── main.py
│ └── greeter_pb2.py ← same as in greeter_service
│ └── greeter_pb2_grpc.py ← same as in greeter_service
├── docker-compose.yml
├── requirements.txt
└── README.md

Step 11: Running the project

To run the project, open the terminal in the root of project and run this command:

Step 12: Testing the project

Open your web browser and enter this address:

This will show you something like this:

Which shows that the project is working fine.

Step 13: How it works

When you open the address in the browser, it is requesting to “client_service” which is a FastAPI app, and then FastAPI will create a GRPC connection to “greeter_service” and then “greeter_service” respond back using GRPC connection and then FastAPI returns the response to your browser.

The link of Github for this project is this:

https://github.com/mjmichael73/fastapi-grpc-integration

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *